存储过程调用出错
CREATE proc info
@infonum varchar(20),
@infotable varchar(20),
@infotype varchar(20),
@intype varchar(20),
@infoid varchar(20)
as
declare @sql varchar(100)
set set @sql = ' select top ' + @infonum + ' * from ' + @infotable + ' where ' + @infotype + ' = ( ' ' ' + @intype + ' ' ') order by ' + @infoid+ ' desc '
EXEC(@sql)
GO
我这样调用出错
exec dgdcc_news_info '10 ', 'news ',datediff(day, ptime, getdate()), '1 ', 'ptime '
错误如下:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'day ' 附近有语法错误。
谢谢!
------解决方案--------------------alter proc info
@infonum varchar(20),
@infotable varchar(20),
@infotype varchar(100), --长度不够
@intype varchar(20),
@infoid varchar(20)
as
declare @sql varchar(100)
--有两个set
set @sql = ' select top ' + @infonum + ' * from ' + @infotable + ' where ' + @infotype + ' = ( ' ' ' + @intype + ' ' ') order by ' + @infoid+ ' desc '
print(@sql)
GO
--1.存储过程名写错
--2.传参数时加引号
exec info '10 ', 'news ', 'datediff(day, ptime, getdate()) ', '1 ', 'ptime '