|
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
MonsonUniversity1, 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 Registrations (Admissions)
- Double-click Courses (Academics)
- Click Close
- In the Diagram pane, click the check box of SemesterID to remove it
- In the Registrations (Admissions) tables, click the check box of
StudentNumber
- In the Courses (Academics) tables, click the check boxes of
CourseCode and CourseName
|
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 Students (Academics)
- Double-click Registrations (Admissions)
- Double-click Semesters (admissions)
- Double-click Courses (Academics)
- Click Close
- In the Students (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 Courses (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 94708257

- 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 let the user see the records of a certain student in a specify
semester, in the Add
Table dialog box, double-click Students (Academics)
- Double-click Registrations (Admissions)
- Double-click Semesters (admissions)
- Double-click Courses (Academics)
- Click Close
- In the Students (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 Semester
- In the Courses (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
- Still in
the Criteria pane, click the box at the intersection of Semester and
Filter, type @SpecifySemester
- To see the result, on the main menu, click Query Designer -> Execute
SQL
- In the Query Parameters dialog box, under Value, on the right side
of @StdNbr and type 20409220
- At the intersection of @SpecifySemester and Value, type FALL
2010

- 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, on the right side
of @StdNbr and type 94708257
- At the intersection of @SpecifySemester and Value, type SUMMER 2010
- Click OK

- Since we know the student and the semester we are querying, there is
no reason to show it in the result.
In the Criteria pane, in the
Output column, click the check boxes of StudentNumber and Semester to
remove them
- To see the records of another student, on the main menu, click Query
Designer -> Execute SQL
- In the Query Parameters dialog box, under Value, on the right side
of @StdNbr and type 71513159
- At the intersection of @SpecifySemester and Value, type FALL 2010
- Click OK
- Close Microsoft SQL Server
|
|