设为首页 收藏本站
查看: 2087|回复: 0

[经验分享] SQL Server Transact-SQL 编程

[复制链接]

尚未签到

发表于 2015-6-27 09:41:46 | 显示全部楼层 |阅读模式
  T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。
  Ø 变量
  1、 局部变量(Local Variable)
  局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
  
  基本语法:

声明变量declare @变量名 变量类型 [@变量名 变量类型]为变量赋值set @变量名 = 变量值;select @变量名 = 变量值;
  
  示例:



--局部变量declare @id char(10)--声明一个长度的变量iddeclare @age int    --声明一个int类型变量age    select @id = 22    --赋值操作    set @age = 55    --赋值操作    print convert(char(10), @age) + '#' + @id    select @age, @idgo 简单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进行赋值的时候,如果查询的结果是多条的情况下,会利用最后一条数据进行赋值,前面的赋值结果将会被覆盖。
  
  2、 全局变量(Global Variable)
  全局变量是系统内部使用的变量,其作用范围并不局限于某一程序而是任何程序均可随时调用的。全局变量一般存储一些系统的配置设定值、统计数据。



全局变量select @@identity;--最后一次自增的值select identity(int, 1, 1) as id into tab from student;--将studeng表的烈属,以/1自增形式创建一个tabselect * from tab;select @@rowcount;--影响行数select @@cursor_rows;--返回连接上打开的游标的当前限定行的数目select @@error;--T-SQL的错误号select @@procid; --配置函数set datefirst 7;--设置每周的第一天,表示周日select @@datefirst as '星期的第一天', datepart(dw, getDate()) AS '今天是星期';select @@dbts;--返回当前数据库唯一时间戳set language 'Italian';select @@langId as 'Language ID';--返回语言idselect @@language as 'Language Name';--返回当前语言名称select @@lock_timeout;--返回当前会话的当前锁定超时设置(毫秒)select @@max_connections;--返回SQL Server 实例允许同时进行的最大用户连接数select @@MAX_PRECISION AS 'Max Precision';--返回decimal 和numeric 数据类型所用的精度级别select @@SERVERNAME;--SQL Server 的本地服务器的名称select @@SERVICENAME;--服务名select @@SPID;--当前会话进程idselect @@textSize;select @@version;--当前数据库版本信息 --系统统计函数select @@CONNECTIONS;--连接数select @@PACK_RECEIVED;select @@CPU_BUSY;select @@PACK_SENT;select @@TIMETICKS;select @@IDLE;select @@TOTAL_ERRORS;select @@IO_BUSY;select @@TOTAL_READ;--读取磁盘次数select @@PACKET_ERRORS;--发生的网络数据包错误数select @@TOTAL_WRITE;--sqlserver执行的磁盘写入次数
  
  Ø 输出语句
  T-SQL支持输出语句,用于显示结果。常用输出语句有两种:
  基本语法



print 变量或表达式select 变量或表达式
  
  示例



select 1 + 2;select @@language;select user_name(); print 1 + 2;print @@language;print user_name();
  print在输出值不少字符串的情况下,需要用convert转换成字符串才能正常输出,而且字符串的长度在超过8000的字符以后,后面的将不会显示。
  
  Ø 逻辑控制语句
  1、 if-else判断语句
  语法



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 + '%';    endelse    begin        print @id + '%';        print @id + '#' + @pid;        select * from ab_area where pid = @pid;    endgo
  
  2、 while…continue…break循环语句
  基本语法



while begin      [break]   [continue]   end
  示例



--while循环输出到declare @i int;    set @i = 1;while (@i < 11)    begin        print @i;        set @i = @i + 1;    endgo --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;                    endgo --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;                    endgo
  
  3、 case
  基本语法



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'    endfrom ab_area;
  
  4、 其他语句



批处理语句goUse masterGo 延时执行,类似于定时器、休眠等waitfor delay '00:00:03';--定时三秒后执行print '定时三秒后执行';

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-80883-1-1.html 上篇帖子: 浅谈SQL Server中的事务日志(一)----事务日志的物理和逻辑构架 下篇帖子: SQL Server 查询性能优化——创建索引原则(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表