• 数据库系列之T-SQL(基础)


    变量

    1 变量分类

    全局变量、局部变量。

    2 局部变量

    DECLARE  @变量名称  变量类型 [,@变量名称 变量类型 ...]

    declare @Name varchar(50)
    declare @upoint int,@birthday datetime
    注意:在局部变量没有被赋值之前,它的值是NULL。 
    2.1 局部变量赋值
    SET  @局部变量 = 变量值

    SELECT  @局部变量 = 变量值
    SET仅支持对一个变量赋值
    SELECT支持同时给多个变量赋值,并且经常在Select查询语句中使用。
    2.2 变量赋值示例
    --声明一个变量
    declare @Name varchar(50)
    --同时声明两个变量
    declare @upoint int, @birthday datetime
    /*可以这样赋值*/
    set @Name = '小新'
    set @upoint = 200
    select @birthday = '1990-5-8'
    /*也可以这样赋值*/
    set @Name = '小新'
    select @upoint = 200,@birthday = '1990-5-8' from customers
    2.3 实例
    思路分析
    首先得到喜来乐的积分,存放到变量@upoint中;
    使用Select语句查询upoint>@upoint的记录。
    /*第一步:得到喜来乐的积分*/
    declare @upoint int
    select @upoint = upoint from customers 
    where customername=‘喜来乐'
    /*第二步:执行带有条件的查询语句*/
    select * from customers where upoint > @ upoint
    注意:SELECT赋值语句不能和SELECT语句同时使用

    3 全局变量

    全局变量不需要用户声明,是服务器级定义的,作用范围是任何程序。
    全局变量以@@开头,局部变量名称不能和全局变量相同。
    3.1 常用的全局变量
    变量    含义
    @@ERROR    最后一个T-SQL错误的错误号
    @@IDENTITY    最后一次插入的标识值
    @@ROWCOUNT        受上一个SQL语句影响的行数
    @@SERVERNAME    本地服务器的名称
    3.2 输出语句有两种
    SELECT查询语句
    PRINT语句
    PRINT 表达式
    PRINT一般用于观察T-SQL程序运行时的中间结果。
    3.3 示例

    declare @name varchar(50)
    declare @upoint int
    use booksmanager
    select @name=customername,@upoint =upoint 
    from customers 
    where customerid=10008'
    print  '姓名:'+@name
    print  ‘积分:'+str(@upoint,5)

    控制语句

    T-SQL中的控制语句和其他编程语言类似,主要有:顺序、条件、循环

    顺序语句

    BEGIN
            <命令行或程序块>
    END

    --示例
    if @myavg >70
       begin
         print 'c#编程成绩优秀,前三名考试信息是:'
         select top 3 * from score 
         where courseno=@courseid 
         order by score desc
       end
    else
      begin
         print 'c#编程成绩较差,后三名考试信息是:'
         select top 3 * from score 
         where courseno=@courseid 
         order by score 
     end

    条件语句

    if 语句

    IF  <条件表达式>
            <命令行或程序块>
    [ELSE [条件表达式]
            <命令行或程序块>]

    use master
    if exists (select * from sysdatabases where name=‘booksmanager')
       drop database booksmanager

    分支语句

    CASE <运算式>
        WHEN  <运算式>  THEN  <运算式>
        ……
       WHEN  <运算式>  THEN  <运算式>
       [ELSE  <运算式>]
    END

    ………

    select stuno,成绩=
    case
        when score<60 then 'E'
        when score between 60 and 69 then 'D'
        when score between 70 and 79 then 'C'
        when score between 80 and 89 then 'B'
        else 'A'
    end
    from Score
    where courseno=@courseid

    循环语句

    WHILE <条件表达式>
    BEGIN
        <命令行或程序块>
        [BREAK]
        [CONTINUE]
        [命令行或程序块]
    END

    while @pass/@total <0.8
    begin
         update Score set score = score+2 where courseno=@courseid
         select @pass = count(*) from Score where courseno=@courseid and score>=60
    end

    批处理

    在SQL Sever中,可以一次执行多个T-SQL语句,这些多个T-SQL语句称为批处理语句。
    “GO”就是批处理的标志。

    use myschool
    go --批处理标志
    select * from course1  --并不存在course1
    select * from student
    go --批处理标志

    注释

    在T-SQL语言中可使用两种注释符:行注释和块注释。
    注释不参与代码执行。
    --行注释用于描述性文字较少的场合

    /**
      块注释用于描述性文字较多的场合
      ........
      ........
    **/
  • 相关阅读:
    使用lua给wireshark编写uTP的Dissector
    Win32下 Qt与Lua交互使用(四):在Lua脚本中自由执行Qt类中的函数
    Win32下 Qt与Lua交互使用(三):在Lua脚本中connect Qt 对象
    Win32下 Qt与Lua交互使用(二):在Lua脚本中使用Qt类
    C# 自动部署之附加数据库
    机器码农:深度学习自动编程
    Oracle 记录插入时“Invalid parameter binding ”错误
    Visual Studio 在调试时启用编辑功能
    航摄比例尺与成图比例尺
    maven引用net.sf.json-lib
  • 原文地址:https://www.cnblogs.com/cmhunter/p/4280528.html
Copyright © 2020-2023  润新知