Sunday, July 13, 2014

How to Insert data using after trigger... SubString


create database mydata121
 CREATE TABLE CONCATE
(
Empid varchar(50)
,Ename varchar(50)
,Salary money,
associatID numeric
)

insert into CONCATE(Empid,Ename,Salary)
values('101','Ashok',25000)
insert into CONCATE(Empid,Ename,Salary)
values('202','shaarth',60000)

select * from CONCATE

select SUBSTRING(empid,1,1)+'_'+SUBSTRING(ename,1,1)+'_'+
SUBSTRING(cast(Salary as varchar(50)),1,2) as Associate,
Empid,Ename,Salary from CONCATE



CREATE TRIGGER AFTERINSERT ON CONCATE AFTER INSERT
AS
BEGIN
DECLARE @EMPID VARCHAR(50)
DECLARE @ENAME VARCHAR(50)
DECLARE @SALARY VARCHAR(50)


SELECT @EMPID= i.empid FROM inserted i
select @ENAME=i.ename from inserted i
select @SALARY=i.Salary from inserted i


DECLARE @SUBSTRING VARCHAR(50)=SUBSTRING(@EMPID,1,1)+'_'+SUBSTRING(@ENAME,1,1)+'_'+
SUBSTRING(cast(@SALARY as varchar(50)),1,2)


update concate set associatID=@SUBSTRING where empid=@EMPID


END

GO