SQLServer动态exec查询语句,返回查询变量
例:createproceduretempProasdeclare@sqlnvarchar(1000),@usernvarchar(500),@tablenamenvar...
例:
create procedure tempPro
as
declare @sql nvarchar(1000),@user nvarchar(500),@tablename nvarchar(100)
set @tablename='T_Table'
set @sql='select '+@user+' =[TwoField] from '+@tablename+' where [OneField]=''hello'
exec sp_executesql @sql, @user OUTPUT
print @user
这样获取不到@user查询后的值,
请问如何获得@user的值呢? 展开
create procedure tempPro
as
declare @sql nvarchar(1000),@user nvarchar(500),@tablename nvarchar(100)
set @tablename='T_Table'
set @sql='select '+@user+' =[TwoField] from '+@tablename+' where [OneField]=''hello'
exec sp_executesql @sql, @user OUTPUT
print @user
这样获取不到@user查询后的值,
请问如何获得@user的值呢? 展开
1个回答
展开全部
drop procedure tempPro;
go
create procedure tempPro
as
declare
@sql nvarchar(200),
@user nvarchar(500),
@tablename nvarchar(100)
begin
set @tablename='t_stars'
set @sql=N'select @userOUT = stars_name from '+@tablename+' where stars_id=''hk1006''';
execute sp_executesql @sql, N'@userOUT varchar(30) output', @userOUT=@user output;
print @user;
end
go
exec tempPro;
go
create procedure tempPro
as
declare
@sql nvarchar(200),
@user nvarchar(500),
@tablename nvarchar(100)
begin
set @tablename='t_stars'
set @sql=N'select @userOUT = stars_name from '+@tablename+' where stars_id=''hk1006''';
execute sp_executesql @sql, N'@userOUT varchar(30) output', @userOUT=@user output;
print @user;
end
go
exec tempPro;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询