sqlserver 局部变量的使用
发布日期: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
下面是结果集: 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
下例从 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)
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。
相关文章