Results 1 to 2 of 2

Thread: Please HELP!! SQL Trigger

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    9

    Exclamation Please HELP!! SQL Trigger

    Hey all,
    I would like to shorten up the coding of the trigger below. The trigger is tracing the transaction done on table "smummd009" and insert it into table "smatth002".

    Code:
    create trigger InsUpDel_smummd009 on smummd009 for insert, update, delete
    as
    begin
    if exists(select * from deleted)
    	begin
    	if exists(select * from inserted)
    		begin
    			if update(smnm)
    			begin
    				insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval, oval)
    				select 'Update', user, 'smummd009', 'smnm', 'mocd,smcd,lodt', 
    				rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), smnm, 
    				(select smnm from deleted where deleted.mocd = inserted.mocd 
    and deleted.smcd = inserted.smcd and deleted.lodt = inserted.lodt)
    				from inserted
    			end
    			if update(lofl)
    			begin
    				insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval, oval)
    				select 'Update', user, 'smummd009', 'lofl', 'mocd,smcd,lodt', 
    				rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), lofl, 
    				(select lofl from deleted where deleted.mocd = inserted.mocd 
    and deleted.smcd = inserted.smcd and deleted.lodt = inserted.lodt)
    				from inserted
    			end
    		end
    	else
    		
    		begin		
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, oval)
    			select 'Delete', user, 'smummd009', 'mocd', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), mocd 
    			from deleted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, oval)
    			select 'Delete', user, 'smummd009', 'smcd', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), smcd 
    			from deleted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, oval)
    			select 'Delete', user, 'smummd009', 'smnm', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), smnm 
    			from deleted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, oval)
    			select 'Delete', user, 'smummd009', 'lofl', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), lofl 
    			from deleted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, oval)
    			select 'Delete', user, 'smummd009', 'lodt', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), lodt 
    			from deleted
    		end
    	end
    else
    	begin
    		if update(mocd) or update(smcd) or update(lodt)
    		begin
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval)
    			select 'Insert', user, 'smummd009', 'mocd', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), mocd 
    			from inserted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval)
    			select 'Insert', user, 'smummd009', 'smcd', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), smcd 
    			from inserted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval)
    			select 'Insert', user, 'smummd009', 'smnm', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), smnm 
    			from inserted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval)
    			select 'Insert', user, 'smummd009', 'lofl', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), lofl 
    			from inserted
    			insert into smatth002(txty, usid, tbln, fldn, keyn, rcid, nval)
    			select 'Insert', user, 'smummd009', 'lodt', 'mocd,smcd,lodt', 
    			rtrim(mocd) + ',' + rtrim(smcd) + ',' + rtrim(lodt), lodt 
    			from inserted
    		end
    	end
    end
    Can someone please help. Any ideas would be appreciated.

    Thank You.


    Regards,
    PlaYb_G

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Check this out:
    http://www.vbforums.com/forumdisplay.php?s=&forumid=3
    It is for Database questions. Chit Chat isn't the place for this type of question, if you get help here you will be lucky, or you will get smart ass responses. The Database forum is where you will get the answers you seek.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width