| Author |
Topic |
|
wonder_gal
Starting Member
4 Posts |
Posted - 2007-03-08 : 01:29:17
|
Hi, wanna ask something about trigger here.I have 2 tables, namely GW_Account and Customer_Profile. I'm writing a trigger so that when there is any insertion into table GW_Account, then the trigger will insert one new record into table Customer_Profile, provided that there's no such record exists in table Customer_Profile yet.My trigger is written asCREATE TRIGGER [tr_gwAccount_INSERT]ON [dbo].[GW_Account]FOR INSERTASDECLARE @variable VARCHAR(100) Select @variable = (SELECT Company_Name FROM inserted)IF NOT EXISTS (SELECT * FROM Customer_Profile WHERE Company_Name = ' + @variable + ') BEGIN Insert into Customer_Profile(....) END My question here is that, Is that the correct way of using IF [b]NOT EXISTS ??? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 01:36:16
|
Your problem is not on the NOT EXISTS. Your trigger can only handle one record being inserted only. You should assume the inserted as a table that may contains more than 1 records.CREATE TRIGGER [tr_gwAccount_INSERT]ON [dbo].[GW_Account]FOR INSERTASBEGIN Insert into Customer_Profile(....) select . . . from inserted i where not exists (select * from Customer_Profile x where x.pkcol = i.pkcol) END take a look at http://www.sqlteam.com/item.asp?ItemID=3850 KH |
 |
|
|
wonder_gal
Starting Member
4 Posts |
Posted - 2007-03-08 : 01:57:03
|
| Thanks for prompt reply. Actually I've read through the examples given in http://www.sqlteam.com/item.asp?ItemID=3850 before posting up this thread. :)But I still dun understand from your example, why is it I need to insert into Customer_Profile(....) before selecting(Means checking whether it already exists or not)? Could you pls explain further on BEGIN Insert into Customer_Profile(....) select . . . from inserted i where not exists (select * from Customer_Profile x where x.pkcol = i.pkcol) ENDThanks alot. :) |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-08 : 02:02:36
|
| It will select only those records from INSERTED table whose primary key value (i.e. company name in your case) does not exist in Customer_Profile table and insert them in the Customer_Profile table.No need to separate IF EXISTS and INSERT statements.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-03-08 : 02:03:31
|
quote: Originally posted by khtan Your problem is not on the NOT EXISTS. Your trigger can only handle one record being inserted only. You should assume the inserted as a table that may contains more than 1 records.CREATE TRIGGER [tr_gwAccount_INSERT]ON [dbo].[GW_Account]FOR INSERTASBEGIN Insert into Customer_Profile(....) select . . . from inserted i where not exists (select * from Customer_Profile x where x.pkcol = i.pkcol) END take a look at http://www.sqlteam.com/item.asp?ItemID=3850 KH
hi khtan,i think there is no need to check condition like NOT EXISTS, but when that guy has PK on his table. Because we have written trigger for insert. Then if there is no any duplication, it will not insert any record into Customer_Profile, and hence there will be no issue for record existanct in Customer_Table.Hopes i have explained well.let me know, if i m wrong,Mahesh |
 |
|
|
wonder_gal
Starting Member
4 Posts |
Posted - 2007-03-08 : 02:47:20
|
| Hi all, I've rewrite my trigger asCREATE TRIGGER [tr_gwAccount_INSERT]ON [dbo].[GW_Account]FOR INSERTASBEGIN INSERT INTO Customer_Profile(paramA, ParamB, paramC) SELECT paramI, paramJ, paramK FROM Inserted Y WHERE NOT Exists (SELECT * FROM Customer_Profile X WHERE X.paramA = Y.paramI) ENDand it WORKS! Really thanks alot to everyone here. :D But I still dun understand why both INSERT and SELECT can be used consecutively in trigger. :( And why the INSERT in statementINSERT INTO Customer_Profile(paramA, ParamB, paramC)does not have to be in the format of INSERT INTO <table name>(.....) [b]VALUES(....)/b]??? Could someone please explain me why? :p |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 02:53:00
|
see BOLquote: Inserting a Row Using INSERT...ValuesThe VALUES keyword specifies the values for one row of a table. The values are specified as a comma-separated list of scalar expressions whose data type, precision, and scale must be the same as or implicitly convertible to the corresponding column in the column list. If a column list is not specified, the values must be specified in the same sequence as the columns in the table or view.For example, this statement inserts a new shipper into the Shippers table using the VALUES clause:INSERT INTO Northwind.dbo.Shippers (CompanyName, Phone) VALUES (N'Snowflake Shipping', N'(503)555-7233')
quote: Inserting Rows Using INSERT...SELECTThe SELECT subquery in the INSERT statement can be used to add values into a table from one or more other tables or views. Using a SELECT subquery also lets more than one row be inserted at one time.This INSERT statement inserts into a separate table some of the data from all the rows in titles whose type is modern cooking:USE pubsINSERT INTO MyBooks SELECT title_id, title, type FROM titles WHERE type = 'mod_cook'The select list of the subquery must match the column list of the INSERT statement. If no column list is specified, the select list must match the columns in the table or view being inserted into.
KH |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-03-08 : 02:53:49
|
| when u create any trigger, two magic tables are created. we can't view them. they are INSERTED and DELETED. when we write trigger for Insert the same insert statement will be inserted in magic table, named INSERTED. the same thing happen in case of deletion. it adds one entry into DELETED table. thats why u have to select values from INSERTED table and insert them into new / desired table.let me know, if i m wrong,Mahesh |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 03:01:14
|
quote: Originally posted by mahesh_bote
quote: Originally posted by khtan Your problem is not on the NOT EXISTS. Your trigger can only handle one record being inserted only. You should assume the inserted as a table that may contains more than 1 records.CREATE TRIGGER [tr_gwAccount_INSERT]ON [dbo].[GW_Account]FOR INSERTASBEGIN Insert into Customer_Profile(....) select . . . from inserted i where not exists (select * from Customer_Profile x where x.pkcol = i.pkcol) END take a look at http://www.sqlteam.com/item.asp?ItemID=3850 KH
hi khtan,i think there is no need to check condition like NOT EXISTS, but when that guy has PK on his table. Because we have written trigger for insert. Then if there is no any duplication, it will not insert any record into Customer_Profile, and hence there will be no issue for record existanct in Customer_Table.Hopes i have explained well.let me know, if i m wrong,Mahesh
IF the 2 tables relationship is 1 to 1.IF the 2 tables having the same PK.IF the 2 tables are in-sync before the trigger is implemented.IF the only way records get inserted into the Customer_Profile is via tis trigger.IF . . . IF . . .Well . . . You can't be really sure of these things. So better be safe. KH |
 |
|
|
wonder_gal
Starting Member
4 Posts |
Posted - 2007-03-08 : 03:04:46
|
| Ok, understood. Thanks to KH for your clear quotes and also thanks to everyone who willing to share here. :) |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-08 : 03:07:28
|
quote: Originally posted by mahesh_bote when u create any trigger, two magic tables are created. we can't view them. they are INSERTED and DELETED. when we write trigger for Insert the same insert statement will be inserted in SQL Server will add the newly inserted records into the magic table, named INSERTED. the same thing happen in case of deletion. it adds one entry the deleted records into DELETED table. thats why u have to select values from INSERTED table and insert them into new / desired table.let me know, if i m wrong,Mahesh
And for UPDATE operation, the affected records will be in inserted (after) and deleted (before) table. KH |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-03-08 : 03:13:37
|
quote: Originally posted by khtan
quote: Originally posted by mahesh_bote when u create any trigger, two magic tables are created. we can't view them. they are INSERTED and DELETED. when we write trigger for Insert the same insert statement will be inserted in SQL Server will add the newly inserted records into the magic table, named INSERTED. the same thing happen in case of deletion. it adds one entry the deleted records into DELETED table. thats why u have to select values from INSERTED table and insert them into new / desired table.let me know, if i m wrong,Mahesh
And for UPDATE operation, the affected records will be in inserted (after) and deleted (before) table. KH
khtan, its brain wash for me.thanks a lot,Mahesh |
 |
|
|
|