Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue Related to where clause in SQL Server

Tags:

c#

sql-server

I have a table name Students having (studentId, StudentName,Address,PhoneNo)

i have provided a filter for user to select only StudentId from Combobox to get the details of Students... and generate Report.

I have written following Query to get student Detail :

(select * from Students where StudentId = stdId)

Here stdId is a Parameter that i pass from code

It works fine if i select single studentId.... But in user selection Comobobox i have also provided "ALL" if user Select All from combobox i want to display details of all student

So what should I pass in stdId if user selects All ?

I used inline Query in C# (not using SQL Stored Procedure)

like image 731
ghanshyam.mirani Avatar asked Oct 24 '12 11:10

ghanshyam.mirani


People also ask

Why is my WHERE clause not working in SQL?

Check for leading or trailing whitespace in property keys and values. Leading or trailing whitespace in properties can make it seem like WHERE clauses aren't working, and this is often an issue with the data in the graph rather than the query itself.

What happen if WHERE clause is not given in query?

If the given condition does not match any record in the table, then the query would not return any row.

Why we Cannot use WHERE clause with aggregate functions?

Aggregate functions are not allowed because the WHERE clause is used for filtering data before aggregation. So while WHERE isn't for aggregation, it has other uses. To filter data based on an aggregate function result, you must use the HAVING clause.

What impact does a WHERE clause have on a SELECT query?

Summary. The SQL WHERE clause is used to restrict the number of rows affected by a SELECT, UPDATE or DELETE query. The WHERE condition in SQL can be used in conjunction with logical operators such as AND and OR, comparison operators such as ,= etc.


4 Answers

You can do it like this .

SELECT * from Students s
WHERE s.studentId = ISNULL(@StudentId,s.studentId) 

When "All" is selected in combo box pass null in your @studentid parameter. If you can not pass null in parameter then pass -1 or anything which can not be contain in you combox option and do it like this:(if -1= All)

 SELECT * from Students s
 WHERE s.studentId = @StudentId or @StudentId=-1

You can also try the answer given by Curt.

like image 167
muhammad kashif Avatar answered Oct 20 '22 17:10

muhammad kashif


If user selects all, pass NULL to @StudentId and change your query to:

select *
from Students
where (StudentId=@StudentId OR @StudentId IS NULL)
like image 45
Curtis Avatar answered Oct 20 '22 16:10

Curtis


In your stored procedure header, change the definition of @StudentId so that it can be passed as NULL.

ALTER PROCEDURE dbo.WhateverYourProcedureIsCalled( @StudentId int  = null )

Then change your WHERE clause as follows

...

SELECT * from Students
WHERE @StudentId IS NULL OR StudentId = @StudentId

From your code, if ALL is passed, you can omit the part where you set the value of the @StudentId parameter. SQL Server will use the default if not passed.

like image 4
Paul Alan Taylor Avatar answered Oct 20 '22 17:10

Paul Alan Taylor


If the where clause is included in the query you also need to supply a valid parameter value and it is therefore not possible to get all students when the where clause is included.

You need to differentiate your query based on the value selected in the combobox. You can do something like the following.

int studentId = 0;
//Where selectedValue comes from your combobox
Int32.TryParse(selectedValue, out studentId);
var query = "select * from Students";
if (studentId > 0)
{
  query = query + " where StudentId = @StudentId";
  //Remember to add studentId as parameter value
}
like image 1
Andreas Sabroe Jydebjerg Avatar answered Oct 20 '22 17:10

Andreas Sabroe Jydebjerg