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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Please Help me buddies as iam new to Sql Server

Author  Topic 

vivekhhh123
Starting Member

2 Posts

Posted - 2012-09-08 : 13:05:05
guys i want a code in sql my scenario is this.

Created a master table employee and another table called employee_loan
When a Employee is registered/inserted the the employee id must be inserted into employee_loan table and and generate an automatic loan id .I know that a trigger must be initiated,Can some one write the code and explain,please help me out,

vivek shankar

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-09 : 12:24:08
you can use OUTPUT clause for this


CREATE TABLE EMployee
(
EmployeeID int IDENTITY(1,1) NOT NULL,
EmloyeeName varchar(100),
...
)

CREATE TABLE Employee_Loan
(
EmployeeLoanID int IDENTITY(1,1) NOT NULL,
EmployeeID int,
....
)

DECLARE @INSERTED_EMPLOYEE Table
(
EmployeeID int
)

INSERT Employee(EmployeeName,...)
OUTPUT INSERTED.EmployeeName INTO @INSERTED_EMPLOYEE
VALUES ('some value',...)

INSERT EmployeeLoan
SELECT EmployeeID ,
<auto loan value>
FROM @INSERTED_EMPLOYEE
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vivekhhh123
Starting Member

2 Posts

Posted - 2012-09-10 : 01:08:22
thanks http://visakhm.blogspot.in/

vivek shankar
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-10 : 10:26:53
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -