exec语句后如何在临时表中存储值 [英] how to store value in temporary table after exec statement

查看:106
本文介绍了exec语句后如何在临时表中存储值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在exec语句后在临时表中存储值

我有一个,
exec(@sql)

@sql包含一条select语句.我想将结果存储到临时表中.但是它将存储在exec语句之后.

how to store value in temptable after exec statement

I have one,-
exec (@sql )

Here @sql contains a select statement. I want to store that result into a temp table. but it will store after the exec statement.

推荐答案


类似的事情,您必须创建临时表

Something like this you have to create the Temp Table

CREATE TABLE #LocalTempTable(
UserID int,
UserName varchar(50), 
UserAddress varchar(150))




并用于插入临时表




And for inserting in Temp table

insert into #LocalTempTable values ( 1, ''Abhijit'',''India'');



并用于



And for seleting

select * from #LocalTempTable


如果exec语句返回的列与为临时表指定的列相同,则可以执行以下操作.

插入@tempTable
exec(@sql)


完整示例:

Following will work if the columns returned by exec statement are same as the columns specified for temporary table.

insert into @tempTable
exec(@sql)


Complete example:

declare @tempTable table(userid int, userName varchar(50))

declare @sql varchar(1000)

set @sql = 'select 1, ''test user'''

insert into @tempTable
exec (@sql)

select * from @tempTable


看看下面类似的问答"讨论.

将动态SQL结果转换为SQL存储过程中的临时表 a>
Have a look at similar Question-Answer discussion below.

Dynamic SQL results into temp table in SQL Stored procedure


这篇关于exec语句后如何在临时表中存储值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆