Solution
As we know a simple SQL select statement goes like this:
select column_names from table_name
Now, If we want to add a condition to selected records based on a single value of a column, we use where clause
select column_names from table_name where column_value=value
Example
USE [DB_Name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_SelectAGraders]
@percentage decimal(18,0)
AS
BEGIN
SET NOCOUNT ON;
SELECT * from EnglishResults where Percentage>=@percentage
END
Explanation
This procedure will get whole record of all the students who scored percentage greater than or equal to given percentage in English Exams. This is done by using the where clause
* means data of all columns of EnglishResults table will be selected.
Percentage is the name of column in EnglishResults table.
@percentage is the parameter passed to the stored procedure.
EnglishResults is name of the table.
where refers to condition. Everything written after the where clause is the actual condition that needs to be met in order for the record to be selected i.e. Percentage is greater than or equal to given percentage.
If you want to select records based on multiple columns, you can simply use and between conditions in the where clause e.g.
USE [DB_Name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_SelectAGraders]
@percentage decimal(18,0),
@class nvarchar(max)
AS
BEGIN
SET NOCOUNT ON;
SELECT * from EnglishResults where Percentage>=@percentage and class=@class
END
You can apply as many conditions or filters as you want using and in the where clause
Leave a comment