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 |
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 04:47:09
|
I have a insert trigger which validates the inserted data and passes the errors to text file.The problem is that trigger works fine for good data but if there is some wrong data than the error is not being captured by the code instead goes into infinite loop I used the following code for testing.first time I insert it then it works but if i insert it again it is supposed to show an error in text file instead it gets into infinite loop.The actual insert is done using SSIS package on the table.insert into clientcode(cc_clientcode,cc_rmcode,cc_productname,cc_panno)values(13213,20003,'Equity','AAGPW6964M') If I pass some wrong value to any of the fields than it gets into infinite loop instead of passing the error into text file.USE [Religare]GO/****** Object: Trigger [dbo].[clientpanno] Script Date: 10/17/2008 12:00:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER trigger [dbo].[clientpanno] on [dbo].[ClientCode] for insert AsBegin Delete from sql_identity where id_tableid=10183 Declare @panno as nvarchar(10),@panno1 as nvarchar(10) Declare @rmcode as nvarchar(10),@rmcode1 as nvarchar(10),@clientcode as nvarchar(10),@top as nvarchar(10) Declare @activeid as varchar(10) Declare @ccid as int,@count as int /*Create Table ClientCodeError (SerialNo int Identity(1,1), Remark nchar(50) not null, ClientCode nchar(20) Not Null, RMCode nchar(20) not null, PANNo nchar(20) not null, Product nchar(20) not null)*/ Declare @InvalidRMCodematch as nvarchar(50),@InvalidRMCode as nvarchar(50),@InvalidPANNo as nvarchar(50),@DuplicateClientCode as nvarchar(50) Set @InvalidRMCodematch='Clientcode mapped to wrong RMCode' Set @InvalidRMCode='RMCode does not exist in the CRM' Set @InvalidPANNo='PAN No. does not exist in the CRM' Set @DuplicateClientCode='This Client Code already exists' Select @ccid=cc_clientcodeid from clientcode order by cc_clientcodeid Declare curClientCode SCROLL cursor For Select cc_panno,cc_rmcode from inserted where cc_activeid is null Order By cc_clientcodeid Open curClientCode Fetch curClientCode into @panno,@rmcode While(@@fetch_status=0) Begin Select @activeid=client_activeid from client where client_panno=@panno Select @rmcode1=user_empcode from users where user_empcode=@rmcode Select @panno1=client_panno from client where client_userid=(Select user_userid from users where user_empcode=@rmcode) select @count=count(cc_clientcode) from clientcode where cc_panno=@panno select @top=cc_clientcodeid from clientcode where cc_panno=@panno order by cc_clientcodeid desc if(@activeid is null) Begin Print 'The PAN No does not exist' Insert into ClientCodeError Select @InvalidPANNo,cc_clientcode,cc_rmcode,cc_panno,cc_productname from clientcode where cc_panno=@panno delete from clientcode where cc_panno=@panno End Else if(@rmcode1 is null) Begin Print 'The RMCode does not exist in CRM' Insert into ClientCodeError Select @InvalidRMCode,cc_clientcode,cc_rmcode,cc_panno,cc_productname from clientcode where cc_panno=@panno delete from clientcode where cc_panno=@panno End Else if(@panno1 != @panno) Begin Print 'Clientcode mapped to wrong RMCode' Insert into ClientCodeError Select @InvalidRMCodematch,cc_clientcode,cc_rmcode,cc_panno,cc_productname from clientcode where cc_panno=@panno delete from clientcode where cc_panno=@panno End Else if(@count>1) Begin Print 'This Client Code already exists' Insert into ClientCodeError Select @DuplicateClientCode,cc_clientcode,cc_rmcode,cc_panno,cc_productname from clientcode where cc_panno=@panno delete from clientcode where cc_clientcodeid=@top End Else Begin Update clientcode set cc_activeid=@activeid where cc_panno=@panno End Fetch curClientCode into @panno,@rmcode Print 'The Pan No. is '+@panno Print 'The RMCode is '+@rmcode End Close curClientCode Deallocate curClientCode Select @ccid=cc_clientcodeid from clientcode order by cc_clientcodeid Set @ccid=@ccid+1 Insert sql_identity (id_tableid,id_nextid)values(10183,@ccid) Declare @bcpCommand varchar(100) SET @bcpCommand='bcp "SELECT * FROM Religare..ClientCodeError" queryout "c:\ClientCodeError.txt" -U sa -P SA1 -c' EXEC master..xp_cmdshell @bcpCommand Delete from ClientCodeErrorEnd |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-17 : 04:53:53
|
| why are you using cursor in trigger? can you explain what you're trying to do in trigger code? it would be better to rewrite logic using set based methods. |
 |
|
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 04:57:13
|
| I am using trigger because I am using SSIS package to insert records into the table.Since the SSIS uses bulk insert I can cannot validate individual record .So the cursor holds the result set and code goes through every record individually and that way every record can be validated. |
 |
|
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 05:06:47
|
| I am using Cusror because I am using SSIS package to insert records into the table.Since the SSIS uses bulk insert I can cannot validate individual record .So the cursor holds the result set and code goes through every record individually and that way every record can be validated. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-17 : 05:07:29
|
Try something like this instead...ALTER TRIGGER dbo.trgClientPannoON dbo.ClientCodeFOR INSERT ASSET NOCOUNT ON-- PAN No. does not exist in the CRMDELETE ccOUTPUT 'PAN No. does not exist in the CRM', cc.cc_clientcode, cc.cc_rmcode, cc.cc_panno, cc.cc_productnameINTO ClientCodeErrorFROM ClientCode AS ccINNER JOIN inserted AS i ON i.cc_panno = cc.cc_pannoLEFT JOIN Client AS c ON c.client_panno = cc.cc_pannoWHERE c.client_panno IS NULL E 12°55'05.63"N 56°04'39.26" |
 |
|
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 05:49:12
|
| Will DELETE ccdelete the unwanted rows by itself or do I need to explicitly delete the rows |
 |
|
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 07:04:45
|
| I tried this by first uploading all the records after disabling the trigger then used this codeDELETE ccOUTPUT 'PAN No. does not exist in the CRM', cc.cc_clientcode, cc.cc_rmcode, cc.cc_panno, cc.cc_productnameINTO ClientCodeErrorFROM ClientCode AS ccLEFT JOIN Client AS c ON c.client_panno = cc.cc_pannoWHERE c.client_panno IS NULLit gave me this errorMsg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_clientcode" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_rmcode" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_panno" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_productname" could not be bound. |
 |
|
|
nithin.gujjar
Starting Member
19 Posts |
Posted - 2008-10-17 : 07:06:24
|
quote: Originally posted by Peso Try something like this instead...ALTER TRIGGER dbo.trgClientPannoON dbo.ClientCodeFOR INSERT ASSET NOCOUNT ON-- PAN No. does not exist in the CRMDELETE ccOUTPUT 'PAN No. does not exist in the CRM', cc.cc_clientcode, cc.cc_rmcode, cc.cc_panno, cc.cc_productnameINTO ClientCodeErrorFROM ClientCode AS ccINNER JOIN inserted AS i ON i.cc_panno = cc.cc_pannoLEFT JOIN Client AS c ON c.client_panno = cc.cc_pannoWHERE c.client_panno IS NULL E 12°55'05.63"N 56°04'39.26"
I tried this by first uploading all the records after disabling the trigger then used this codeDELETE ccOUTPUT 'PAN No. does not exist in the CRM',cc.cc_clientcode,cc.cc_rmcode,cc.cc_panno,cc.cc_productnameINTO ClientCodeErrorFROM ClientCode AS ccLEFT JOIN Client AS c ON c.client_panno = cc.cc_pannoWHERE c.client_panno IS NULLit gave me this errorMsg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_clientcode" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_rmcode" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_panno" could not be bound.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "cc.cc_productname" could not be bound. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-17 : 08:36:53
|
you can return only columns from INSERTED & UPDATED using OUTPUT. it should beDELETE ccOUTPUT DELETED.cc_clientcode,DELETED.cc_rmcode,DELETED.cc_panno,DELETED.cc_productnameINTO ClientCodeErrorFROM ClientCode AS ccLEFT JOIN Client AS c ON c.client_panno = cc.cc_pannoWHERE c.client_panno IS NULL |
 |
|
|
|
|
|
|
|