1,105,456 Community Members

How to give 2 Conditions in an SQL Query

Member Avatar
B.H
Newbie Poster
13 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

hi,

My Problem is when am deleting a row from the SQL Server Database....I need to check two conditions.

For eg: While deleting a row from the database,i need to check based upon the Name of the Employee and The Employee Number.

Because there might be Employees with the same Name but differenet Employee Number.

dim sql as string

sql = "DELETE FROM Employees WHERE Emp_Name='" & Txt_EmpName.Text.Trim & "'".....// Here can I add one more condition ie Employee Number in the Where clause.

Anyone...please help me.

Thanks lot

Cheers

B.H

Member Avatar
DavidRyan
Posting Whiz in Training
229 posts since Jul 2006
Reputation Points: 5 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

Currently:

sql = "DELETE FROM Employees WHERE Emp_Name='" & Txt_EmpName.Text.Trim & "'"

Outputs (example employee name = Joe):
sql = "DELETE FROM Employees WHERE Emp_Name='Joe'"

What I think you want:

sql = "DELETE FROM Employees WHERE Emp_Name='" & Txt_EmpName.Text.Trim & "' AND Emp_Number=" & Txt_EmpNumber.Trim

Outputs (example employee name (number) = Joe(2)):
sql = "DELETE FROM Employees WHERE Emp_Name='Joe' AND Emp_Number=2"

I'm assuming you have the employee number stored as an integer (or other number type), if it is stored as text you will need to add single quotes around the Txt_EmpNumber.Trim call.

Member Avatar
B.H
Newbie Poster
13 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I Tried the way u told me...but its showing error....do u have any other idea..thnks bro for replying to my doubt.

if u can help me with any other code that thinks can help me to work out the solution quickly..it would be greatly appreciatable.

Thanks once again for ur help

sql = "DELETE FROM Employees WHERE Emp_Name='" & Txt_Name.Text.Trim & "' AND Emp_Number= " & Txt_Empno.Text.Trim & ""
cnn.Execute(sqldelete)
End Sub

Member Avatar
DavidRyan
Posting Whiz in Training
229 posts since Jul 2006
Reputation Points: 5 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
0
 

As I said, if you have the employee number stored as text, the SQL statement requires single quotes around it. As follows (do not include the double parentheses, they are there to show the difference between '" and "):

sql = "DELETE FROM Employees WHERE Emp_Name='" & Txt_Name.Text.Trim & "' AND Emp_Number= (('))" & Txt_Empno.Text.Trim & "(('))"
Member Avatar
B.H
Newbie Poster
13 posts since Aug 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks bro..urs worked..sorry 4 da late reply.

cheers

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article