Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL if statement...

Status
Not open for further replies.

lidds

Programmer
Joined
Jun 9, 2005
Messages
72
Location
GB
What I want to be able to do is inside a SQL stored procedure perform a SELECT query and if that SELECT query returns 0 rows then an UPDATE query is performed

I know this is probably really simple, but I can't seem to get it to work

Thanks

Simon
 
If you're talking about Microsoft SQL Server, then....

Code:
If Exists(Select * From YourTable Where YourPrimaryKey = 1234)
  Update YourTable
  Set    Col1 = 'Blah',
         Col2 = 'Whatever'
  Where  YourPrimaryKey = 1234
Else
  Insert
  Into   YourTable(YourPrimaryKey, Col1, Col2)
  Values (YourPrimaryKey, 'Blah', 'Whatever')


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top