SQL Server - Multiple Where condition

Asked By Smith
09-Feb-12 02:17 AM
How and why to use multiple where condition in sqlserver ?
give example how to use 2 or 3 where condition ?

cmd = new SqlCommand("select sum(credit) AS total from creditadd where cid<=cid and where userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);
is this is corret syntax ?

  Sandeep Mittal replied to Smith
09-Feb-12 02:23 AM
Hi remove second where from the condition
also there is no relevance of cid<=cid condtion
  Reena Jain replied to Smith
09-Feb-12 02:23 AM
Hi,

Yes you can use multiple where condition in single query.

SQL always allows us to combine two or more conditions by using the AND and OR or NOT operators. Any number of simple conditions can be present in a single SQL statement, to allow us to create complex WHERE clauses that allow us to control which rows are included in our query results.

following is syntax to execute an SQL SELECT query with multiple conditions in the WHERE clause:
SELECT column_list
FROM table_name
WHERE column_name condition {[AND|OR} column_name condition}
So your query is correct
  dipa ahuja replied to Smith
09-Feb-12 02:52 AM
You can't write more than one Where in your query.

U can combine more condition with the AND keyword

for ex:

cmd = new SqlCommand("select sum(credit) AS total from creditadd where (cid<=cid ) AND (userid='" + TextBox1.Text + "') AND (username='" + TextBox2.Text + "')", con);  

  Somesh Yadav replied to Smith
09-Feb-12 03:21 AM
Hi here is an example.

select *
from table1 t1
WHERE
EXISTS
(
 
Select * --or 1. No difference.
 
From CRM_VCM_CURRENT_LEAD_STATUS Ex
 
Where Lead_Key = :_Lead_Key
-- correlation here
AND
t1
.CM_PLAN_ID = Ex.CM_PLAN_ID AND t1.CM_PLAN_ID =  Ex.Individual_ID
)

If you have multiple rows in the correlation then a JOIN gives multiple rows in the output, so you'd need distinct. Which usually makes the EXISTS more efficient.

Note "SELECT" * with a JOIN would also include columns from the row limiting tables

  Web Star replied to Smith
09-Feb-12 03:23 AM
Where keyword should be single and you can put multiple condition with same where keyword in query with the help of Operator 'AND' or 'OR'
so everything is correct just you remove 2nd where keyword from your query

cmd = new SqlCommand("select sum(credit) AS total from creditadd where cid<=cid and userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);  
Create New Account
help
SQL CAL Needed for a SQL Server to Access Another SQL Server? SQL Server Sorry for posting multiple groups. I have a SQL job running on SQL server A to query SQL server
Moving SQL Server 2000 to SQL Server 2005 - Need suggestions on syncronization of data between the server SQL Server Hi, Our current production database server is SQL Server 2000 and it is running on Windows 2000 server. Now as part
SQL Server SQL Server Hi, I 'm deploying a program running with SQL Server 2005 (open and modify SQL 2005 data bases) I want to know if i use my program with an SQL 2000 server will it work ? Thanks Omar Abid SQL Server Programming Discussions SQL Server 2005
Dumping sql server SQL Server Hi Is there a way to dump all the sql server data into some sort of file that can be imported into a local sql server later? I have used sql server dumper but it is not very stable and crashes
What's the go with sql server SQL Server I was just saying today that sql server is full of silly limitations and I seem to hit on a new one every features with more ways to manipulate things than you can poke a stick at. In sql server we don't even have a full range of date functions (a pretty basic feature