Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

sql - 将存储过程的结果插入临时表(Insert results of a stored procedure into a temporary table)

How do I do a SELECT * INTO [temp table] FROM [stored procedure] ?

(如何SELECT * INTO [temp table] FROM [stored procedure]进行SELECT * INTO [temp table] FROM [stored procedure] ?)

Not FROM [Table] and without defining [temp table] ?

(不是FROM [Table] ,也没有定义[temp table] ?)

Select all data from BusinessLine into tmpBusLine works fine.

(从BusinessLine Select所有数据到tmpBusLine可以正常工作。)

select *
into tmpBusLine
from BusinessLine

I am trying the same, but using a stored procedure that returns data, is not quite the same.

(我正在尝试相同的方法,但是使用返回数据的stored procedure并不完全相同。)

select *
into tmpBusLine
from
exec getBusinessLineHistory '16 Mar 2009'

Output message:

(输出信息:)

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'exec'.

(消息156,级别15,状态1,第2行关键字“ exec”附近的语法错误。)

I have read several examples of creating a temporary table with the same structure as the output stored procedure, which works fine, but it would be nice to not supply any columns.

(我已经阅读了几个创建与输出存储过程具有相同结构的临时表的示例,该示例工作正常,但是最好不要提供任何列。)

  ask by Ferdeen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use OPENROWSET for this.

(您可以为此使用OPENROWSET 。)

Have a look.

(看一看。)

I've also included the sp_configure code to enable Ad Hoc Distributed Queries, in case it isn't already enabled.

(我还包括了sp_configure代码,以启用临时分布式查询(如果尚未启用)。)

CREATE PROC getBusinessLineHistory
AS
BEGIN
    SELECT * FROM sys.databases
END
GO

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local)SQL2008;Trusted_Connection=yes;',
     'EXEC getBusinessLineHistory')

SELECT * FROM #MyTempTable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...