日期:2014-05-17  浏览次数:20573 次

代码里面的sql语法错误

" select top @size  a.Action_ID, a.[User_ID], a.Total_Score ,b.Action_Desp,b.CreateTime ,b.Point_Change from "
        + " tbl_Action_Hist b inner join tbl_Record_Score a on a.Action_ID=b.Action_ID  where a.Action_ID  not in (select top (@size * (@now -1)) a.Action_ID  from  "
        + " tbl_Action_Hist b ) and b.Student_ID=@stuid and b.Point_Change like '-% ' and b.CreateTime between @starTime and  @endTime"


这些参数的类型string stuid, int size, int now, DateTime starTime, DateTime endTime
在sql里面没报错,但在程序里面运行之后报错。
'@size' 附近有语法错误。
关键字 'and' 附近有语法错误。 

------解决方案--------------------
引用:
引用:在外面@size 是变量 ,放到字符串里面就变成'@size'字符了。
可是我以前写int类型的变量也是直接写的啊,难道是因为在top 后面吗


这样是可以的:
declare @size int
set @size=2
select top (@size) * from master..spt_values


你把括号去掉就报错,主要是你放到字符串里面了。