sql server 数据库 存储过程 必须声明标量变量 错误
ALTER PROCEDURE [dbo].[AllStatisticsSearch1]
@s_StartTime varchar(50)
AS
BEGIN
declare @mysql varchar(5000)
set @mysql='SELECT [UserList].id,PeopleName'
set @mysql=@mysql+' FROM [UserList] left join WarList on WarList.PeopleID = UserList.ID '
set @mysql=@mysql+' and datediff(day,@s_StartTime,WarList.WarDate)>=0'--开始日期
set @mysql=@mysql+'where 1=1'
exec (@mysql)
出错:必须声明标量变量 "@s_StartTime"。
------解决方案--------------------变量前后要加号的。另外你可以先print你的语句检查错误
------解决方案--------------------
ALTER PROCEDURE [dbo].[AllStatisticsSearch1]
@s_StartTime nvarchar(50)
AS
BEGIN
declare @mysql nvarchar(4000)
set @mysql='SELECT [UserList].id,PeopleName'
set @mysql=@mysql+' FROM [UserList] left join WarList on WarList.PeopleID = UserList.ID '
set @mysql=@mysql+' and datediff(day,@s_StartTime,WarList.WarDate)>=0'--开始日期
set @mysql=@mysql+'where 1=1'
exec sp_executesql @mysql,N'@s_StartTime nvarchar(50)',@s_StartTime
参数化要用sp_executesql执行,exec (@mysql)用这个执行的话,字符串里面的变量必须全部用字符拼接的形式如
set @mysql=@mysql+' and datediff(day,'+@s_StartTime+',WarList.WarDate)>=0'