且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从[EXEC SQL1 + SQL2 + SQL3]中将SELECT *导入[临时表]?不是来自[table]而没有定义[temp table]?

更新时间:2023-09-23 14:57:40

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



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- [ ^ ]