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
set @mysql=@mysql+' and datediff(day,'+@s_StartTime+',WarList.WarDate)>=0'