https://www.cnblogs.com/champaign/p/3492510.html

/****** Object: StoredProcedure [dbo].[GET_TableScript_MSSQL] Script Date: 06/15/2012 11:59:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*==============================================================
名称: GET_TableScript_MSSQL
功能: 获取customize单个表的mysql脚本
创建:2010年5月12日
参数:@DBNAME --数据库名称
@TBNAME --表名
@SQL --输出脚本
==============================================================*/
ALTER PROCEDURE [dbo].[GET_TableScript_MSSQL] (@DBNAME varchar(40),@TBNAME VARCHAR(100),@SQL VARCHAR(max) OUTPUT) AS

declare @table_script nvarchar(max) --建表的脚本
declare @index_script nvarchar(max) --索引的脚本
declare @default_script nvarchar(max) --默认值的脚本
declare @check_script nvarchar(max) --check约束的脚本
declare @sql_cmd nvarchar(max) --动态SQL命令
declare @err_info varchar(200)
set @tbname = UPPER(@tbname);
if OBJECT_ID(@DBNAME+'.dbo.'+@TBNAME) is null
BEGIN
set @err_info='对象:'+@DBNAME+'.dbo.'+@TBNAME+'不存在!'
raiserror(@err_info,16,1)
return
END
----------------------生成创建表脚本----------------------------
--1.添加算定义字段
set @table_script = 'CREATE TABLE '+@TBNAME+'
('+char(13)+char(10);

--添加表中的其它字段
set @sql_cmd=N'
use '+@DBNAME+'
set @table_script=''''
select @table_script=@table_script+
'' [''+t.NAME+''] ''
+(case when t.xusertype in (175,62,239,59,122,165,173) then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (231) and t.length=-1 then ''[ntext]''
when t.xusertype in (231) and t.length<>-1 then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (167) and t.length=-1 then ''[text]''
when t.xusertype in (167) and t.length<>-1 then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (106,108) then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'',''+convert(varchar(30),isnull(t.scale,''''))+'')''
else ''[''+p.name+'']''
END)
+(case when t.isnullable=1 then '' null'' else '' not null ''end)
+(case when COLUMNPROPERTY(t.ID, t.NAME, ''ISIDENTITY'')=1 then '' identity'' else '''' end)
+'',''+char(13)+char(10)
from syscolumns t join systypes p on t.xusertype = p.xusertype
where t.ID=OBJECT_ID('''+@TBNAME+''')
ORDER BY t.COLID;
'
EXEc sp_executesql @sql_cmd,N'@table_script varchar(max) output',@sql_cmd output
set @table_script=@table_script+@sql_cmd
IF len(@table_script)>0
set @table_script=substring(@table_script,1,len(@table_script)-3)+char(13)+char(10)
+')'+char(13)+char(10)
+'GO'+char(13)+char(10)+char(13)+char(10)

--------------------生成索引脚本---------------------------------------
set @index_script=''
set @sql_cmd=N'
use '+@DBNAME+'
declare @ct int
declare @indid int --当前索引ID
declare @p_indid int --前一个索引ID
select @indid=-1, @p_indid=0,@ct=0 --初始化,以后用@indid和@p_indid判断是否索引ID发生变化
set @index_script=''''
select @indid=INDID
,@index_script=@index_script
+(case when @indid<>@p_indid and @ct>0 then '')''+char(13)+char(10)+''go''+char(13)+char(10) else '''' end)
+(case when @indid<>@p_indid and UNIQ=''PRIMARY KEY''
then ''ALTER TABLE ''+TABNAME+'' ADD CONSTRAINT ''+name+'' PRIMARY KEY ''+cluster+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid<>@p_indid and UNIQ=''UNIQUE''
then ''ALTER TABLE ''+TABNAME+'' ADD CONSTRAINT ''+name+'' UNIQUE ''+cluster+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid<>@p_indid and UNIQ=''INDEX''
then ''CREATE ''+cluster+'' INDEX ''+name+'' ON ''+TABNAME+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid=@p_indid
then '' ,''+COLNAME+char(13)+char(10)
END)
,@ct=@ct+1
,@p_indid=@indid
from
(
SELECT A.INDID,B.KEYNO
,NAME,(SELECT NAME FROM SYSOBJECTS WHERE ID=A.ID) AS TABNAME,
(SELECT NAME FROM SYSCOLUMNS WHERE ID=B.ID AND COLID=B.COLID) AS COLNAME,
(CASE WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=''UQ'') THEN ''UNIQUE''
WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=''PK'') THEN ''PRIMARY KEY''
ELSE ''INDEX'' END) AS UNIQ,
(CASE WHEN A.INDID=1 THEN ''CLUSTERED'' WHEN A.INDID>1 THEN ''NONCLUSTERED'' END) AS CLUSTER
FROM SYSINDEXES A INNER JOIN SYSINDEXKEYS B ON A.INDID=B.INDID AND A.ID=B.ID
WHERE A.ID=OBJECT_ID('''+@TBNAME+''') and a.indid<>0
) t
ORDER BY INDID,KEYNO'
EXEc sp_executesql @sql_cmd,N'@index_script varchar(max) output',@sql_cmd output
set @index_script=@sql_cmd
IF len(@index_script)>0
set @index_script=@index_script+')'+char(13)+char(10)+'go'+char(13)+char(10)+char(13)+char(10)
--生成默认值约束
set @sql_cmd='
use '+@DBNAME+'
set @default_script=''''
SELECT @default_script=@default_script
+''ALTER TABLE ''+OBJECT_NAME(O.PARENT_OBJ)
+'' ADD CONSTRAINT ''+O.NAME+'' default ''+t.text+'' for ''+C.NAME+char(13)+char(10)
+''GO''+char(13)+char(10)
FROM SYSOBJECTS O INNER JOIN SYSCOMMENTS T ON O.ID=T.ID
INNER JOIN SYSCOLUMNS C ON O.PARENT_OBJ=C.ID AND C.CDEFAULT=T.ID
WHERE O.XTYPE=''D'' AND O.PARENT_OBJ=OBJECT_ID('''+@TBNAME+''')'
EXEc sp_executesql @sql_cmd,N'@default_script varchar(max) output',@sql_cmd output
set @default_script=@sql_cmd+char(13)+char(10)

set @SQL=@table_script+@index_script+@default_script
declare @len int,@n int
set @len=LEN(@SQL)
set @n=0
while(@len>0)
BEGIN
PRINT(substring(@SQL,@n*4000+1,4000));
set @n=@n+1
set @len=@len-4000;
END

------------------------------hbh-------------------------

/*

select *
into hbh_tmp_SM_SO_20190312001
from SM_SO

exec [GET_TableScript_MSSQL] '','SM_SO'

*/

/*==============================================================
名称: GET_TableScript_MSSQL
功能: 获取customize单个表的mysql脚本
创建:2010年5月12日
参数:@DatabaseName --数据库名称
@TableName --表名
@SQL --输出脚本
==============================================================*/
alter PROCEDURE HBH_SP_Common_GetTableScript (
@DatabaseName varchar(40) = ''
,@TableName VARCHAR(100) = ''
,@ContainIndex bit = 1
,@ContainConstraint bit = 1
-- ,@SQL VARCHAR(max) OUTPUT
)
with encryption
AS

declare @SQL VARCHAR(max)

declare @UseDB varchar(125) = ''

if (@DatabaseName is not null and @DatabaseName != '')
begin
set @UseDB = 'use ' + @DatabaseName
end

declare @table_script nvarchar(max) = '' --建表的脚本
declare @index_script nvarchar(max) = '' --索引的脚本
declare @default_script nvarchar(max) = '' --默认值的脚本
declare @check_script nvarchar(max) = '' --check约束的脚本
declare @sql_cmd nvarchar(max) = '' --动态SQL命令
declare @err_info varchar(200) = ''
set @TableName = UPPER(@TableName);
if OBJECT_ID(@DatabaseName+'.dbo.'+@TableName) is null
BEGIN
set @err_info='对象:'+@DatabaseName+'.dbo.'+@TableName+'不存在!'
raiserror(@err_info,16,1)
return
END
----------------------生成创建表脚本----------------------------
--1.添加算定义字段
set @table_script = 'CREATE TABLE '+@TableName+'
('+char(13)+char(10);

--添加表中的其它字段
set @sql_cmd=N'
' --use '+@DatabaseName+'
+ @UseDB + '
set @table_script=''''
select @table_script=@table_script+
'' [''+t.NAME+''] ''
+(case when t.xusertype in (175,62,239,59,122,165,173) then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (231) and t.length=-1 then ''[ntext]''
when t.xusertype in (231) and t.length<>-1 then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (167) and t.length=-1 then ''[text]''
when t.xusertype in (167) and t.length<>-1 then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'')''
when t.xusertype in (106,108) then ''[''+p.name+''] (''+convert(varchar(30),isnull(t.prec,''''))+'',''+convert(varchar(30),isnull(t.scale,''''))+'')''
else ''[''+p.name+'']''
END)
+(case when t.isnullable=1 then '' null'' else '' not null ''end)
+(case when COLUMNPROPERTY(t.ID, t.NAME, ''ISIDENTITY'')=1 then '' identity'' else '''' end)
+'',''+char(13)+char(10)
from syscolumns t join systypes p on t.xusertype = p.xusertype
where t.ID=OBJECT_ID('''+@TableName+''')
ORDER BY t.COLID;
'
EXEc sp_executesql @sql_cmd,N'@table_script varchar(max) output',@sql_cmd output
set @table_script=@table_script+@sql_cmd
IF len(@table_script)>0
set @table_script=substring(@table_script,1,len(@table_script)-3)+char(13)+char(10)
+')'+char(13)+char(10)
+'GO'+char(13)+char(10)+char(13)+char(10)

--------------------生成索引脚本---------------------------------------
if(@ContainIndex is not null and @ContainIndex = 1)
begin
set @index_script=''
set @sql_cmd=N'
' --use '+@DatabaseName+'
+ @UseDB + '
declare @ct int
declare @indid int --当前索引ID
declare @p_indid int --前一个索引ID
select @indid=-1, @p_indid=0,@ct=0 --初始化,以后用@indid和@p_indid判断是否索引ID发生变化
set @index_script=''''
select @indid=INDID
,@index_script=@index_script
+(case when @indid<>@p_indid and @ct>0 then '')''+char(13)+char(10)+''go''+char(13)+char(10) else '''' end)
+(case when @indid<>@p_indid and UNIQ=''PRIMARY KEY''
then ''ALTER TABLE ''+TABNAME+'' ADD CONSTRAINT ''+name+'' PRIMARY KEY ''+cluster+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid<>@p_indid and UNIQ=''UNIQUE''
then ''ALTER TABLE ''+TABNAME+'' ADD CONSTRAINT ''+name+'' UNIQUE ''+cluster+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid<>@p_indid and UNIQ=''INDEX''
then ''CREATE ''+cluster+'' INDEX ''+name+'' ON ''+TABNAME+char(13)+char(10)
+''(''+char(13)+char(10)
+'' ''+COLNAME+char(13)+char(10)
when @indid=@p_indid
then '' ,''+COLNAME+char(13)+char(10)
END)
,@ct=@ct+1
,@p_indid=@indid
from
(
SELECT A.INDID,B.KEYNO
,NAME,(SELECT NAME FROM SYSOBJECTS WHERE ID=A.ID) AS TABNAME,
(SELECT NAME FROM SYSCOLUMNS WHERE ID=B.ID AND COLID=B.COLID) AS COLNAME,
(CASE WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=''UQ'') THEN ''UNIQUE''
WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=''PK'') THEN ''PRIMARY KEY''
ELSE ''INDEX'' END) AS UNIQ,
(CASE WHEN A.INDID=1 THEN ''CLUSTERED'' WHEN A.INDID>1 THEN ''NONCLUSTERED'' END) AS CLUSTER
FROM SYSINDEXES A INNER JOIN SYSINDEXKEYS B ON A.INDID=B.INDID AND A.ID=B.ID
WHERE A.ID=OBJECT_ID('''+@TableName+''') and a.indid<>0
) t
ORDER BY INDID,KEYNO'
EXEc sp_executesql @sql_cmd,N'@index_script varchar(max) output',@sql_cmd output
set @index_script=@sql_cmd
IF len(@index_script)>0
set @index_script=@index_script+')'+char(13)+char(10)+'go'+char(13)+char(10)+char(13)+char(10)
end
--生成默认值约束
if(@ContainConstraint is not null and @ContainConstraint = 1)
begin
set @sql_cmd='
' --use '+@DatabaseName+'
+ @UseDB + '
set @default_script=''''
SELECT @default_script=@default_script
+''ALTER TABLE ''+OBJECT_NAME(O.PARENT_OBJ)
+'' ADD CONSTRAINT ''+O.NAME+'' default ''+t.text+'' for ''+C.NAME+char(13)+char(10)
+''GO''+char(13)+char(10)
FROM SYSOBJECTS O INNER JOIN SYSCOMMENTS T ON O.ID=T.ID
INNER JOIN SYSCOLUMNS C ON O.PARENT_OBJ=C.ID AND C.CDEFAULT=T.ID
WHERE O.XTYPE=''D'' AND O.PARENT_OBJ=OBJECT_ID('''+@TableName+''')'
EXEc sp_executesql @sql_cmd,N'@default_script varchar(max) output',@sql_cmd output
set @default_script=@sql_cmd+char(13)+char(10)
end

set @SQL=@table_script+@index_script+@default_script
declare @len int,@n int
set @len=LEN(@SQL)
set @n=0
while(@len>0)
BEGIN
PRINT(substring(@SQL,@n*4000+1,4000));
set @n=@n+1
set @len=@len-4000;
END

select @SQL

(转)SQL SERVER 生成建表脚本的更多相关文章

  1. SQL SERVER 生成建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MSSQL] Script Date: 06/15/2012 11:59:00 ***** ...

  2. 生成建表脚本up_CreateTable

    已经很久没用使用这个脚本了,今天用到,并做修改,增加了生成扩展属性功能. Go if object_ID('[up_CreateTable]') is not null Drop Procedure ...

  3. Sql Server 删除所有表 脚本

    如果由于外键约束删除table失败,则先删除所有约束: --/第1步**********删除所有表的外键约束*************************/ DECLARE c1 cursor f ...

  4. sql server数据建表

    use edudbgoif exists(select * from sysobjects where name='department')drop table departmentcreate ta ...

  5. SQL SERVER 生成MYSQL建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_MYSQL] Script Date: 06/15/2012 13:05:14 ***** ...

  6. SQL SERVER 生成ORACLE建表脚本

    /****** Object: StoredProcedure [dbo].[GET_TableScript_ORACLE] Script Date: 06/15/2012 13:07:16 **** ...

  7. powerdesigner中将表的name在生成建表sql时生成注释

    1.为powerdesigner的表设置注释方法: powerdesigner默认没有注释: 设置方法: 选择那个表 右键- >Properties- >Columns- >Cust ...

  8. PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写

    原文:PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入T ...

  9. 【SQL】ORACLE在sqlplus中使用spool方式生成建表语句

    在实际生产中有时我们需要将一张表的数据导入到另外一张表,如果有PLSQL,我们可以通过PLSQL工具将数据导出为sql脚本,然后再在另外一个数据库中执行这个脚本.但有时在实际生产中我们没有PLSQL这 ...

随机推荐

  1. componentWillMount和componentDidMount的区别

    1.componentWillMount  将要装载,在render之前调用: componentDidMount,(装载完成),在render之后调用 2.componentWillMount  每 ...

  2. 神州数码广域网PPP封装CHAP认证配置

    实验要求:掌握PPP封装协议下的CHAP认证 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 ip a ...

  3. SpringMVC + MyBatis分库分表方案

    mybatis作为流行的ORM框架,项目实际使用过程中可能会遇到分库分表的场景.mybatis在分表,甚至是同主机下的分库都可以说是完美支持的,只需要将表名或者库名作为动态参数组装sql就能够完成.但 ...

  4. [LeetCode&Python] Problem 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. freeswitch黑名单mod_blacklist使用

    freeswitch自带黑名单模块"mod_blacklist",此文只是对该模块简单使用的实例. 最近接到客户投诉有大量骚扰电话,而从源头查不太容易,因此想到的笨方法是将投诉人加 ...

  6. vue day3 bootstrap 联动下拉

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  7. C++类中一个构造函数调用另一个构造函数

    class A { int a; int b; int c; public: A(int aa, int bb) : a(aa), b(bb),c(0) { cout << "a ...

  8. 自定义pip 包开发简单说明

    文档主要来自官方文档,主要是为了测试学习 创建pip 包项目 项目结构 ├── LICENSE ├── README.md ├── dalongrong_example_pkg │ └── __ini ...

  9. 记账本NABCD分析

    学生记账本NABCD分析 N(Need,需求) 随着我们进入大学开始逐步的扩大自己的消费水平,而我们每天无法准确的记住一笔一笔的消费记录.常常,每一个月末时我们在宿舍楼道听到不少学生抱怨这个月怎么花钱 ...

  10. SQL Agent 服务无法启动

    问题现象 从阿里云上镜像过来的一台的数据库服务器,SQL Agent服务启动不了,提示服务启动后停止. 如下是系统日志和SQL Agent的日志 SQLServerAgent could not be ...