sql server存储过程赋值问题
关于exec(sql语句)赋值问题,
DECLARE @count int, @sql VARCHAR(1000), @flag VARCHAR(20)
SET @flag=SUBSTRING(@rwzt, CHARINDEX('_', @rwzt)+1, len(@rwzt))
SET @sql = 'SELECT @COUNT = COUNT(xx) from xxx WHERE delflg=''' + @flag+''''
SET @count = EXEC(@sql)
DECLARE @count int, @sql NVARCHAR(1000), @flag VARCHAR(20), @rwzt VARCHAR(2000)
SET @flag=SUBSTRING(@rwzt, CHARINDEX('_', @rwzt)+1, len(@rwzt))
SET @sql = N'SELECT @COUNT = COUNT(xx) from xxx WHERE delflg=@flag'
EXEC sp_executesql @sql, N'@COUNT INT OUTPUT, @flag varchar(20)', @count OUTPUT, @flag
Err] 42000 - [SQL Server]过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@statement'。
我想让这个存储过程执行时,不需要传参的。
DECLARE @count int, @sql NVARCHAR(1000), @flag VARCHAR(20), @rwzt VARCHAR(2000)
SET @flag=SUBSTRING(@rwzt, CHARINDEX('_', @rwzt)+1, len(@rwzt))
SET @flag = 'rpc'
SET @sql = N'SELECT @count = COUNT(number) from master..spt_values where name = @flag'
PRINT @sql
EXEC sp_executesql @sql, N'@flag varchar(20), @count INT OUTPUT', @flag, @count OUTPUT
IF(@count > 0)
SELECT @count
ELSE
SELECT 0
/*
(无列名)
1
*/
SET @sql = 'SELECT @COUNT = COUNT(xx) from xxx WHERE delflg=''' + @flag+'''' EXEC(@sql)
SELECT @count --这样就可以得到@count的值
declare @count int, @sql nvarchar(1000), @flag nvarchar(20)
set @flag=substring(@rwzt,charindex('_', @rwzt)+1,len(@rwzt))
set @sql=N'SELECT @C=COUNT(xx) from xxx WHERE delflg='''+@flag+N''' '
exec sp_executesql @sql,N'@C int output',@C=@count output
select @count