如何根据以前的插入内容插入记录? [英] How to Insert Records based on the Previous Insert?

查看:93
本文介绍了如何根据以前的插入内容插入记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序:

PROCEDURE add_values
AS
BEGIN

   INSERT INTO TABLE_A ...
   SELECT t.id, t.name FROM TABLE_C ("This selection will return multiple records")

END

当它插入到TableA中时,我想为插入到tableA中的特定记录插入另一个表(TableB)中.

While it inserts in TableA, I would like insert into another table(TableB) for that particular record which got inserted in tableA.

TableA和TableB中的列不同.在插入到TableB中之前调用一个函数是否明智-我想根据插入到tableA中的ID进行某些获取和设置?

The columns in TableA and TableB are different. Is it wise to call a function before inserting into TableB - I would like to perform certain gets and sets based on the id inserted in tableA?

推荐答案

尝试一下

输入

declare @tblA table (id int,name varchar(20))
declare @tblB table (id int,name varchar(20))
declare @tblC table (id int identity,name varchar(20))
insert into @tblC 
    select 'name1' union all select 'name2' union all
    select 'name3' union all select 'name4' union all
    select 'name5' union all select 'name6' union all
    select 'name7' union all select 'name8' union all
    select 'name9' union all select 'name10' union all
    select 'name11' union all select 'name12' union all
    select 'name13' union all select 'name14' union all
    select 'name15' union all select 'name16' union all
    select 'name17' union all select 'name18' union all
    select 'name19' union all select 'name20' 

查询

insert @tblA 
output INSERTED.id, INSERTED.Name
into @tblB 
select 
    id,name
from @tblC 
where id % 2 = 0

select * from @tblA
select * from @tblB

输出:[对于表A& B]

Output: [ For both table A & B]

id名称

2   name2
4   name4
6   name6
8   name8
10  name10
12  name12
14  name14
16  name16
18  name18
20  name20

基本上,我将这些记录从ID为偶数的TableC插入到TableA中.然后使用Output子句将TableA中的值插入TableB中

Basically I am inserting those records into TableA from TableC whose id's are even. And then by using Output clause inserting the values from TableA to TableB

有关更多信息,输出子句

希望这很有意义

这篇关于如何根据以前的插入内容插入记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆