zoukankan      html  css  js  c++  java
  • TSQL编程

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

    2.视图

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

    3.SQL编程

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

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

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

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

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

    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

    C#里的Switch case 变形到数据库里用法

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

    case --switch...case的开头
    when @ccname='宝马' then '%宝马%'
    when @ccname='奥迪' then '%奥迪%'
    else '%'
    end --switch...case的结尾


    循环:
    注意循环四要素

    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

    whie(条件)
    {
    循环体
    }

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

    常用函数

    1.数学函数:操作一个数据,返回一个结果

    --取上限ceiling
    select code,name,ceiling(price) from car ;

    --取下限 floor
    select floor(price) from car

    --ABS 绝对值

    --派 PI(),圆周率,括号里不需要加东西

    --ROUND 四舍五入
    select ROUND(3.76,0)

    --SQRT 开根号

    --SQUARE 平方,乘以自己


    2.字符串函数:

    --转换大写 upper
    select upper(pic) from car;

    --转换小写 lower

    --去空格
    select ltrim (' 123 ') 去左空格

    select ' 123123 ' 可以不查数据,直接这样显示出来

    --space() 里面放几个数字,就打印出来几个空格

    --LEFT,类似于SubString,从左边开头截取
    select LEFT('123456',3);

    --len,长度
    select len('aaaaaa'); 返回几个长度

    --replace 替换
    select replace('aaaaabbaaaaa','bb','haha');把第一个字符串中的bb替换成haha

    --reverse 翻转
    select reverse('abc'); 结果是 cba

    --字符串转换函数 str
    select str(1.567,3,1);
    把1.567转换成字符串,最多留3位,小数点算一位,保留小数点后1位

    --字符串截取 SUBSTRING
    select substring('abcdefg',2,3);
    从第2位开始截取3位,索引从1开始


    3.时间日期函数:

    --获取当前系统时间 GetDate()
    select getdate();

    sysdatetime() 获取数据库服务的时间戳

    --获取年月日 year month day
    select year('1999-1-1');

    --判断日期是否正确,isdate 返回bit
    select isdate('2000-2-31')返回bit类型,false是0,true是1

    --添加时间 dateadd
    select dateadd(year,5,'2000-1-1');
    添加什么类型,加多少,给谁加

    --返回星期几 datename,返回的值是字符串
    select datename(weekday,'2000-1-1');

    也可以返回第几天,按月
    select datename(day,'2000-1-1');

    一年中第几天
    select datename(dayofyear,'2000-1-1');

    datepart 一样可以返回周几,但是返回的是int类型

  • 相关阅读:
    496. 下一个更大元素 I『简单』
    492. 构造矩形『简单』
    443. 压缩字符串『简单』
    455. 分发饼干『简单』
    463. 岛屿的周长『简单』
    38. 外观数列『简单』
    28. 实现 strStr()『简单』
    441. 排列硬币『简单』
    628. 三个数的最大乘积『简单』
    575. 分糖果『简单』
  • 原文地址:https://www.cnblogs.com/blueteasama/p/5816607.html
Copyright © 2011-2022 走看看