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
 Select record based on multiple criteria (vars)

Author  Topic 

RedlightG20
Starting Member

3 Posts

Posted - 2008-04-17 : 17:05:31
Hi! I'm new to SQL and have a question...

I'm writing a script that gathers a few variables from an outside source, then queries a table and looks for a record that has the exact values of those variables. If the record is not found, a new record is added. If the record is found, nothing happens.

Basically my SELECT statement looks something like this, then is followed by an If... Else statement


SELECT * FROM TableName
WHERE LastName = varLastName
AND FirstName = varFirstName
AND Address = varAddress

If RecordSet.EOF = True Then
'Item Not Found, add new record
'code to add new record......
Else
'Item Found, do nothing
End If

RecordSet.Update
RecordSet.Close


Even when I try to delete the If.. statement and simply display the records, it comes up as blank. Is the syntax correct for my SELECT statement??

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-04-17 : 23:56:18
Your SELECT statement is fine. Can you print your varibales and see if it is getting the values as expected?

Prakash.P
The secret to creativity is knowing how to hide your sources!
Go to Top of Page

RedlightG20
Starting Member

3 Posts

Posted - 2008-04-18 : 09:02:42
Ok it seems I fixed the problem...

In my SELECT statement, I needed to use the proper quotations around the variables, otherwise it seems it is using the actual textual name of the variable as the criteria... not sure if this matters but this is a VBScript file for an Access DB :)

Came out to look something like this
SELECT * FROM TableName WHERE LastName = '" & varLastName & "' AND FirstName = '" & varFirstName & "' AND Address = '" & varAddress & "'"

Thanks for the reply pravin
Cheers!
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2008-04-18 : 09:14:58
Maybe you have already, but don't forget to consider people with names like O'Brian...


Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

RedlightG20
Starting Member

3 Posts

Posted - 2008-04-18 : 13:35:18
I haven't thought about that... How would I go about ensuring that data like that does not screw up the statement?

Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-18 : 13:44:22
Try using QUOTENAME() function.
Go to Top of Page
   

- Advertisement -