当前位置: 代码迷 >> Sql Server >> 表名改变量后出现异常
  详细解决方案

表名改变量后出现异常

热度:24   发布时间:2016-04-27 11:00:32.0
表名改变量后出现错误
不知道该怎么改

SQL code
declare @TableName nvarchar(100),@Sql nvarchar(1000)set @TableName = 'YY_TEST.dbo.gh_ghzdk  b'    set @sql=N'select @CountApp=count(*) '+         N'from AppointList a' +         N'left join ' + @TableName +'  on a.MedicalNo=b.blh and dbo.f_IsSameDate(a.AppointTime,b.ghrq)=1' +         N'where AppointTime >= @S_BeginTime and AppointTime<= @S_EndTime and b.blh is null and a.cancel=0'    exec sp_executesql @sql,[email protected]_BeginTime datetime,@S_EndTime datetime,@CountApp int output',@S_BeginTime,@S_EndTime,@CountApp output下边注释掉的是可以正常运行的    /*    select @CountAppNo=count(*) --爽约人数,算出爽约率就行了,在对赴约进行重复记录Distinc是,可能去掉预约多科室的情况。    from AppointList a     left join YY_TEST.dbo.gh_ghzdk b on a.MedicalNo=b.blh and dbo.f_IsSameDate(a.AppointTime,b.ghrq)=1    where AppointTime >= @S_BeginTime and AppointTime<= @S_EndTime and b.blh is null and a.cancel=0    */    


服务器: 消息 107,级别 16,状态 2,行 1
列前缀 'a' 与查询中所用的表名或别名不匹配。
服务器: 消息 107,级别 16,状态 1,行 1
列前缀 'a' 与查询中所用的表名或别名不匹配。
服务器: 消息 107,级别 16,状态 1,行 1
列前缀 'a' 与查询中所用的表名或别名不匹配。

------解决方案--------------------
提示已经够明显的了。
  相关解决方案