代码里面的sql语法错误

heli_1005 2013-04-25 06:25:08

" 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' 附近有语法错误。
...全文
236 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
heli_1005 2013-04-26
  • 打赏
  • 举报
回复
引用 7 楼 shoppo0505 的回复:
Quote: 引用 6 楼 heli_1005 的回复: select * from table where Action_ID=@actid Quote: 如果你把这句语句在程序中用引号括起来,再让SQL执行,肯定会报错,你在SQL窗口下当然没错。

  StringBuilder strBl = new StringBuilder("select  Action_ID, Question_ID, Type, Score, Fluency, Accuracy, Inegrity, "
        + " Rhythm, Others from  tbl_Record_Score_Detail where Action_ID=@actid");
真的没报错
shoppo0505 2013-04-25
  • 打赏
  • 举报
回复
[quote=引用 6 楼 heli_1005 的回复:] select * from table where Action_ID=@actid [quote] 如果你把这句语句在程序中用引号括起来,再让SQL执行,肯定会报错,你在SQL窗口下当然没错。
heli_1005 2013-04-25
  • 打赏
  • 举报
回复
引用 5 楼 maco_wang 的回复:
引用 4 楼 heli_1005 的回复: 引用 3 楼 maco_wang 的回复:在外面@size 是变量 ,放到字符串里面就变成'@size'字符了。 可是我以前写int类型的变量也是直接写的啊,难道是因为在top 后面吗 这样是可以的: SQL code ? 123 declare @size intset @size=2 select top (……
select * from table where Action_ID=@actid 这句里面,Action_ID也是int类型,为什么就不会报错呢
叶子 2013-04-25
  • 打赏
  • 举报
回复
引用 4 楼 heli_1005 的回复:
引用 3 楼 maco_wang 的回复:在外面@size 是变量 ,放到字符串里面就变成'@size'字符了。 可是我以前写int类型的变量也是直接写的啊,难道是因为在top 后面吗
这样是可以的:
declare @size int
set @size=2
select top (@size) * from master..spt_values
你把括号去掉就报错,主要是你放到字符串里面了。
heli_1005 2013-04-25
  • 打赏
  • 举报
回复
引用 3 楼 maco_wang 的回复:
在外面@size 是变量 ,放到字符串里面就变成'@size'字符了。
可是我以前写int类型的变量也是直接写的啊,难道是因为在top 后面吗
叶子 2013-04-25
  • 打赏
  • 举报
回复
在外面@size 是变量 ,放到字符串里面就变成'@size'字符了。
heli_1005 2013-04-25
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
你这里的@size变量要直接转换成数值再拼接入SQL语句。 比如: int size = 10; "select top " + @size + " a.Action_ID, ......" 你这里的两个错误都是这个问题引起的。
为什么不能直接用变量呢
shoppo0505 2013-04-25
  • 打赏
  • 举报
回复
你这里的@size变量要直接转换成数值再拼接入SQL语句。 比如: int size = 10; "select top " + @size + " a.Action_ID, ......" 你这里的两个错误都是这个问题引起的。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧