Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-09-20 : 21:56:37
|
| Guys,I have scenario where trigger on a table should insert in multiple child tables, but the parent and child tables are referenced by identity ID columns.I am unable to capture the Parent ID columns and inserting into child tables.In the below example all the tables have primary keys with autogenerated property and foreign keys refer to these auto generated primary keys. In the 2nd and 3rd insert statements of stored procedure P_EMPLOYEE_INSERT below how can parentid which have identity property be passed. I cannot use below select statement to capture ParentIDs, since there are cases where name and dob are duplicated and do not want to use select statements before each insert.select empid from employee where name = @name and dob = @dob Is there anyway to capture identity ParentID.Any suggestions/inputs would help.ThanksCREATE TABLE PERSON PERSONID INT IDENTITY(1, 1) NOT NULL, (primary key)PARTYID BIGINT,NAME VARCHAR(200),DATE_TIME_CREATED DATETIME NULL,DATE_TIME_MOD DATETIME NULL)CREATE TABLE EMPLOYEE (EMPID INT IDENTITY (1, 1) NOT NULL, (primary key)PERSONID INT NOT NULL, (Foreign key constraint with PERSON.PERSONID),STARTDATE DATETIME)CREATE TABLE EMPLOYEEHIST (EMPHISTID INT IDENTITY (1, 1) NOT NULL, (primary key)EMPID INT NOT NULL, (Foreign key constraint with EMPLOYEE.EMPID),STATUSDT DATETIME,STATUS VARCHAR(200))create procedure [dbo].[P_EMP_INSERT](@PARTYID BIGINT, @NAME VARCHAR(200), @DOB DATETIME) asbeginbegin transaction insert into PERSON (PARTYID, NAME, DOB, DATE_TIME_CREATED, DATE_TIME_MOD) values(@PARTYID, @NAME, @DOB, GETDATE(), GETDATE()) insert into EMPLOYEE (PERSONID, STARTDATE) values(????, getdate()) insert into EMPLOYEEHIST (EMPID, STATUSDT, STATUS) values (????, getdate(), 'open')commit end |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-09-20 : 22:01:09
|
| Use the Scope_Identity() function to retrieve the Identity value created from the last INSERT.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-21 : 01:37:16
|
quote: Originally posted by amsqlguy Guys,I have scenario where trigger on a table should insert in multiple child tables, but the parent and child tables are referenced by identity ID columns.I am unable to capture the Parent ID columns and inserting into child tables.In the below example all the tables have primary keys with autogenerated property and foreign keys refer to these auto generated primary keys. In the 2nd and 3rd insert statements of stored procedure P_EMPLOYEE_INSERT below how can parentid which have identity property be passed. I cannot use below select statement to capture ParentIDs, since there are cases where name and dob are duplicated and do not want to use select statements before each insert.select empid from employee where name = @name and dob = @dob Is there anyway to capture identity ParentID.Any suggestions/inputs would help.ThanksCREATE TABLE PERSON PERSONID INT IDENTITY(1, 1) NOT NULL, (primary key)PARTYID BIGINT,NAME VARCHAR(200),DATE_TIME_CREATED DATETIME NULL,DATE_TIME_MOD DATETIME NULL)CREATE TABLE EMPLOYEE (EMPID INT IDENTITY (1, 1) NOT NULL, (primary key)PERSONID INT NOT NULL, (Foreign key constraint with PERSON.PERSONID),STARTDATE DATETIME)CREATE TABLE EMPLOYEEHIST (EMPHISTID INT IDENTITY (1, 1) NOT NULL, (primary key)EMPID INT NOT NULL, (Foreign key constraint with EMPLOYEE.EMPID),STATUSDT DATETIME,STATUS VARCHAR(200))create procedure [dbo].[P_EMP_INSERT](@PARTYID BIGINT, @NAME VARCHAR(200), @DOB DATETIME) asbeginDECLARE @Person_ID int,@Emp_ID intbegin transaction insert into PERSON (PARTYID, NAME, DOB, DATE_TIME_CREATED, DATE_TIME_MOD) values(@PARTYID, @NAME, @DOB, GETDATE(), GETDATE())SET @Person_ID=SCOPE_IDENTITY() insert into EMPLOYEE (PERSONID, STARTDATE) values(@Person_ID, getdate())SET @Emp_ID = SOPE_IDENTITY() insert into EMPLOYEEHIST (EMPID, STATUSDT, STATUS) values (@Emp_ID, getdate(), 'open')commit end
just modify like above |
 |
|
|
|
|
|
|
|