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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Exists

Status
Not open for further replies.

DirtDawg

IS-IT--Management
Apr 17, 2002
218
JP
Hello all,

I need some help with if exists (select * statement. I am trying to use if it exists to update and if the record does not exist to insert it. I am tyring to use asp to MSSQL 7 server. Here is the code below

strwkn = Request.form("wkn")
strsrv = request.form ("srv")
strvslvoy = Request.form("vslvoy")
stretd = Request.form("etd")

SET db = Server.CreateObject("ADODB.Connection")
db.Open "DSN=DSN", "id","pass"
SQL = if exists(select * from vsl where wkn = '" & strwkn & "' and srv = '" & strsrv & "')
"Update vsl set vslvoy='" & strvslvoy & "' Where wkn='" & strwkn & "' and srv='" & strsrv & "', and etd='" & stretd & "'"
ELSE
"Insert into vsl (wkn, vslvoy, srv, etd)Values ('" & strwkn & "', '" & strvslvoy & "', '" & strsrv & "', '" & stretd & "')"


Thanks in advance
 
You have a comma in the where clause, remove it the rest looks ok at first glance

and srv='" & strsrv & "', and etd='" & stretd & "'"

and srv='" & strsrv & "' and etd='" & stretd & "'"
 
Also Not sure about ASP but from a VB point of view you need to build the string correctly (quotes and contiuation chars).

SQL = "if exists(select * from vsl where wkn = '" & strwkn & "' and srv = '" & strsrv & "')" _ &
"Update vsl set vslvoy='" & strvslvoy & "' Where wkn='" & strwkn & "' and srv='" & strsrv & "', and etd='" & stretd & "'" _ &
" ELSE " _ &
"Insert into vsl (wkn, vslvoy, srv, etd)Values ('" & strwkn & "', '" & strvslvoy & "', '" & strsrv & "', '" & stretd & "')"
 
3rd attempt , you cant use select * with if exists, you must select one field (say wkn), also your where clauses should be the same in the Select and Update statements.

Thats it from me.
 
SonOf,

I tried what you said but I am still getting errors on the Select statement, I also read a little more about it and found out that it only drops tables. Is this true?

Thanks
 
The best answer is to write a stored procedure that accepts a few parameters (strwkn, strvslvoy, strsrv, and stretd) and determines whether to insert or update, then call the procedure from ASP. That way there is no question of the SQL being executed.

The EXISTS predicate is only concerned if a row is or is not returned from the sub-query, not what columns are included in that row. You can use SELECT * or SELECT Column or SELECT 1. SQL only cares if a row EXISTS or not. BOL shows examples of of all three.

--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top