zoukankan      html  css  js  c++  java
  • 一气呵成得到 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)


     

  • 相关阅读:
    DB2数据库BACKUP PENDING状态(修改日志模式导致)(转)
    JAVA调用WebService实例
    eclipse启动报JVM terminated. Exit code=-1的解决方法
    JAVA使用Dom4j组装、解析XML
    任务调度IBM Tivoli Workload Scheduler(TWS)
    Java遍历Map数据的几种方式
    谈谈我对Log4j2以外的感想
    ESQL中添加JMS参数
    node.js-node-inspector调试
    前端-浏览器内核
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/217103.html
Copyright © 2011-2022 走看看