SQL Server How to assign set @result into another SQL query inside stored procedure

bfhwhh0e  于 2023-04-10  发布在  其他
关注(0)|答案(2)|浏览(141)

I have return stored procedure

create procedure t1
    Declare @tablename varchar(1000)
As
Begin
    Declare @result varchar(50), @t1 varchar(60)
    Set @result = 'select * from' + @tablename

    Exec(@result)

    set @t1 = (select * into #temp from @result)

I am stuck how to pass @result variable to @t1 inside the stored procedure.

I wanted to pass @result output to another SQL query

0tdrvxhp

0tdrvxhp1#

Perhaps the following will help with what you are trying to do.

Create your temp table first then insert into it as part of your dynamic statement.

If you create the temp table within the dynamic SQL it won't be accessible outside of its execution scope.

Declare @result nvarchar(max), @tablename sysname = N'MyTable';

Set @result = Concat(N'insert into #temp <columns> select <columns> from ', QuoteName('schema eg dbo'), '.', QuoteName(@tablename));

Exec(@result);
r3i60tvu

r3i60tvu2#

The temp table structure is not fixed it will vary as per different tablename.wanted to create temp table dynamically

相关问题