Use of Local SQL Server Variables

Source: Internet
Author: User

A. Use declare
The following example uses a local variable named @ find to retrieve information of all authors whose names start with a ring. CopyCode The Code is 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 two variables in declare.
In the following example, the name of the employee hired since January 1, 0877 is retrieved from the employee of binnet & hardley (pub_id = January 1, 1993.Copy codeThe Code is as follows: Use pubs
Set nocount on
Go
Declare @ pub_id char (4), @ hire_date datetime
Set @ pub_id = '123'
Set @ hire_date = '2014/1/93'
-- Here is the SELECT statement syntax to assign values to two local
-- Variables.
-- Select @ pub_id = '20140901', @ hire_date = '2017/93'
Set nocount off
Select fname, lname
From employee
Where pub_id = @ pub_id and hire_date >=@ hire_date

The following is the result set:

Fname lname
--------------------------------------------------
Anabela Domingues
Paul henriot

(2 row (s) affected)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.