当前位置: 代码迷 >> Sql Server >> 使用 sp_executesql出现列名无效解决思路
  详细解决方案

使用 sp_executesql出现列名无效解决思路

热度:16   发布时间:2016-04-27 15:52:21.0
使用 sp_executesql出现列名无效
ALTER   PROCEDURE   selectmessage
(
@tablename   nchar(10),
@id   bigint   ,
        @time   datetime,
@name_user   nchar(10)
)
AS
DECLARE       @SqlStr     NVARCHAR(1000)      
SET       @SqlStr=N 'select   id   ,status,smname,rmname,message   from '
+CONVERT(NVARCHAR(100),@tablename)
+N 'where     time> '+ ' ' ' '+CONVERT(NVARCHAR(100),@time)+ ' ' ' '
+N 'and   id> '+ ' ' ' '+CONVERT(NVARCHAR(100),@id)+ ' ' ' '
+N '   and
          (status= '+ ' ' ' '+N 'false '+ ' ' ' '+N '   or   rmname= '+ ' ' ' '+CONVERT(NVARCHAR(10),@name_user)+ ' ' ' '+N '
            or   smname= '+ ' ' ' '+CONVERT(NVARCHAR(10),@name_user)+ ' ' ' '+N 'or   rmname   like   '+ ' ' ' '+N '大家%   '+ ' ' ' '+N ') '

  EXEC   sp_executesql   @SqlStr
                                                                                                                                 
/*   SET   NOCOUNT   ON   */  
RETURN  


请问怎么修改?


------解决方案--------------------
...from '+...+ ' where ...
缺少空格,如果不是这样的话,可能是你传入的表没有这些字段