Làm sao insert kết quả của một stored procedure vào một temporary table?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
330 lượt xem
Sql Server master

Làm sao ta có thể thực hiện

SELECT * INTO [temp table] FROM [stored procedure]

mà không cần định nghĩa [temp table]

Giải pháp:

Bạn có thể sử dụng OPENROWSET cho việc này. Chúng ta cũng đã bao gồm mã sp_configure để bật Ad Hoc Distributed Queries, trong trường hợp nó chưa được bật.

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
{{login.error}}