zoukankan      html  css  js  c++  java
  • 明日科技的SQL Server---6

    use db_2012
    --声明局部变量,并为它赋值
    declare @someName nchar(10)
    select @someName = Name from tb_Student where Spe = '会计学'
    print @someName
    --使用SET为变量赋值
    declare @song char(20)
    set @song = 'I love flower'
    print @song
    --多个变量一起声明并赋值
    declare @b int, @c char(10),@a int
    select @b = 1, @c ='love', @a = 2
    print @b
    print @c
    print @a
    
    --全局变量
    --select @@connections
    --select @@cpu_busy
    --select @@CURSOR_ROWS
    --select @@DBTS
    --select @@ERROR
    --select @@FETCH_STATUS
    --select @@IDENTITY
    --select @@IDLE
    --select @@IO_BUSY
    --select @@LOCK_TIMEOUT
    --select @@PACK_RECEIVED
    --select @@PACK_SENT
    --select @@PROCID
    --select @@remserver    
    --select @@ROWCOUNT
    --select @@SPID
    --select @@TOTAL_ERRORS
    --select @@TOTAL_READ
    --select @@TOTAL_WRITE
    --select @@TRANCOUNT
    --select @@VERSION
    
    --把所选行一次都注释的快捷键是Ctrl + K, Ctrl + C, 取消注释是Ctrl + K, Ctrl + U
    
    --流程控制
    -- begin ... end
    declare @x int, @y int, @t int
    set @x = 1
    set @y = 2
    begin
    set @t = @x
    set @x = @y
    set @y = @t
    end
    print @x
    print @y
    go
    -- if
    declare @x int
    set @x = 3
    if @x > 0
    print '@x是正数'
    print 'end'
    go
    
    declare @x int
    set @x = 8
    if @x % 2 = 0
    begin
    print '@x是偶数'
    print 'end'
    end
    go
    
    -- if ... else
    declare @x int, @y int
    set @x = 3
    set @y = 3
    if @x > @y
    print '@x大于@y'
    else
    print '@x小于或等于@y'
    go
    
    --嵌套使用
    declare @x int, @y int
    set @x = -8
    set @y = -3
    if @x > 0
        if @y > 0
            print '@x@y位于第一象限'
        else
            print '@x@y位于第四象限'
    else
        if @y > 0
            print '@x@y位于第二象限'
        else
            print '@x@y位于第三象限'
    
    -- case ... when分为case简单函数和case搜索函数
    -- 这个一个case搜索函数的例子
    use db_2012
    go
    select *, 备注=
    case
        when Grade >= 90 then '成绩优秀'
        when Grade < 90 and Grade >= 80 then '成绩良好'
        when Grade < 80 and Grade >= 70 then '成绩及格'
    else '不及格'
    end
    from tb_Grade
    -- while(可搭配continue和break语句使用)
    -- 求1到10之间的和
    declare @n int, @sum int
    set @n = 1
    set @sum = 0
    while @n <= 10
        begin
            set @sum = @sum + @n
            set @n = @n+1
        end
    print @sum
    
    -- return
    declare @x int
    set @x = 3
    if @x > 0
        print '遇到return之前'
    return
    print '遇到return之后'
    go
    
    -- goto
    declare @x int
    select @x = 1
    loving:
        print @x
        select @x = @x + 1
    while @x <= 3 goto loving
    
    -- waitfor
    -- 等待3秒显示
    waitfor delay '00:00:03'
    print '祝你节日快乐'
    
    -- ?点显示
    waitfor time '10:28:40'
    print '现在时间10:25:50'
    
    -- declare 字符时要指定长度,多个声明用逗号相隔
    declare @c char(8), @c2 char(20)
    set @c = 'xyz'
    set @c2 = @c
    print @c2
    go
    
    -- print
    declare @x char(20)
    set @x = '不再让你孤单'
    print @x
    print '最喜爱的电影' + @x
    
    -- backup
    backup database db_2012 to disk='backup.bak'
    --restore
    restore database db_2012 from disk='backup.bak' with replace
    
    -- select
    use db_2012
    declare @courses char(10)
    select @courses = Subjet from tb_Grade
    print @courses
    go
    
    -- set
    declare @x int
    set @x = 1
    print @x
    
    --shutdown
    shutdown with nowait --立即停止SQL Server的执行
    
    -- use
    use db_2012
    select * from Student
    
    --练习
  • 相关阅读:
    全面了解Nginx主要应用场景
    手把手教你构建 C 语言编译器
    Docker镜像原理和最佳实践
    Docker网络深度解读
    PostgreSQL 10.0 preview 功能增强
    阿里沈询:分布式事务原理与实践
    CPU、内存、IO虚拟化关键技术及其优化探索
    原理、方法双管齐下,大神带你细解Redis内存管理和优化---场景研读
    ASP.NET 5已终结,迎来ASP.NET Core 1.0和.NET Core 1.0 转
    RabbitMQ学习系列
  • 原文地址:https://www.cnblogs.com/captionAmazing/p/15167160.html
Copyright © 2011-2022 走看看