【问题标题】:How to copy exactly same schema of one table to another table using stored procedure如何使用存储过程将一个表的完全相同的模式复制到另一个表
【发布时间】:2016-06-17 18:30:16
【问题描述】:

最初,我将使用存储过程 (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

【问题讨论】:

  • 请不要使用图片。而是将您的代码复制并粘贴到问题中。

标签: sql-server stored-procedures schema


【解决方案1】:

如果您已经有目标表:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多