Transact-SQL编程的要点有哪些
Admin 2022-08-06 群英技术资讯 489 次浏览
局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
基本语法:
声明变量 declare @变量名 变量类型 [@变量名 变量类型] 为变量赋值 set @变量名 = 变量值; select @变量名 = 变量值;
示例:
--局部变量 declare @id char(10)--声明一个长度的变量id declare @age int --声明一个int类型变量age select @id = 22 --赋值操作 set @age = 55 --赋值操作 print convert(char(10), @age) + '#' + @id select @age, @id go 简单hello world示例 declare @name varchar(20); declare @result varchar(200); set @name = 'jack'; set @result = @name + ' say: hello world!'; select @result; 查询数据示例 declare @id int, @name varchar(20); set @id = 1; select @name = name from student where id = @id; select @name; select赋值 declare @name varchar(20); select @name = 'jack'; select * from student where name = @name;
从上面的示例可以看出,局部变量可用于程序中保存临时数据、传递数据。Set赋值一般用于赋值指定的常量个变量。而select多用于查询的结果进行赋值,当然select也可以将常量赋值给变量。
注意:在使用select进行赋值的时候,如果查询的结果是多条的情况下,会利用最后一条数据进行赋值,前面的赋值结果将会被覆盖。
全局变量是系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用的。全局变量一般存储一些系统的配置设定值、统计数据。
--局部变量 declare @id char(10)--声明一个长度的变量id declare @age int --声明一个int类型变量age select @id = 22 --赋值操作 set @age = 55 --赋值操作 print convert(char(10), @age) + '#' + @id select @age, @id go 简单hello world示例 declare @name varchar(20); declare @result varchar(200); set @name = 'jack'; set @result = @name + ' say: hello world!'; select @result; 查询数据示例 declare @id int, @name varchar(20); set @id = 1; select @name = name from student where id = @id; select @name; select赋值 declare @name varchar(20); select @name = 'jack'; select * from student where name = @name;
T-SQL支持输出语句,用于显示结果。常用输出语句有两种:
基本语法
print 变量或表达式 select 变量或表达式
示例
select 1 + 2; select @@language; select user_name(); print 1 + 2; print @@language; print user_name();
print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。
语法
if <表达式> <命令行或程序块> else if <表达式> <命令行或程序块> else <命令行或程序块>
示例
if简单示例 if 2 > 3 print '2 > 3'; else print '2 < 3'; if (2 > 3) print '2 > 3'; else if (3 > 2) print '3 > 2'; else print 'other'; 简单查询判断 declare @id char(10), @pid char(20), @name varchar(20); set @name = '广州'; select @id = id from ab_area where areaName = @name; select @pid = pid from ab_area where id = @id; print @id + '#' + @pid; if @pid > @id begin print @id + '%'; select * from ab_area where pid like @id + '%'; end else begin print @id + '%'; print @id + '#' + @pid; select * from ab_area where pid = @pid; end go
基本语法
while <表达式> begin <命令行或程序块> [break] [continue] <命令行或程序块> end
示例
--while循环输出到 declare @i int; set @i = 1; while (@i < 11) begin print @i; set @i = @i + 1; end go --while continue 输出到 declare @i int; set @i = 1; while (@i < 11) begin if (@i < 5) begin set @i = @i + 1; continue; end print @i; set @i = @i + 1; end go --while break 输出到 declare @i int; set @i = 1; while (1 = 1) begin print @i; if (@i >= 5) begin set @i = @i + 1; break; end set @i = @i + 1; end go
基本语法
case when <条件表达式> then <运算式> when <条件表达式> then <运算式> when <条件表达式> then <运算式> [else <运算式>] end
示例
select *, case sex when 1 then '男' when 0 then '女' else '火星人' end as '性别' from student; select areaName, '区域类型' = case when areaType = '省' then areaName + areaType when areaType = '市' then 'city' when areaType = '区' then 'area' else 'other' end from ab_area;
批处理语句go Use master Go 延时执行,类似于定时器、休眠等 waitfor delay '00:00:03';--定时三秒后执行 print '定时三秒后执行';
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了mybatis-plus的sql语句打印问题,今天将常用的方式拷贝过来之后,发现没有发生效果(开始的时候以为是使用配置中心nacos导致问题,最后经过仔细的检查发现是单词拼错了),所以在这里记录一下
本文详细讲解了SQL Server中JSON函数的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
继SQl -Function创建函数数据库输出的结果用逗号隔开,在开发中也有许多以参数的形式传入带逗号字条串参数,需要的朋友可以参考下
这篇文章主要为大家详细介绍了SQL Server2012数据库备份和还原的教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了详解SQL Server表和索引存储结构,有助于大家对SQL存储方式有深入的理解,参考学习下吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008