• 一气呵成得到 MSSQL DB 中所有表的字段默认值约束的 DDL SQL 脚本


    Ms SQL Server
    企业管理器EM:
    在生成 SQL 脚本时不能为某数据库的所有表只生成"默认值约束"相关的 DDL SQL 脚本,
    而不生成建表。
    查询分析器QA:
    可以单独生成"默认值约束"相关的 DDL SQL 脚本,
    但又不能一气呵成得到所有表的"默认值约束"相关的 DDL SQL 脚本。

    自己动手丰衣足食:
    两种方法:
    1.纯 SQL 多表 self join 实现

    select 'ALTER TABLE [' + b.name + '] ADD CONSTRAINT [' + a.name + '] DEFAULT ' + d.text + ' FOR [' + c.name + ']' as [AddDefaultConstraint]
    ,
    'ALTER TABLE [' + b.name + '] DROP CONSTRAINT [' + a.name + ']' as [DropDefaultConstraint]
    from sysobjects a
    left join sysobjects b on a.parent_obj = b.id
    left join syscolumns c on a.parent_obj = c.id and a.info = c.colid
    left join syscomments d on a.id = d.id
    where a.xtype = 'd'
    order by b.name

    2.使用 MS T-SQL 函数,少两次显式 self join 使代码简洁些

    select 'ALTER TABLE [' + object_name(a.parent_obj) + '] ADD CONSTRAINT [' + object_name(a.id) + '] DEFAULT ' + d.text + ' FOR [' + COL_NAME(a.parent_obj,a.info) + ']' as [AddDefaultConstraint]
    ,
    'ALTER TABLE [' + object_name(a.parent_obj) + '] DROP CONSTRAINT [' + object_name(a.id) + ']' as [DropDefaultConstraint]
    from sysobjects a
    left join syscomments d on a.id = d.id
    where a.xtype = 'd'
    order by object_name(a.parent_obj)


     

  • 相关阅读:
    spark 修改默认log4j.properties 配置
    shell $x的含义
    hadoop hdfs 有内网、公网ip后,本地调试访问不了集群解决
    JAVA concurrent包下Semaphore、CountDownLatch等用法
    ETL DAG调度策略
    python2.7 Cheetah You don't have the C version of NameMapper installed
    python threading 用法
    log4j2 Filter用法详解
    ThreadLocal 原理及一些实现
    ETL hive update 之 deltamerge 优化
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/217103.html
Copyright © 2020-2023  润新知