A. Use of DECLARE
The following example uses a local variable named @find to retrieve all the author information that begins with the ring of the last name.
Copy Code code as follows:
Use pubs
DECLARE @find varchar (30)
Set @find = ' ring% '
Select Au_lname,au_fname,phone
From authors
where au_lname like @find
@find is a local variable.
B. Use of two variables in DECLARE
The following example retrieves the name of the employee employed since January 1, 1993 from the employee of Binnet & Hardley (pub_id = 0877).
Copy Code code as follows:
Use pubs
SET NOCOUNT on
Go
DECLARE @pub_id char (4), @hire_date datetime
SET @pub_id = ' 0877 '
SET @hire_date = ' 1/01/93 '
--The SELECT statement syntax to assign values to two local
--variables.
--SELECT @pub_id = ' 0877 ', @hire_date = ' 1/01/93 '
SET NOCOUNT off
SELECT fname, lname
From employee
WHERE pub_id = @pub_id and hire_date >= @hire_date
Here is the result set:
FName lname
-------------------- ------------------------------
Anabela Domingues
Paul Henriot
(2 row (s) affected)