sqlserver 局部變量的使用
發(fā)布日期:2022-01-29 08:29 | 文章來源:腳本之家
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
下面是結(jié)果集: fname lname
-------------------- ------------------------------
Anabela Domingues
Paul Henriot (2 row(s) affected)
下例使用名為 @find 的局部變量檢索所有姓以 Ring 開頭的作者信息。
復(fù)制代碼 代碼如下:
Use pubs
declare @find varchar(30)
set @find='Ring%'
select au_lname,au_fname,phone
from authors
where au_lname like @find
下例從 Binnet & Hardley (pub_id = 0877) 的雇員中檢索從 1993 年 1 月 1 日起所雇傭的雇員名稱。
復(fù)制代碼 代碼如下:
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
下面是結(jié)果集: fname lname
-------------------- ------------------------------
Anabela Domingues
Paul Henriot (2 row(s) affected)
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章