使用父表列id将数据插入子表 [英] Insert data into child table using parent table column id

查看:196
本文介绍了使用父表列id将数据插入子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我有两个表有父表和子表。我想在这些表中插入数据,首先我想在父表中插入,我想从父级获取Identity col值并使用此,我想在子表中插入详细信息。



这里我正在使用存储过程将数据插入表格



ex:



Hi all,


I have two tables there are parent and child table.and i want to insert data into those tables , firstly i want to insert in parent table and i want to get Identity col value from parent and using this, i want to insert details information into child table.

Here am using stored procedure for inserting data to tables

ex:

create table parent(id int primary key,name varchar(40),class varchar(40))

create table child(id int foreign key references parent(id), age int,village varchar(50))





谢谢和问候,



Dhineshkumar V



Thanks and Regards,

Dhineshkumar V

推荐答案

示例sql查询:

Example sql query:
DECLARE @pid INT --parent id, where id is identity for parent table

INSERT INTO parent ([name], [class])
VALUES('Dhinesh kumar.V', 'last')

SELECT @pid = @@IDENTITY

INSERT INTO parent (pid, age, village)
VALUES(@pid, 21, 'Best Village')



看看这里:

INSERT(T-SQL) [ ^ ]

@@IDENTITY [ ^ ]


这篇关于使用父表列id将数据插入子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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