In all conditional statements we have used so far, we
knew the value to check against a column. In reality, you can create a query
without specifying the value to check. A query is referred to as
parameterized if it would expect an external value to perform its
comparison. The statement starts like a normal SELECT
operation with a WHERE clause. Here is an example:
SELECT * FROM Students WHERE Sex = N'male';
Instead of specifying the value of the column(s) in the
WHERE clause, you can wait for the user to do so.
Practical
Learning: Introducing Parameterized Queries
|
|
- In the Object Explorer, in the Tables section under
MonsonUniversity2, right-click Admissions.Semesters and click Edit Top
200 Rows
- On the main menu, click Query Designer -> Pane -> Diagram
- On the main menu, click Query Designer -> Pane -> Criteria
- On the main menu, click Query Designer -> Pane -> SQL
- On the main menu, click Query Designer -> Add Table...
- To prepare to let a user select a semester to get a list of students
attendance based on that semester, in the Add Table dialog box,
double-click UndergraduateRegistrations (Admissions)
- Double-click UndergraduateCourses (Academics)
- Click Close
- In the Diagram pane, click the check box of SemesterID to remove it
- In the UndergraduateRegistrations (Admissions) tables, click the check box of
StudentNumber
- In the UndedrgraduateCourses (Academics) tables, click the check boxes of
CourseCode, CourseName, and Credits
Creating a Parameterized Query
|
|
To visually create a parameterized statement, in the
Object Explorer, right-click the table and click Edit Top 200 Rows. In the
Criteria pane, click the box at the intersection of the column and Filter.
Type @ followed by a variable name.
To create a parameterized query with code, open a Query
window. Start a normal SELECT expression that contains a
condition. In the WHERE expression, replace the value with the name of a
variable starting with @. Here is an example:
SELECT StudentNumber, LastName, FirstName, City, State
FROM Students
WHERE StudentNumber = @StdNbr;
Practical
Learning: Creating a Parameter
|
|
- In the Criteria pane, click the box at the intersection of Semester
and Filter
- Type @SpecifySemester
- In the SQL pane, delete TOP (200)
Executing a Parameterized Statement
|
|
After creating a parameterized statement, you can test
and/or use it. When you run the query, the SQL interpreter would request a
value for the column. When you execute the statement, a dialog box would
come up, asking you to enter a value for the filtered field. You can then type the appropriate value and click OK (or
press Enter).
Practical
Learning: Executing a Parameterized Statement
|
|
- To see the result, on the main menu, click Query Designer -> Execute
SQL
- In the Query Parameters dialog box, under Value, type FALL
2010
- Click OK
- In the Diagram pane, right-click each of the tables and click Remove
- On the main menu, click Query Designer -> Add Table
- To let the user see the records of a certain student, in the Add
Table dialog box, double-click GraduateStudents (Academics)
- Double-click GraduateRegistrations (Admissions)
- Double-click Semesters (admissions)
- Double-click GraduateCourses (Academics)
- Click Close
- In the GraduateStudents (Admissions) tables, click the check box of
StudentNumber
- In the Criteria pane, click the first empty box under StudentNumber
and type
LastName + N', ' + FirstName
- Press Tab and type [Student Name]
- In the Diagram pane, in the Semesters (Admissions) table, click the
check box of Semester
- In the GraduateCourses (Academics) table, click the check boxes of
CourseCode, CourseName, and Credits
- In the Criteria pane, click the box at the intersection of
StudentNumber and Filter
- Type @StdNbr
- To see the result, on the main menu, click Query Designer -> Execute
SQL
- In the Query Parameters dialog box, under Value, type
24795711
- Click OK
- To see the records of another student, on the main menu, click Query
Designer -> Execute SQL
- In the Query Parameters dialog box, under Value, type 31741957
- To let a user specify a student and a semester to view records, in
the Criteria pane, click the box at the intersection of Semester and
Filter, type @SpecifySemester
- To test it, right-click the Diagram pane and click Execute SQL
- Make sure 31741957 is specified for the @StdNbr parameter.
At the intersection of @SpecifySemester and Value, type
FALL
2010
- Click OK
- Close Microsoft SQL Server
|
|