如何从[EXEC SQL1 + SQL2 + SQL3]中将SELECT *导入[临时表]?不是来自[table]而没有定义[temp table]? [英] How do I do a SELECT * into [temp table] from [EXEC SQL1+SQL2+SQL3]? not from [table] and without defining [temp table]?

查看:98
本文介绍了如何从[EXEC SQL1 + SQL2 + SQL3]中将SELECT *导入[临时表]?不是来自[table]而没有定义[temp table]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,





如何进行 SELECT * INTO [临时表] FROM [ EXEC SQL1 + SQL2 + SQL3] ?不是 FROM [Table] 而没有定义 [临时表]



以下是我的语法: EXEC @ SQL1 + @ SQL2 + @ SQL3



i想要插入此结果集到临时表。



请帮帮我。



提前致谢。



我尝试了什么:



i尝试了两种方法,但它没有用。

1)

Hi Friends,


How do I do a SELECT * INTO [temp table] FROM [EXEC SQL1+SQL2+SQL3]? Not FROM [Table] and without defining [temp table]?

Following is my syntax: EXEC @SQL1+@SQL2+@SQL3

i want to insert this resultset into temp table.

Please help me.

Thanks in advance.

What I have tried:

i have tried following two methods but it didn't work.
1)

insert into #temptable exec(@SQL1+@SQL2+@SQL3)
select * from #temptable



2)


2)

SELECT
  *
INTO
  #temptable
FROM
  OPENROWSET(
    'SQLNCLI',
    'Server=(local)\SQL2008;Trusted_Connection=yes;',
    'EXEC ' + @SQL1 + '+' + @SQL2 +',' +
)

推荐答案

这是可行的代码......



Here is the workable code...

--prepare table
create table t1(code nvarchar(10), name nvarchar(64));
go
insert into t1 values('001', 'Jon Doe');
insert into t1 values('002', 'Michael Doe');
go
--prepare stored procedure
create procedure getdata
as
begin
	--functional stuff
	select * from t1;
end





执行sp和store的实际代码临时表





actual code to execute sp and store in temp table

CREATE TABLE #temp
(
   code nvarchar(10),
   name nvarchar(64)
)

INSERT INTO #temp
Exec getdata


尝试使用以下代码

Try with below code
SELECT  *
INTO    #tempTable
FROM    OPENQUERY(YOURSERVERNAME, 'EXEC exec(@SQL1+@SQL2+@SQL3)')





它需要sqlserver的额外权限并格式化查询字符串。

hint- [ ^ ]


这篇关于如何从[EXEC SQL1 + SQL2 + SQL3]中将SELECT *导入[临时表]?不是来自[table]而没有定义[temp table]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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