SQL Server 2012 - Transact-SQL

时间:2023-03-09 04:36:18
SQL Server 2012 - Transact-SQL
  1. 变量
     --全局变量
    select @@VERSION --局部变量
    declare @i int
    set @i=5
    select @i  
  2. 通配符:   like   'joh%',  %任意长度的任意字符 ;   like 'joh_'  -单个字符 ;  like '[jk]ohn'  [jk] 匹配字符集合中的一个字符   ;  like '[^jh]' [^jh] 不包含j和h开头的
  3. SQL Server 2012 - Transact-SQL
  4. 数据库权限控制语句(DDL)
      --权限控制
    
      --赋予权限
    grant update,delete,select on Course
    To [guest] with grant option -- 拒绝更新
    deny update on Course
    To [guest] cascade -- 收回权限操作
    revoke delete on Course
    To [guest] cascade
  5. 流程控制语句
    1. while
        --流程控制
      declare @Count int
      set @Count=0 while(@Count<10)
      begin
      print @Count
      set @Count=@Count+1
      end
    2. if else
        declare @Age int
      set @Age=20
      if(@Age<30)
      print 'Young'
      else
      print 'Old'
    3. Case When
    4. Goto 语句
    5. Waitfor Delay 延迟执行 
         declare @Name varchar(10)
      set @Name='Micky'
      begin
      waitfor delay '00:00:05'
      print @Name
      end
    6. Return语句:终止当前T-SQL语句的执行,或者返回结果

           --创建 表值函数方法
      use SchoolDB
      Go
      create function FunGetStuByID(@stuID varchar(10))
      returns Table
      as
      return
      (
      select * from Student where StuID=@stuID
      )
      -- 调用方法
      select * from FunGetStuByID('003')