• TSQL编程



    1.索引

    唯一键/主键
    添加索引,设计界面,在任何一列前右键--索引/键--点击进入添加某一列为索引

    2.视图

    视图就是我们查询出来的虚拟表
    创建视图create view 视图名
          as
          SQL查询语句,分组,排序,in 等都不能写
    视图的用法 select * from 视图名

    3.SQL编程

    定义变量declare @变量名 数据类型 declare @a int
    变量赋值set @变量名 = 值 

    select @a --直接打印在结果框中
    set @a = 10 --也是赋值,不打印

    select @a; --打印在结果集中
    print @a; --打印在消息框中 输出

    例:
    1.查汽车表中名称含有宝马两个字的
    declare @name varchar(20)
    set @name='宝马'
    select * from car where Name like '%'+@name+'%'

    2.查汽车表中所有汽车的平均值并输出
    declare @price decimal(10,4)
    select @price = AVG(Price) from Car
    print '所有汽车的平均价格为:'+cast(@price as varchar(20))

                  ※cast字符类型转换(需要转换的 as 转换的类型)

    if ... else

    用法if后面没有小括号,花括号用begin end 替代

    if  判断条件
    begin
      要执行的语句
    end
    else
    begin
      要执行的语句
    end

    例:
    declare @a int
    declare @b int
    declare @c int

    set @a =10;
    set @b =5;

    if @a>@b
    begin
    set @c = @a + @b;
    end
    else
    begin
    set @c = @a - @b;
    end
    print @c

    Switch case

    declare @ccname varchar(20)
    set @ccname = '宝马'
    select * from Car where Name like

    case --switch...case的开头
    when @ccname='宝马' then '%宝马%'  --when当满足什么条件的时候then执行这个
    when @ccname='奥迪' then '%奥迪%'
    else '%'
    end --switch...case的结尾


    循环
    注意循环四要素

    whie 条件
    {
    循环体
    }

    例:

    declare @str varchar(20)
    set @str = '你好'
    declare @i int
    set @i = 1

    while @i<=10
    begin
    print @str + cast (@i as varchar(20))
    set @i = @i + 1
    end

    ※注意:语句结束之后不要写分号或逗号

  • 相关阅读:
    MySQL 必知必会学习笔记
    jemter 之cookies管理器
    linux shell通配符、元字符、转义符
    linux cut 、awk、grep、sed
    shell脚本的执行方式
    shell概述
    linux 查看用户常用命令
    linux的挂载命令
    linux关机和重启命令
    linux常用压缩格式
  • 原文地址:https://www.cnblogs.com/ShenG1/p/5761724.html
Copyright © 2020-2023  润新知