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
 General SQL Server Forums
 New to SQL Server Programming
 insert if not exists

Author  Topic 

Naeemeh
Starting Member

3 Posts

Posted - 2010-08-03 : 14:59:44
Hi,

Can you tell me what is wrong with this query:
"if not exists TestDieFile values(7)"

TestDieFile is a table that has a column that accepts integer.
I want to insert 7 into the table if it doesn't already exists in the table. But I keep getting this error:

"Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TestDieFile'."

I tried putting the the "if not exists" phrase at the beginning of the
query too, but I get the same error.

Please tell me what I'm doing wrong? How should I user "if not exists"?

Thanks alot,
Naeemeh

Naeemeh
Starting Member

3 Posts

Posted - 2010-08-03 : 15:09:33
Corrections:

the query is: "insert if not exists TestDieFile values(7) "
and the error I get is :
"
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'if'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'TestDieFile'.
"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-03 : 15:10:36
[code]
DECLARE @i int; SET @i = 7
IF NOT EXISTS (SELECT * FROM TestDieFile WHERE intCol = @i)
INSERT INTO TestDieFile (intCol) SELECT @i
ELSE
PRINT 'Already Exists'
[/code]



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Naeemeh
Starting Member

3 Posts

Posted - 2010-08-03 : 15:22:14
Thank you soooooo much :D
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-03 : 15:29:34
quote:
Originally posted by Naeemeh

Thank you soooooo much :D




Our Pleasure..


DECLARE @QuestionAsked datetime; SET @QuestionAsked = '08/03/2010 14:59:44'
DECLARE @QuestionAnswered datetime; SET @QuestionAnswered = '08/03/2010 15:10:36'

SELECT DATEDIFF(mi,@QuestionAsked, @QuestionAnswered) AS [TimeToAnswer(minutes)]






Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -