2016-03-04 20 views
0

最初,我將使用存儲過程(SP1)將源表的所有列存儲到單個變量。此SP1在SP2內部調用,並且該變量被插入到目標表中。如何使用存儲過程將完全相同的一個表的模式複製到另一個表

我知道這種方法,但堅持實施它。

-- SP1 
Create procedure insert_data 
AS 
Declare @data nvarchar(60) 
Begin 
EXEC get 
--Here i have to insert 
END 
Go 

--SP2

Create procedure get_column 

AS 

Declare 
    @e_id int , @e_name nvarchar(20) , @d_id int,@res nvarchar(50) 

Begin 

    SET @e_id =(select Emp_ID from Dim_Employee where Emp_Name='Cathy'); 

    SET @e_name =(select Emp_Name from Dim_Employee where emp_id=101); 

    SET @d_id =(select Dept_ID from Dim_Employee where emp_name='Cathy'); 

    SET @res ='@e_id , @e_name , @d_id'; 

    return @res 

END 
Go 
+0

請使用圖片避免。改爲在問題中複製並粘貼代碼。 –

回答

0

如果你已經有了目標表:

insert into DestTbl(emp_id, dept_id, ...) 
select emp_id, dept_id, ... 
from Dim_Employee d 
where ... 

如果你不併希望創建它

select emp_id, dept_id, ... 
into DestTbl 
from Dim_Employee d 
where ... 

如果你仍然想使用變量

insert into DestTbl(emp_id, dept_id) 
values (@emp_id, @dept_id, ...) 

這是你要求的嗎? MSDN上

INSERT語句: https://technet.microsoft.com/en-us/library/ms174335.aspx

+0

它幫助..感謝@Ivan Starostin – Sans

相關問題