![](/img/trans.png)
[英]cannot access a data table located in another server from a stored procedure in one server of SQL server 2008 R2 management studio
[英]SQL Server 2008 R2: Return result from stored procedure and insert into temp table
我有一些表testing
一些數據:
表:
select * from testing;
cola colb
---------
A B
C D
C X
S T
S Q
存儲過程:
CREATE PROC spTesting
@TableName nvarchar(max)
AS
DECLARE @sql nvarchar(max)
SET @sql = 'SELECT * from '+ @TableName + ''
EXEC(@sql)
GO
執行存儲過程:
execute spTesting 'testing'
我會得到結果:
cola colb
---------
A B
C D
C X
S T
S Q
注意 :執行存儲過程后,我想將結果插入到temptable
,我不想在之前用結構聲明。
喜歡:
select * into temptable from execute spTesting 'testing'
插入到表/ temptable中的這種簡單語法是一個過程的結果集
INSERT INTO TempTable
EXEC MyProc
但是這種方法的問題是TempTable應該已經存在,然后才能執行上述操作。
調用存儲過程並將其結果集插入表時,無法像使用select語句那樣動態創建表select * INTO temptabe From tablename
。
您需要先創建此表,然后從存儲過程中插入它。
現在,因為一些表可以有50列並找到它們的DDL並創建表,然后才能執行該過程,有一種簡單的方法可以解決類似....
-- before you execute the procedure
SELECT * INTO TempTable FROM TargetTable WHERE 1 =2
-- this will create an empty table with matching schema of your TargetTable
-- Now execute the procedure
INSERT INTO TempTable
EXECUTE spTesting 'TargetTable'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.