Thursday, September 15, 2011

TRIGGER WITH EX:

If I Inset Any Value In the Table it will Be Automatically Push to Another Table ,So Use This bellow steps…

 

CREATE TABLE [dbo].[Employee_Test](

      [Emp_ID] [int] IDENTITY(1,1) NOT NULL,

      [Emp_name] [varchar](100) NULL,

      [Emp_Sal] [decimal](10, 2) NULL

) ON [PRIMARY]

 

GO

 

INSERT A TABLE VALUE

 

INSERT INTO Employee_Test VALUES ('Anees',1000);

INSERT INTO Employee_Test VALUES ('Rick',1200);

INSERT INTO Employee_Test VALUES ('John',1100);

INSERT INTO Employee_Test VALUES ('Stephen',1300);

INSERT INTO Employee_Test VALUES ('Maria',1400);

 

 

Which Table U Want to Insert the content so Create a Table..

 

CREATE TABLE [dbo].[Employee_Test_Audit](

      [Emp_ID] [int] NULL,

      [Emp_name] [varchar](100) NULL,

      [Emp_Sal] [decimal](10, 2) NULL,

      [Audit_Action] [varchar](100) NULL,

      [Audit_Timestamp] [datetime] NULL

) ON [PRIMARY]

 

GO

 

This trigger is fired after an INSERT on the table. Let's create the trigger as:-

 

CREATE TRIGGER trgAfterInsert ON [dbo].[Employee_Test]

FOR INSERT

AS

      declare @empid int;

      declare @empname varchar(100);

      declare @empsal decimal(10,2);

      declare @audit_action varchar(100);

 

      select @empid=i.Emp_ID from inserted i;  

      select @empname=i.Emp_Name from inserted i;    

      select @empsal=i.Emp_Sal from inserted i;

      set @audit_action='Inserted Record -- After Insert Trigger.';

 

      insert into Employee_Test_Audit

           (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)

      values(@empid,@empname,@empsal,@audit_action,getdate());

 

      PRINT 'AFTER INSERT trigger fired.'

GO

 

 

 

AFTER UPDATE Trigger

This trigger is fired after an update on the table. Let's create the trigger as:-

 

CREATE TRIGGER trgAfterUpdate ON [dbo].[Employee_Test]

FOR UPDATE

AS

      declare @empid int;

      declare @empname varchar(100);

      declare @empsal decimal(10,2);

      declare @audit_action varchar(100);

 

      select @empid=i.Emp_ID from inserted i;  

      select @empname=i.Emp_Name from inserted i;    

      select @empsal=i.Emp_Sal from inserted i;

     

      if update(Emp_Name)

            set @audit_action='Updated Record -- After Update Trigger.';

      if update(Emp_Sal)

            set @audit_action='Updated Record -- After Update Trigger.';

 

      insert into Employee_Test_Audit(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)

      values(@empid,@empname,@empsal,@audit_action,getdate());

 

      PRINT 'AFTER UPDATE Trigger fired.'

GO

 

UPDATE THE TABLE:

 

 update Employee_Test set Emp_Sal=1550 where Emp_ID=6

  

THEN SELECT THE OUR REFERANCE TABLE:

 

 select * from Employee_Test_Audit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AFTER DELETE Trigger

This trigger is fired after a delete on the table. Let's create the trigger as:-

 

 

CREATE TRIGGER trgAfterDelete ON [dbo].[Employee_Test]

AFTER DELETE

AS

      declare @empid int;

      declare @empname varchar(100);

      declare @empsal decimal(10,2);

      declare @audit_action varchar(100);

 

      select @empid=d.Emp_ID from deleted d;   

      select @empname=d.Emp_Name from deleted d;     

      select @empsal=d.Emp_Sal from deleted d; 

      set @audit_action='Deleted -- After Delete Trigger.';

 

      insert into Employee_Test_Audit

(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)

      values(@empid,@empname,@empsal,@audit_action,getdate());

 

      PRINT 'AFTER DELETE TRIGGER fired.'

GO

 

DELETE THE SELECTED ROW

 

delete from [Employee_Test] where [Emp_ID]=1

 

 select * from Employee_Test_Audit

 



--
--
Regards,
Basheer Ahamed


Wednesday, September 14, 2011

Disable And Enable Trigger


Disable   And  Enable Trigger

Disable  the  Trigger

DISABLE TRIGGER TRIGGER_NAME ON WHICH_TABLE;

Enable the Trigger

ENABLE TRIGGER TRIGGER_NAME ON WHICH_TABLE;


--
--
Regards,
Basheer Ahamed
 


Get Week Count Of Year..

GET WEEK COUNT OF YEAR..

 

CREATE FUNCTION ISOweek  (@DATE DATETIME)

RETURNS INT

AS BEGIN  

 DECLARE @ISOweek INT 

  

SET @ISOweek = DATEPART(wk,@DA+1-DATEPART(wk,CAST(DATEPART(yy,@DATE) AS CHAR(4))+'0104')   

--Special cases: Jan 1-3 may belong to the previous year  

    IF (@ISOweek=0)      

    SET @ISOweek = dbo.ISOweek(CAST(DATEPART(yy,@DATE) - 1    AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1   

    -- Special case: Dec 29-31 may belong to the next year 

      IF ((DATEPART(mm,@DATE)=12) AND       ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))      

      SET @ISOweek=1    RETURN(@ISOweek) END

      GO

     

     

RESULT

      select dbo.ISOweek(GETDATE())



--
--
Regards,
Basheer Ahamed