查询条件作为参数并返回值的存储过程?
create procedure sp_aa
(
@where varchar(8000)= ' ',--条件
@sqlstr varchar(8000)=null,
@total varchar(50) output
)
AS
set @sqlstr= 'select '+@total+ '=count(id) from aa '+@where
exec(@sqlstr)
为什么这样返回的@total为空,表中有记录的
------解决方案--------------------create procedure sp_aa
(
@where varchar(8000)= ' ',--条件
@sqlstr nvarchar(4000)=null,
@total varchar(50) output
)
AS
set @sqlstr= 'select @total=count(id) from sysobjects '+@where
exec sp_executesql @sqlstr,N '@total varchar(50) output ',@total output
declare @total varchar(50)
exec sp_aa ' ',NULL,@total output
print @total