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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 if query

Author  Topic 

pille
Starting Member

15 Posts

Posted - 2011-03-09 : 05:53:34
hello all

i need to check with a trigger before inserting in the table if the values exist - if not create it

temp table (a couple of 1k entries - many are new many, are repeating)
1
2
3
4

now i want to insert a new value "5". the trigger should check if 5 exist already - if not create a entry "5".
i read a bit about it but i do not understand the syntax (to be honest)
thank you guys

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-03-09 : 06:04:20
Please be more specific.
Where is the data that has to be inserted coming from?
Are there Columns that can be used to determine if a row already exists?

Best is to give table structure, sample data and wanted result.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

pille
Starting Member

15 Posts

Posted - 2011-03-09 : 06:24:20

hello webfred
thank you for replay

the table called "temp_import"
here are colums called
dateset
timeset
c_ip
.....

i imported with cvs import my testfile
which looks like
2011-01-01 23:35:40 10.1.1.1 ....
2011-01-01 23:40:24 10.2.2.2 ....
2011-01-02 01:01:01 10.10.10.1 ....
how the 1 row i want to check if the datetime exist already if not create an insert "non_existing_date"
at these way, the table stay dynamically and extend itself.
hope it helps further
Go to Top of Page

lappin
Posting Yak Master

182 Posts

Posted - 2011-03-09 : 06:26:48
If I understand you correctly you want insert data unless it already exists. Use the MERGE statement.
Lots of examples on web. Here is one:
http://www.mssqltips.com/tip.asp?tip=1704
Go to Top of Page

pille
Starting Member

15 Posts

Posted - 2011-03-09 : 08:59:48
quote:
Originally posted by lappin

If I understand you correctly you want insert data unless it already exists. Use the MERGE statement.
Lots of examples on web. Here is one:
http://www.mssqltips.com/tip.asp?tip=1704



thank you
excellent hint
i am quite new to sql so i am happy for any suggestion
regards
Go to Top of Page
   

- Advertisement -