日期:2014-05-18  浏览次数:20326 次

SQL存储过程传递参数的问题
SQL code


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[proc_new]
@id int=0,
@mun int=100
as
select top @num * from employee where id>@id
GO

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
-----------------------------------
error:
消息 102,级别 15,状态 1,过程 proc_newproc,第 5 行
'@num' 附近有语法错误。



------解决方案--------------------
SQL code

--加个括号试试哟
select top (@num) * from employee where id>@id

------解决方案--------------------
SQL code
ALTER proc [dbo].[proc_new]
@id int=0,
@num int=100  --变量名定义错了
as
select top (@num) * from employee where id>@id --加括号
GO

------解决方案--------------------
2005中top支持参数
select top (@num) * from employee where id>@id

2000的话需要拼接成动态sql以后执行

exec ('sekect top '+cast(@num as varchar(10))+' * from employee where id>'+cast(@id as varchar(10)))

------解决方案--------------------
SQL code
2005的话

select top (@num) * from employee where id>@id

2000的话
exec ('sekect top '+ltrim(@num)+' * from employee where id>'+ltrim(@id)'