• 个人工作用SQL短句,不定时更新


    表字段操作

    --一、修改字段默认值

    alter table 表名 drop constraint 约束名字 ------说明:删除表的字段的原有约束

    alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 -------说明:添加一个表的字段的约束并指定默认值

    --二、修改字段名:

    alter table 表名 rename column A to B

    --三、修改字段类型:

    alter table 表名 alter column UnitPrice decimal(18, 4) not null

    --四、修改增加字段:

    alter table 表名 ADD 字段 类型 NOT NULL Default 0

    查找重复(根据一个字段)

    SELECT  字段 FROM  表名 t WHERE 字段 IN (SELECT 字段 FROM 表名 GROUP BY 字段 HAVING COUNT(字段)>1 ) ORDER BY t.字段

    分页查询

    (可能并不是效率最高得一种,但自己用的蛮顺手)根据select查询出的结果集做分页,并根据date排序,一下为实例。 DHFLOW_201904 为表名  date为字段

    select top 15 * 
    from(select row_number()
    over(order by date asc) as rownumber,* 
    from(select* from DHFLOW_201904 where date>'2019-04-01 15:01:57' and date<'2019-04-27 15:10:00' ) as a) temp_row
    where rownumber>((1-1)*15) order by date asc;

    去空格

    (抱怨一下sb运维导入数据的时候全弄成了非法数据)

    update 表名 set 字段a=RTRIM(字段a)

    update 表名 set 字段a=LTRIM(字段a)

    当然,也可以一句搞定两边   update 表名 set 字段a=LTRIM(RTRIM(字段a))

    向左补齐0

    如果字段a长度不够10位,则向左补0直到10位,只能处理数字(sb运维)

    update 表名 set 字段a=right(100000000000000000000+字段a,10) where  DATALENGTH(字段a)<10

    游标循环+将每行时间戳字段转换成datetime

    declare cur cursor for select id from DHFLOW_201904
    open cur
    declare @id varchar(50)
    fetch next from cur into @id
    while @@FETCH_STATUS=0
    begin
    update DHFLOW_201904 set date=DATEADD(S,cast((select swipDate from DHFLOW_201904 where id=@id) as int),'1970-01-01 08:00:00') where id=@id
    fetch next from cur into @id
    end
    close cur
    deallocate cur

  • 相关阅读:
    Celery详解
    JWT详解
    进程及进程池
    多线程详解
    python常用模块之os模块的用法
    python常用模块之paramiko与ssh
    reflect 反射
    http 静态文件
    模板渲染语言
    http web 开发
  • 原文地址:https://www.cnblogs.com/jbdxbl/p/10996346.html
Copyright © 2020-2023  润新知