sqlserver 局部變量的使用
更新時間:2010年06月24日 00:56:17 作者:
在SQL中我們或許會使用到局部變量的問題,下面給出微軟的兩個小例子
A. 使用 DECLARE
下例使用名為 @find 的局部變量檢索所有姓以 Ring 開頭的作者信息。
Use pubs
declare @find varchar(30)
set @find='Ring%'
select au_lname,au_fname,phone
from authors
where au_lname like @find
@find就是一個局部變量。
B. 在 DECLARE 中使用兩個變量
下例從 Binnet & Hardley (pub_id = 0877) 的雇員中檢索從 1993 年 1 月 1 日起所雇傭的雇員名稱。
USE pubs
SET NOCOUNT ON
GO
DECLARE @pub_id char(4), @hire_date datetime
SET @pub_id = '0877'
SET @hire_date = '1/01/93'
-- Here is 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
下面是結果集:
fname lname
-------------------- ------------------------------
Anabela Domingues
Paul Henriot
(2 row(s) affected)
下例使用名為 @find 的局部變量檢索所有姓以 Ring 開頭的作者信息。
復制代碼 代碼如下:
Use pubs
declare @find varchar(30)
set @find='Ring%'
select au_lname,au_fname,phone
from authors
where au_lname like @find
@find就是一個局部變量。
B. 在 DECLARE 中使用兩個變量
下例從 Binnet & Hardley (pub_id = 0877) 的雇員中檢索從 1993 年 1 月 1 日起所雇傭的雇員名稱。
復制代碼 代碼如下:
USE pubs
SET NOCOUNT ON
GO
DECLARE @pub_id char(4), @hire_date datetime
SET @pub_id = '0877'
SET @hire_date = '1/01/93'
-- Here is 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
下面是結果集:
fname lname
-------------------- ------------------------------
Anabela Domingues
Paul Henriot
(2 row(s) affected)
相關文章
如何安裝 SQL Server 2016及SQL Server Man
這篇文章主要介紹了如何安裝 SQL Server 2016及SQL Server Management Studio安裝配置,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2024-12-12
詳解DB2 sqlstate 57016 SQLCODE=-668 原因碼 "7"錯誤的快速解決辦法
db2 sqlstate 57016,db2 57016 原因碼7錯誤怎么解決呢?下面小編給大家?guī)砹薉B2 sqlstate 57016 SQLCODE=-668 原因碼 "7"錯誤的快速解決辦法,一起看下吧2016-08-08

