那这样怎么不对呢 exec("SELECT TOP "+ @oPageSize +" * FROM bbstopic where id NOT IN (SELECT Top "+ @oPageSize*(@oIndex-1) + " id FROM bbstopic WHERE referencesubtitleid="+@oBoardId+" ORDER BY currentdate DESC) ORDER BY currentdate DESC") 错误在@oPageSize*(@oIndex-1)里,但我不知道 为什么错 了
这个地方不能出现表达式形式 你利用另一个变量转换一下就可以啦 另外在数据库中不能使用双引号的,现将你的代码改动如下: 你试一下:declare @tt int set @tt=@oPageSize *(@oIndex-1) exec('SELECT TOP '+ @oPageSize +' * FROM bbstopic where id NOT IN (SELECT Top '+ @tt+ ' id FROM bbstopic WHERE referencesubtitleid='+@oBoardId+' ORDER BY currentdate DESC) ORDER BY currentdate DESC')
直接使用exec('SELECT TOP 20 * FROM bbstopic where id NOT IN (SELECT Top '+ @oCount+' id FROM bbstopic WHERE referencesubtitleid='+@oBoardId+' ORDER BY currentdate DESC) ORDER BY currentdate DESC') 这样是正确的 可为什么下面就是错误的呢,错误信息是将 varchar 值 'SELECT TOP 20 * FROM bbstopic where id NOT IN (SELECT Top ' 转换为数据类型为 int 的列时发生语法错误。 declare @oCount int declare @oString varchar set @oString='SELECT TOP 20 * FROM bbstopic where id NOT IN (SELECT Top '+ @oCount+' id FROM bbstopic WHERE referencesubtitleid='+@oBoardId+' ORDER BY currentdate DESC) ORDER BY currentdate DESC' exec(@oString)
exec("SELECT TOP "+ @oPageSize +" * FROM bbstopic where id NOT IN (SELECT Top "+ @oPageSize*(@oIndex-1) + " id FROM bbstopic WHERE referencesubtitleid="+@oBoardId+" ORDER BY currentdate DESC) ORDER BY currentdate DESC")
错误在@oPageSize*(@oIndex-1)里,但我不知道 为什么错 了
你利用另一个变量转换一下就可以啦
另外在数据库中不能使用双引号的,现将你的代码改动如下:
你试一下:declare @tt int
set @tt=@oPageSize *(@oIndex-1)
exec('SELECT TOP '+ @oPageSize +' * FROM bbstopic where id NOT IN (SELECT Top '+ @tt+ ' id FROM bbstopic WHERE referencesubtitleid='+@oBoardId+' ORDER BY currentdate DESC) ORDER BY currentdate DESC')
这样是正确的
可为什么下面就是错误的呢,错误信息是将 varchar 值 'SELECT TOP 20 * FROM bbstopic where id NOT IN (SELECT Top ' 转换为数据类型为 int 的列时发生语法错误。
declare @oCount int
declare @oString varchar
set @oString='SELECT TOP 20 * FROM bbstopic where id NOT IN (SELECT Top '+ @oCount+' id FROM bbstopic WHERE referencesubtitleid='+@oBoardId+' ORDER BY currentdate DESC) ORDER BY currentdate DESC'
exec(@oString)