Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


To insert data only if it doesnot exist if exist show the table info and print a message


To insert data only if it doesnot exist if exist show the table info and print a message

Author
Message
SQLTestUser
SQLTestUser
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 548
Basically i am trying to accomplish
Inserting data into a table only if it doesnot exist and if does to show me the content of the table and print a simple message in either case, but i can seem to accomplish this

If exists
(Select * from TableA where PKEY = '1' )
---Display the content of the table
Select * FROM TableA

Print 'Table A info'
Else
Insert Into TableA
(X,Y,Z)
Values(1,'testLink', TestValue)
Print 'Table has been inserted with the specified data'

GO

tHanks for the help
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
avi-631555 (9/14/2010)
Basically i am trying to accomplish
Inserting data into a table only if it doesnot exist and if does to show me the content of the table and print a simple message in either case, but i can seem to accomplish this
If exists
(Select * from TableA where PKEY = '1' )
---Display the content of the table
Select * FROM TableA

Print 'Table A info'
Else
Insert Into TableA
(X,Y,Z)
Values(1,'testLink', TestValue)
Print 'Table has been inserted with the specified data'

GO

tHanks for the help

I guess all that's missing are the BEGIN..END statements to define the IF..ELSE blocks:
If exists 
(Select * from TableA where PKEY = '1' )
BEGIN
---Display the content of the table
Select * FROM TableA
Print 'Table A info'
END

Else
BEGIN
Insert Into TableA
(X,Y,Z)
Values(1,'testLink', TestValue)
Print 'Table has been inserted with the specified data'
END





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
SQLTestUser
SQLTestUser
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 548
I get an error with the begin end as well
Msg 156 level 15 state 1 line 7
Incorrect syntax near the keyword Else
Msg 102 level 15 state 1 line 12
Incorrect syntax near END

Is there any other way to do this
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36224 Visits: 18751
Can you post exactly what you run. The syntax Lutz showed should work.

It's

IF ()
begin
end
else
begin
end



Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
SQLTestUser
SQLTestUser
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 548
I think i got it the problem was that
on the else statement where the insert is specified i had to have [] specified for every column instead of simply having () i have no idea why
but once i added [] the table will update only if the the values are non existent if not the table infor is provided
If exists
(Select * from TableA where PKEY = '1' )
BEGIN
---Display the content of the table
Select * FROM TableA
Print 'Table A info'
END

Else
BEGIN
Insert Into TableA
([X],[Y],[Z])
Values(1,'testLink', TestValue)
Print 'Table has been inserted with the specified data'
END
thanks for the help
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
You should still get the inserted message when successful.
The square bracket issue is weird. Possible any "non-standard" column names? (e.g. including space, comma or any other fancy stuff?)



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
SQLTestUser
SQLTestUser
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 548
no there are no fancy columns, and i do get the inserted message, May be i should show the table info after the insert to show me the insert results. I will let u know what comes out
thanks
SQLTestUser
SQLTestUser
SSC-Enthusiastic
SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)SSC-Enthusiastic (173 reputation)

Group: General Forum Members
Points: 173 Visits: 548
Thanks the Merge was really helpful, will use it
thanks again
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search