Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hii.. all
i want to use trigger in sql server using asp .net c#.. my stored procedure update data in 2 tables using inner join.. i want to store this updated data in one table (i create a table (UPDATE_LOG)with both table parameters)..my table name is passed using like this for update in database tables

SQL
SET @ChildTblName = '[' + @DistrictID + 'Child_D' + ']'
SET @FamilyTblName = '[' + @DistrictID + 'Family_D' + ']'


how to use trigger in both tables that should store updated field in UPDATE_LOG and also update the data in main database tables..

my sp is updating the database tables correctly..bt i want to store updated data in (UPDATE_LOG) table..

plz help..

my trigger code is that use the UPDATE_LOG table

SQL
USE [SCJ]
GO
/****** Object:  Trigger [dbo].[UPDATEINFO_trggr]    Script Date: 03/19/2014 04:27:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UPDATEINFO_trggr] ON [dbo].[CHLDINFO_LOG]
FOR UPDATE
AS
declare @DistrictID VARCHAR(250),
     @barcode VARCHAR(50),  
     @distic Nvarchar(50),
     @block Nvarchar(50),
     @GP Nvarchar(50),
     @village Nvarchar(50),
     @habitation Nvarchar(50),
     @location Nvarchar(50),
     @residing Nvarchar(50),
     @mother_ton Nvarchar(50),
     @Pincode Nvarchar(50),
     @House_no Nvarchar(50),
     @Father Nvarchar(50),
     @Mother Nvarchar(50),
     @Guardian Nvarchar(50),
     @social_grp Nvarchar(50),
     @Minority Nvarchar(50),
     @Male Nvarchar(50),
     @Female Nvarchar(50),
     @seasnal_mgrtn Nvarchar(50),
     @Mgrtn_Type Nvarchar(50),
     @durtn_Mgrtn Nvarchar(50),
     @LP Nvarchar(50),
     @UP Nvarchar(50),
     @SEC Nvarchar(50),
     @Ctchmnt_LP Nvarchar(50),
     @Ctchmnt_UP Nvarchar(50),
     @Ctchmnt_SEC Nvarchar(50),
     @Chld_Name NVARCHAR(50),
     @SEX Nvarchar(50),
     @DOB Nvarchar(50),
     @Disblty_type Nvarchar(50),
     @Schl_CODE Nvarchar(50),
     @CLASS Nvarchar(50),
     @NEREASON Nvarchar(50),
     @DRPOUT_rsn Nvarchar(50),
     @HIGH_class Nvarchar(50),
     @LOG_Timestamp datetime

select @Mother=i.Mother from inserted i;
select @Chld_Name=i.Chld_Name from inserted i;

insert INTO  CHLDINFO_LOG (DistrictID
      ,barcode
      ,distic
      ,block
      ,GP
      ,village
      ,habitation
      ,location
      ,residing
      ,mother_ton
      ,Pincode
      ,House_no
      ,Father
      ,Mother
      ,Guardian
      ,social_grp
      ,Minority
      ,Male
      ,Female
      ,seasnal_mgrtn
      ,Mgrtn_Type
      ,durtn_Mgrtn
      ,LP
      ,UP
      ,SEC
      ,Ctchmnt_LP
      ,Ctchmnt_UP
      ,Ctchmnt_SEC
      ,Chld_Name
      ,SEX
      ,DOB
      ,Disblty_type
      ,Schl_CODE
      ,CLASS
      ,NEREASON
      ,DRPOUT_rsn
      ,HIGH_class
      ,LOG_Timestamp)
values (@DistrictID,
      @barcode
      ,@distic
      ,@block
      ,@GP
      ,@village
      ,@habitation
      ,@location
      ,@residing
      ,@mother_ton
      ,@Pincode
      ,@House_no
      ,@Father
      ,@Mother
      ,@Guardian
      ,@social_grp
      ,@Minority
      ,@Male
      ,@Female
      ,@seasnal_mgrtn
      ,@Mgrtn_Type
      ,@durtn_Mgrtn
      ,@LP
      ,@UP
      ,@SEC
      ,@Ctchmnt_LP
      ,@Ctchmnt_UP
      ,@Ctchmnt_SEC
      ,@Chld_Name
      ,@SEX
      ,@DOB
      ,@Disblty_type
      ,@Schl_CODE
      ,@CLASS
      ,@NEREASON
      ,@DRPOUT_rsn
      ,@HIGH_class
      ,getdate());
PRINT 'AFTER UPDATE trigger fired.'
Posted
Updated 19-Mar-14 1:20am
v2

1 solution

Hi,

First of all, you're saying that your stored procedure updates data in 2 tables using INNER JOIN. Actually, you can't update multiple tables in one statement. So you're updating one table with one UPDATE statement (maybe you're taking values from 2 tables using INNER JOIN?). After UPDATE operation, a trigger on that table fires up.

Secondly, when AFTER UPDATE trigger fires up, SQL Server stores records in two special tables: inserted and deleted. The inserted table stores copies of the affected rows after INSERT and UPDATE. The deleted table stores copies of the affected rows after UPDATE and DELETE. So records before UPDATE operation are stored in deleted table and records after UPDATE operation are stored in inserted table.

Thirdly, you're declaring a lot of parameters without assigning values to them. There is no point to do that.

Fourthly, you could create a stored procedure, which would insert records into UPDATE_LOG table. This procedure should be called from both triggers: one trigger on one table, other trigger on other table. This is because you can't create one trigger on multiple tables. You can pass values to the stored procedure from the inserted table.

Maybe my answer is not as you expected, but I want to explain how things work.

If you have some questions regarding my answer, please ask (use comments).
 
Share this answer
 
Comments
bindash 20-Mar-14 0:48am    
my stored procedure is passed table name like this

USE [SCJ]
GO
/****** Object: StoredProcedure [dbo].[spUPDATE_INFO] Script Date: 03/19/2014 22:15:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <author,,name>
-- Create date: <create date,,="">
-- Description: <description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spUPDATE_INFO]
-- Add the parameters for the stored procedure here
@DistrictID VARCHAR(250),
@barcode VARCHAR(50),
@distic Nvarchar(50)='',
@block Nvarchar(50)='',
@GP Nvarchar(50)='',
@village Nvarchar(50)='',
@habitation Nvarchar(50)='',
@location Nvarchar(50)='',
@residing Nvarchar(50)='',
@mother_ton Nvarchar(50)='',
@Pincode Nvarchar(50)='',
@House_no Nvarchar(50)='',
@Father Nvarchar(50)='',
@Mother Nvarchar(50)='',
@Guardian Nvarchar(50)='',
@social_grp Nvarchar(50)='',
@Minority Nvarchar(50)='',
@Male Nvarchar(50)='',
@Female Nvarchar(50)='',
@seasnal_mgrtn Nvarchar(50)='',
@Mgrtn_Type Nvarchar(50)='',
@durtn_Mgrtn Nvarchar(50)='',
@LP Nvarchar(50)='',
@UP Nvarchar(50)='',
@SEC Nvarchar(50)='',
@Ctchmnt_LP Nvarchar(50)='',
@Ctchmnt_UP Nvarchar(50)='',
@Ctchmnt_SEC Nvarchar(50)='',
@Chld_Name NVARCHAR(50),
@SEX Nvarchar(50)='',
@DOB Nvarchar(50),
@Disblty_type Nvarchar(50)='',
@Schl_CODE Nvarchar(50)='',
@CLASS Nvarchar(50)='',
@NEREASON Nvarchar(50)='',
@DRPOUT_rsn Nvarchar(50)='',
@HIGH_class Nvarchar(50)=''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE
@ChildTblName AS VARCHAR (125),
@FamilyTblName AS VARCHAR (125),
@vQuery1 AS VARCHAR (max),
@vQuery2 AS VARCHAR (max);


SET @ChildTblName = '[' + @DistrictID + 'Child_D' + ']'
SET @FamilyTblName = '[' + @DistrictID + 'Family_D' + ']'


SET @vQuery1 = 'update Child SET Child.cname=' + CHAR(39) +@Chld_Name+ CHAR(39) + ',Child.distt=' + CHAR(39) +@distic+ CHAR(39) + ',
Child.block=' + CHAR(39) +@block+ CHAR(39) + ',Child.gpmctc=' + CHAR(39) +@GP+ CHAR(39) + ',Child.village=' + CHAR(39) +@village+ CHAR(39) + ',
Child.habitation=' + CHAR(39) +@habitation+ CHAR(39) + ',Child.sex=' + CHAR(39) +@SEX+ CHAR(39) + ',Child.dob=' + CHAR(39) +@DOB+ CHAR(39) + ',
Child.location=' + CHAR(39) +@location+ CHAR(39) + ',
Child.residing=' + CHAR(39) +@residing+ CHAR(39) + ',Child.mother_ton=' + CHAR(39) +@mother_ton+ CHAR(39) + ',Child.disability=' + CHAR(39) +@Disblty_type+ CHAR(39) + ',
Child.sch_code=' + CHAR(39) +@Schl_CODE+ CHAR(39) + ',Child.class=' + CHAR(39) +@CLASS+ CHAR(39) + ',
Child.nereason=' + CHAR(39) +@NEREASON+ CHAR(39) + ',Child.droupout=' + CHAR(39) +@DRPOUT_rsn+ CHAR(39) + ',Child.highclass=' + CHAR(39) +@HIGH_class+ CHAR(39) + '
FROM ' + @ChildTblName + ' As Child WHERE Child.barcode=' + @barcode + ' AND Child.distt = ' + @DistrictID + '';


SET @vQuery2= 'update Family SET Family.distt=' + CHAR(39) +@distic+ CHAR(39) + ', Family.location=' + CHAR(39) +@location+ CHAR(39) + ',
Family.residing=' + CHAR(39) +@residing+ CHAR(39) + ',
Family.block=' + CHAR(39) +@block+ CHAR(39) + ',Family.gpmctc=' + CHAR(39) +@GP+ CHAR(39) + ',Family.village=' + CHAR(39) +@village+ CHAR(39) + ',
Family.habitation=' + CHAR(39) +@habitation+ CHAR(39) + ' ,Family.pincode=' + CHAR(39) +@Pincode+ CHAR(39) + ',Family.hhno=' + CHAR(39) +@House_no+ CHAR(39) + ',
Family.fname=' + CHAR(39) +@Father+ CHAR(39) + ', Family.mname=' + CHAR(39) +@Mother+ CHAR(39) + ',Family.gname=' + CHAR(39) +@Guardian+ CHAR(39) + ',
Family.social_grp=' + CHAR(39) +@social_grp+ CHAR(39) + ',Family.minority=' + CHAR(39) +@Minority+ CHAR(39) + ',Family.total_male=' + CHAR(39) +@Male+ CHAR(39) + ',
Family.total_fema=' + CHAR(39) +@Female
bindash 20-Mar-14 0:49am    
how to use these table names in trigger..
Andrius Leonavicius 20-Mar-14 7:07am    
You can't pass any parameters to the trigger directly because triggers fires up automatically. There are two ways to pass some information to the trigger indirectly:
1. Store the information in a table.
2. Use CONTEXT_INFO.

This link might be useful to you:
http://www.rahulsingla.com/blog/2010/11/sql-server-passing-parameters-to-triggers
bindash 20-Mar-14 7:17am    
i m store this data in a table..

an error is occured..

SET @vquery1='select Child.cname FROM ' + @ChildTblName + ' As Child INNER JOIN ' + @FamilyTblName + ' AS Family ON
Child.barcode=Family.barcode WHERE Child.distt = ' + @DistrictID + '';

insert into CHLDINFO_LOG(Chld_Name) values(@vquery1)
value is not inserted in table.. no error is occured
Andrius Leonavicius 20-Mar-14 7:33am    
Sorry, not enough information to help you with this error...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900