• 【2017-3-16】Tsql基本编程,分支语句,循环语句,存储过程,分页查询,触发器


    Tsql基本编程:
    定义变量    declare @变量名 数据类型
    赋值          set @变量名 = 值 select @变量名 = 值
    取值打印    select @变量名 print @变量名


    分支语句:
    if @a>@b
    begin
       语句。。。。
    end
    else
    begin
       ....
    end

    循环语句:
    注意循环四要素:初始条件,循环条件,循环体,状态改变

    declare @a int;
    select @a = 1;

    while 循环条件 @a<=10
    begin
       循环体
    end

    存储过程:

    相当于函数,四要素:输入,输出,函数名,函数体
    创建:
    create proc 存储过程名
    参数 @a int,
    @b int
    as
    函数体
    return 值


    调用:
    exec 存储过程名

    exec @a = 存储过程名

    -----------分页查询-------------
    select top 10 * from student where Sno not in(select top 20 Sno from student) --分页查询标准语句

    --------------------------------------------------------------------

    declare @abc nvarchar(200);   --定义变量,变量名一定要带@
    set @abc = 'haha';            --变量赋值,用set
    select @abc = MAX(degree) from score   --变量赋值,适用方式不同
    select @abc                   --将结果映射到结果集
    print @abc                    --将结果打印到消息
    
    
    
    ----------分支语句判断------------
    declare @a int;  --定义变量,变量名一定要带@
    declare @b int;
    
    select @a =2;    --变量赋值
    select @b =1;
    
    if @a>@b         -------标准格式-------
    begin
        select 'a比b大'
    end
    else
    begin
        select 'b比a大'
    end
    
    
    
    
    ----------循环语句------------
    declare @aa int;
    select @aa = 1;
    
    while @aa<=10     -------标准格式------
    begin
        select @aa;
        select @aa = @aa+1;
    end
    
    
    
    
    -----------存储过程-------------
    create proc JiaFa     --创建一个存储过程,命名
    @a int,               --参数
    @b int
    as
        return @a+@b;     --返回的值
        
        
        
    declare @ccc int;         --定义一个变量
    exec @ccc = JiaFa 25,10   --exec表示你要用存储过程
    select @ccc               --打印结果
    
    
    
    
    --另一个用法
    create proc SelectAll     --创建一个存储过程
    as
    select *from Student      --函数体
    select *from Score
    select *from Course
    select *from teacher
    
    exec SelectAll            --使用这个存储过程,结果就是把上面的内容执行一边
    
    
    
    
    -----------分页查询-------------
    select top 10 * from student where Sno not in(select top 20 Sno from student)     --分页查询标准语句

    触发器:
    一个特殊的存储过程,没办法直接调用它,而是通过增删改的动作来触发它
    一个表的一个动作只能有一个触发器

    create trigger 命名(哪个表的哪个动作)
    on 表名 --针对于哪一个表写的触发器
    for 动作 --针对于哪一个动作触发之后的触发器
    instead of 动作 --针对于哪一个动作执行替换
    as 
    触发器内容

    ------增删改后执行查询-------
    create trigger users_Insert   --创建触发器,命名
    on users                      --作用于什么表
    for insert                    --触发器作用关键词(增删改)
    as
    select *from users            --触发器执行内容
    
    
    
    ------虽然执行删除,但是被替换了,触发器执行的是查看要删除的内容-------
    create trigger No_delete
    on users
    instead of delete
    as
    select *from deleted
    
    
    
    
    --------触发器与分支语句额的结合应用----------
    create trigger No_delete
    on users
    instead of delete
    as
    declare @a nvarchar(200);  --定义变量,变量名一定要带@
    select @a =classcode from class;    --变量赋值
    
    
    if @a= 'c002'         -------分支语句标准格式-------
    begin
        delete from class where classcode = 'c002'
    end
    else
    begin
        select '不能删除'
    end
    
    
    
    
    
    
    
    ----------级联删除---------同时删除拥有主外键关系的两个表的数据
    create trigger D_delete
    on class
    instead of delete
    as
    declare @aa nvarchar(200)
    select @aa = class from delete             --变量赋值
    delete from student where class = @aa      --删除外键表数据
    delete from class where class_code = @aa   --删除主键表数据
  • 相关阅读:
    DevC++实现调试功能
    DevC++实现调试功能
    《算法竞赛入门经典》计算组合数问题
    C#根据控件名获取控件对象
    C#根据控件名获取控件对象
    C# 使用反射获取私有属性的方法
    C# 使用反射获取私有属性的方法
    JSON转Object的方式
    JSON转Object的方式
    Hadoop: Setting up a Single Node Cluster.
  • 原文地址:https://www.cnblogs.com/hanqi0216/p/6566699.html
Copyright © 2020-2023  润新知