当前位置:数据库 > SQLServer >>

mssql存储过程

declare @j int select @j=0 select top 1 编号 from salaryp where 编号 not in(select top @j 编号 from salaryp ) 报的错误是“@j附近有错误” 请问一下,是什么原因
补充:declare @j int 
declare @name varchar(20)
select @j=0
select @name=(select  top 1 编号 from salaryp where 编号 not in(select  top @j 编号 from salaryp))
其实问题是这样的
答案:改为:
declare @j int 
select @j=0
exec('select top 1 编号 from salaryp where 编号 not in(select  top  '+@j+'  编号 from salaryp ) ')
用动态执行的语句就可以了,变量应该只能在后面条件中用吧,如果想在前面用的话 就要用exec来执行吧
其他:你应该试试当你查询前0条数据的时候是不是会出错 原因就在于sql server 的sql语法中,top后面只能跟一个常数,而不能跟一个变量,有时候可以通过rowcount去弥补,或者用动态语句.
比如: 
  DECLARE @n INT
  SET @n = 100
  SET ROWCOUNT @n
  SELECT * FROM Table_1 
  SET @n = 100

这个就相当于:
  select top 100
  from Table_1.
但是对于你这种情况,还是动态语句好实现.1楼那样就可以! 

上一个:谁能帮我把BAK备份文件还原到远程mssql数据库
下一个:MSSQL数据库中有表用户表USER,已知密码“888”在表中对应的字符为“C6B9BDB9BDB9C7BCBCBDBDBC”。

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,