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)


     

  • 相关阅读:
    大小端判断
    引用计数
    STL_ALGORITHM_H
    书单一览
    GIT版本控制系统(二)
    JS随机数生成算法
    STL学习笔记--临时对象的产生与运用
    乱序优化与GCC的bug
    你的灯亮着吗?
    交换机和路由器
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/217103.html
Copyright © 2011-2022 走看看