如何存储从 sp_executesql 返回的多个值或值列表?

发布于 2024-09-17 22:33:15 字数 1358 浏览 6 评论 0原文

更新:这就是我所做的 -

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-09-24 22:33:15

通过临时表。

[更新]

declare @sql nvarchar(max)
  set @sql = '
select 
    E.ID as EmployeeID, --doesn't really matter how you name them, it's the order that matters
    E.UserName as Username -- and this order should match the order of columns in the insert statement
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
'
DECLARE @LeaderBoard TABLE
( 
    EmployeeId int, 
    UserName varchar(50)
) 
INSERT INTO @LeaderBoard (EmployeeId, UserName)
exec sp_executesql @sql

select * from @LeaderBoard

--Hurray, we made it!

Via a temp table.

[Update]

declare @sql nvarchar(max)
  set @sql = '
select 
    E.ID as EmployeeID, --doesn't really matter how you name them, it's the order that matters
    E.UserName as Username -- and this order should match the order of columns in the insert statement
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
'
DECLARE @LeaderBoard TABLE
( 
    EmployeeId int, 
    UserName varchar(50)
) 
INSERT INTO @LeaderBoard (EmployeeId, UserName)
exec sp_executesql @sql

select * from @LeaderBoard

--Hurray, we made it!
九厘米的零° 2024-09-24 22:33:15

您可以将 EXEC sp_executesql 的结果插入到临时表或表变量中。

DECLARE  @t TABLE
(
a INT,
b INT
)
INSERT INTO @t (a,b)
EXEC sp_executesql N'SELECT 1, 2 UNION SELECT 3, 4 '

或者,动态 SQL 可以访问在父作用域中声明的临时表(但在动态 SQL 本身中创建的任何临时表在执行完成后都将超出作用域)

You can insert the results of EXEC sp_executesql into a temp table or table variable.

DECLARE  @t TABLE
(
a INT,
b INT
)
INSERT INTO @t (a,b)
EXEC sp_executesql N'SELECT 1, 2 UNION SELECT 3, 4 '

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文