一次将数据保存到两个表中. [英] Saving Data into two table at a time.

查看:49
本文介绍了一次将数据保存到两个表中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我有两个数据库表

表A
StudID(通过设置身份自动生成),姓名,联系人等.

表B
ID StudID地址城市...



我要存储StudentName联系人等,然后首先获取StudID并将数据与检索到的StudID地址一起保存到B中,成为一个事务.

Hello Friends,

I Have Two Database Tables

Table A
StudID(it is Autogenerated by Setting Identity), Name, Contact, etc.

Table B
ID StudID Address City...



I want to store StudentName contact etc First then get the StudID and Save Data into B with retrived StudID Address into one Transaction.

推荐答案

1.插入TableA
INSERT INTO TableA(...)VALUES(...)

2.存储最后生成的ID
declare @StuID int; set @StuID=SCOPE_IDENTITY();请参见 SCOPE_IDENTITY() [
1. Insert into TableA
INSERT INTO TableA(...)VALUES(...)

2. Store last generated id
declare @StuID int; set @StuID=SCOPE_IDENTITY(); See SCOPE_IDENTITY()[^]

3 Insert into TableB
INSERT INTO TableB(StuID,...)VALUES(@StuID,...)


首次插入后,您必须使用SCOPE_IDENTITY().
after first insert you must use of SCOPE_IDENTITY().


只需在A上的表上使用after触发器,就必须重新绑定StudID,并仅在该触发器中插入表B.
Just use a after trigger on table on A, You have to retieve StudID, and insert into Table B in that trigger only.


这篇关于一次将数据保存到两个表中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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