如何存储从 sp_executesql 返回的多个值或值列表?
更新:这就是我所做的
-
set @dyn_sql = '
select
@UserName=UserName
from
(
select
E.ID as EmployeeID,
E.UserName as Username
from
Leaderboard K
inner join Employee E on K.EmployeeId = E.Id
inner join INFO KD on KD.EmployeeId=E.Id
where E.CompanyId=4
) as d1'
DECLARE @leaderboards TABLE
(
UserName varchar(50)
)
set @params='@Employee_Id int, @UserName varchar(200) OUTPUT'
INSERT INTO @leaderboards (UserName)
EXEC sp_executesql @dyn_sql, @params,@EmployeeId=@Employee_Id OUTPUT,@UserName = @User_Name OUTPUT
SELECT * from @leaderboards
但这并没有返回记录,尽管如果我看到查询是正确的并返回记录..
大家好,我正在执行动态sql使用 sp_executesql 语句,这就是我当前正在做的事情 -
EXEC sp_executesql @dyn_sql, @params,@EmployeeId=@Employee_Id OUTPUT,@UserName = @User_Name OUTPUT
SELECT @Employee_Id AS EmployeeId,@User_Name AS UserName
但是,如果我单独运行动态 sql 查询,当我获取记录列表时,上面只会得到单个值。如何存储执行我的操作返回的值列表动态SQL? 并返回记录..并返回记录..但是这个
UPDATE : This is what I did
-
set @dyn_sql = '
select
@UserName=UserName
from
(
select
E.ID as EmployeeID,
E.UserName as Username
from
Leaderboard K
inner join Employee E on K.EmployeeId = E.Id
inner join INFO KD on KD.EmployeeId=E.Id
where E.CompanyId=4
) as d1'
DECLARE @leaderboards TABLE
(
UserName varchar(50)
)
set @params='@Employee_Id int, @UserName varchar(200) OUTPUT'
INSERT INTO @leaderboards (UserName)
EXEC sp_executesql @dyn_sql, @params,@EmployeeId=@Employee_Id OUTPUT,@UserName = @User_Name OUTPUT
SELECT * from @leaderboards
But this is not returning records although if I see the query is right and returns records..
Hi all, I am executing a dynamic sql statement using sp_executesql and this is what I am doing currently -
EXEC sp_executesql @dyn_sql, @params,@EmployeeId=@Employee_Id OUTPUT,@UserName = @User_Name OUTPUT
SELECT @Employee_Id AS EmployeeId,@User_Name AS UserName
But the above only gets me single value when I am getting a list of records if I run the dynamic sql query individually.How can I store the list of values returned by execution of my dynamic sql ?
and returns records..and returns records..But this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过临时表。
[更新]
Via a temp table.
[Update]
您可以将 EXEC sp_executesql 的结果插入到临时表或表变量中。
或者,动态 SQL 可以访问在父作用域中声明的临时表(但在动态 SQL 本身中创建的任何临时表在执行完成后都将超出作用域)
You can insert the results of
EXEC sp_executesql
into a temp table or table variable.Or alternatively the dynamic SQL can access a temp table declared in the parent scope (but any temp table created in the dynamic SQL itself will be out of scope as soon as the execution finishes)