/*
 Navicat Premium Data Transfer

 Source Server         : sql server
 Source Server Type    : SQL Server
 Source Server Version : 16001000
 Source Host           : localhost:1433
 Source Catalog        : test
 Source Schema         : dbo

 Target Server Type    : SQL Server
 Target Server Version : 16001000
 File Encoding         : 65001

 Date: 13/09/2023 14:02:51
*/


-- ----------------------------
-- Table structure for cs
-- ----------------------------
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[cs]') AND type IN ('U'))
DROP TABLE [dbo].[cs]
    GO

CREATE TABLE [dbo].[cs] (
    [a] nvarchar(max) COLLATE Chinese_PRC_CI_AS  NULL,
    [b] real  NULL,
    [c] smalldatetime  NULL,
    [d] smallint  NULL,
    [e] smallmoney  NULL,
    [f] sql_variant  NULL,
    [g] sysname  NOT NULL,
    [h] text COLLATE Chinese_PRC_CI_AS  NULL,
    [i] time(7)  NULL,
    [j] timestamp  NOT NULL
    )
    GO

ALTER TABLE [dbo].[cs] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试数据类型21-30',
    'SCHEMA', N'dbo',
    'TABLE', N'cs'
    GO


    -- ----------------------------
-- Table structure for cscs
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[cscs]') AND type IN ('U'))
DROP TABLE [dbo].[cscs]
    GO

CREATE TABLE [dbo].[cscs] (
    [q] bigint  NULL,
    [w] binary(10)  NULL,
    [e] bit  NULL,
    [t] char(1) COLLATE Chinese_PRC_CI_AS  NULL,
    [a] date  NULL,
    [s] datetime  NULL,
    [d] datetime2(7)  NULL,
    [f] datetimeoffset(7)  NULL,
    [h] datetimeoffset(7)  NULL,
    [j] decimal(18)  NULL
    )
    GO

ALTER TABLE [dbo].[cscs] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'q',
    'SCHEMA', N'dbo',
    'TABLE', N'cscs',
    'COLUMN', N'q'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'1',
    'SCHEMA', N'dbo',
    'TABLE', N'cscs',
    'COLUMN', N'w'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试字段类型1-10',
    'SCHEMA', N'dbo',
    'TABLE', N'cscs'
    GO


    -- ----------------------------
-- Table structure for cscscs
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[cscscs]') AND type IN ('U'))
DROP TABLE [dbo].[cscscs]
    GO

CREATE TABLE [dbo].[cscscs] (
    [q] geography  NULL,
    [w] geometry  NULL,
    [e] hierarchyid  NULL,
    [r] image  NULL,
    [t] int  NULL,
    [y] money  NULL,
    [u] nchar(5) COLLATE Chinese_PRC_CI_AS  NULL,
    [i] ntext COLLATE Chinese_PRC_CI_AS  NULL,
    [o] numeric(18)  NULL,
    [p] nvarchar(50) COLLATE Chinese_PRC_CI_AS  NULL
    )
    GO

ALTER TABLE [dbo].[cscscs] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试数据类型11-20',
    'SCHEMA', N'dbo',
    'TABLE', N'cscscs'
    GO


    -- ----------------------------
-- Table structure for cscscscs
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[cscscscs]') AND type IN ('U'))
DROP TABLE [dbo].[cscscscs]
    GO

CREATE TABLE [dbo].[cscscscs] (
    [q] tinyint  NULL,
    [w] uniqueidentifier  NULL,
    [e] varbinary(1)  NULL,
    [rhb] varbinary(max)  NULL,
    [ber] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
    [nn] varchar(max) COLLATE Chinese_PRC_CI_AS  NULL,
    [rn] xml  NULL
    )
    GO

ALTER TABLE [dbo].[cscscscs] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试数据类型31-39',
    'SCHEMA', N'dbo',
    'TABLE', N'cscscscs'
    GO


    -- ----------------------------
-- Table structure for customer
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[customer]') AND type IN ('U'))
DROP TABLE [dbo].[customer]
    GO

CREATE TABLE [dbo].[customer] (
    [id] int  IDENTITY(1,1) NOT NULL,
    [name] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
    [address] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
    [phone] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL
    )
    GO

ALTER TABLE [dbo].[customer] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'客户编号',
    'SCHEMA', N'dbo',
    'TABLE', N'customer',
    'COLUMN', N'id'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'客户名称',
    'SCHEMA', N'dbo',
    'TABLE', N'customer',
    'COLUMN', N'name'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'地址',
    'SCHEMA', N'dbo',
    'TABLE', N'customer',
    'COLUMN', N'address'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'电话',
    'SCHEMA', N'dbo',
    'TABLE', N'customer',
    'COLUMN', N'phone'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'客户表',
    'SCHEMA', N'dbo',
    'TABLE', N'customer'
    GO


    -- ----------------------------
-- Table structure for table_sql
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[table_sql]') AND type IN ('U'))
DROP TABLE [dbo].[table_sql]
    GO

CREATE TABLE [dbo].[table_sql] (
    [t_id] bigint  IDENTITY(1,1) NOT NULL,
    [table_sql] text COLLATE Chinese_PRC_CI_AS  NULL,
    [table_condition] text COLLATE Chinese_PRC_CI_AS  NULL,
    [table_alias] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
    [sql_key] varchar(100) COLLATE Chinese_PRC_CI_AS  NULL,
    [table_export_field] text COLLATE Chinese_PRC_CI_AS  NULL,
    [create_by] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
    [create_time] datetime  NULL,
    [update_by] varchar(50) COLLATE Chinese_PRC_CI_AS  NULL,
    [update_time] datetime  NULL,
    [del_flag] char(1) COLLATE Chinese_PRC_CI_AS  NULL
    )
    GO

ALTER TABLE [dbo].[table_sql] SET (LOCK_ESCALATION = TABLE)
    GO


    -- ----------------------------
-- Table structure for test
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type IN ('U'))
DROP TABLE [dbo].[test]
    GO

CREATE TABLE [dbo].[test] (
    [id] int  NOT NULL,
    [name] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
    [age] int  NULL,
    [address] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL
    )
    GO

ALTER TABLE [dbo].[test] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'编号',
    'SCHEMA', N'dbo',
    'TABLE', N'test',
    'COLUMN', N'id'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'姓名',
    'SCHEMA', N'dbo',
    'TABLE', N'test',
    'COLUMN', N'name'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'年龄',
    'SCHEMA', N'dbo',
    'TABLE', N'test',
    'COLUMN', N'age'
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'地址',
    'SCHEMA', N'dbo',
    'TABLE', N'test',
    'COLUMN', N'address'
    GO


    -- ----------------------------
-- Table structure for test_2
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[test_2]') AND type IN ('U'))
DROP TABLE [dbo].[test_2]
    GO

CREATE TABLE [dbo].[test_2] (
    [id] char(123) COLLATE Chinese_PRC_CI_AS  NOT NULL
    )
    GO

ALTER TABLE [dbo].[test_2] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试新增2',
    'SCHEMA', N'dbo',
    'TABLE', N'test_2'
    GO


    -- ----------------------------
-- Table structure for test_3
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[test_3]') AND type IN ('U'))
DROP TABLE [dbo].[test_3]
    GO

CREATE TABLE [dbo].[test_3] (
[id] bit  NULL
)
    GO

ALTER TABLE [dbo].[test_3] SET (LOCK_ESCALATION = TABLE)
    GO

    EXEC sp_addextendedproperty
    'MS_Description', N'测试3',
    'SCHEMA', N'dbo',
    'TABLE', N'test_3'
    GO


    -- ----------------------------
-- procedure structure for ShowCreateTable
-- ----------------------------
    IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[ShowCreateTable]') AND type IN ('P', 'PC', 'RF', 'X'))
DROP PROCEDURE[dbo].[ShowCreateTable]
GO

CREATE PROCEDURE [dbo].[ShowCreateTable] @tableName NVARCHAR(128)  
AS
BEGIN  
    SET NOCOUNT ON;  
  
    DECLARE @sql NVARCHAR(MAX);  
    DECLARE @objectId INT;  
    DECLARE @schemaName NVARCHAR(128);  
    DECLARE @tableNameWithoutSchema NVARCHAR(128);  
    DECLARE @columnList NVARCHAR(MAX);  
  
    -- 获取表名、架构名和无架构的表名  
    SET @tableNameWithoutSchema = SUBSTRING(@tableName, CHARINDEX('.', @tableName) + 1, LEN(@tableName) - CHARINDEX('.', @tableName));  
    SET @schemaName = SUBSTRING(@tableName, 1, CHARINDEX('.', @tableName) - 1);  
    SET @objectId = OBJECT_ID(@tableName);  
  
    -- 构建列列表  
SELECT @columnList = COALESCE(@columnList + ', ', '') + QUOTENAME(c.name) + ' ' + UPPER(t.name) + COALESCE('(' + CAST(c.max_length AS NVARCHAR(10)) + ')','') + COALESCE(' [' + t2.name + ']', '') + CASE WHEN c.is_nullable = 1 THEN ' NULL' ELSE ' NOT NULL' END
FROM sys.tables AS t
         JOIN sys.columns AS c ON t.object_id = c.object_id
         JOIN sys.types AS t1 ON c.user_type_id = t1.user_type_id
         JOIN sys.types AS t2 ON t1.system_type_id = t2.user_type_id
WHERE t.name = @tableNameWithoutSchema -- 表名
  AND t.type = 'U' -- 表类型为表（非系统表）
  AND c.name = @tableNameWithoutSchema -- 列名
  AND OBJECT_ID(@tableName) IS NOT NULL; -- 表存在

-- 构建 SQL 语句
SET @sql = 'CREATE TABLE ' + QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableNameWithoutSchema) + '(' + @columnList + ')';
  
    -- 执行 SQL 语句并返回结果  
EXEC sp_executesql @sql;
END;
GO


-- ----------------------------
-- procedure structure for sp_getCreateTableSql_zzx
-- ----------------------------
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_getCreateTableSql_zzx]') AND type IN ('P', 'PC', 'RF', 'X'))
	DROP PROCEDURE[dbo].[sp_getCreateTableSql_zzx]
GO

CREATE PROCEDURE [dbo].[sp_getCreateTableSql_zzx] (@table VARCHAR(100) ) AS BEGIN DECLARE @sql TABLE(s VARCHAR(1000), id INT IDENTITY);
INSERT INTO @sql(s)  VALUES('create table [' + @table + '] (');
SELECT A.name AS table_name ,B.name AS column_name ,C.value AS column_description INTO #columnsproperties FROM    sys.tables A
                                                                                                                      INNER JOIN sys.columns B ON B.object_id = A.object_id LEFT JOIN sys.extended_properties C ON C.major_id = B.object_id
    AND C.minor_id = B.column_id WHERE A.name = @table;
INSERT INTO @sql(s) SELECT  '  [' + a.COLUMN_NAME + '] ' + DATA_TYPE + COALESCE('(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR)
                                                                                    + ')', '') + ' ' + CASE WHEN EXISTS(SELECT id FROM syscolumns WHERE OBJECT_NAME(id) = @table
                                                                                                                                                    AND name = a.COLUMN_NAME  AND COLUMNPROPERTY(id, name,'IsIdentity') = 1 ) THEN 'IDENTITY(' + CAST(IDENT_SEED(@table) AS VARCHAR) + ',' + CAST(IDENT_INCR(@table) AS VARCHAR)
    + ')' ELSE '' END + ' ' + (CASE WHEN IS_NULLABLE = 'NO' THEN 'NOT ' ELSE '' END ) +'NULL ' + COALESCE('DEFAULT ' + COLUMN_DEFAULT, '')
                                + CASE WHEN ISNULL(CONVERT(VARCHAR, b.column_description), '') <> '' THEN '/**' + ISNULL(CONVERT(VARCHAR, b.column_description),
                                                                                                                         '') + '**/,'  ELSE ',' END FROM    INFORMATION_SCHEMA.COLUMNS a LEFT JOIN #columnsproperties b ON CONVERT(VARCHAR, a.COLUMN_NAME) = CONVERT(VARCHAR, b.column_name)
WHERE a.TABLE_NAME = @table  ORDER BY ORDINAL_POSITION; DECLARE @pkname VARCHAR(100); SELECT @pkname = CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                                                                      WHERE   TABLE_NAME = @table   AND CONSTRAINT_TYPE = 'PRIMARY KEY'; IF(@pkname IS NOT NULL) BEGIN INSERT  INTO @sql(s ) VALUES('  PRIMARY KEY (');
INSERT INTO @sql(s) SELECT  '   [' + COLUMN_NAME + '],' FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE   CONSTRAINT_NAME = @pkname
ORDER BY ORDINAL_POSITION; UPDATE @sql  SET s = LEFT(s, LEN(s) - 1)  WHERE id = @@identity; END; INSERT INTO @sql(s) VALUES(')');
SELECT s AS CreateTableSQL  FROM @sql ORDER BY id; END;
GO


-- ----------------------------
-- Auto increment value for customer
-- ----------------------------
DBCC CHECKIDENT ('[dbo].[customer]', RESEED, 4)
GO


-- ----------------------------
-- Primary Key structure for table customer
-- ----------------------------
ALTER TABLE [dbo].[customer] ADD CONSTRAINT [PK__customer__3213E83F9F7147CE] PRIMARY KEY CLUSTERED ([id])
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    ON [PRIMARY]
    GO


    -- ----------------------------
-- Auto increment value for table_sql
-- ----------------------------
    DBCC CHECKIDENT ('[dbo].[table_sql]', RESEED, 1)
    GO


-- ----------------------------
-- Primary Key structure for table table_sql
-- ----------------------------
ALTER TABLE [dbo].[table_sql] ADD CONSTRAINT [PK__table_sq__E579775FAE0AA88D] PRIMARY KEY CLUSTERED ([t_id])
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    ON [PRIMARY]
    GO


-- ----------------------------
-- Primary Key structure for table test
-- ----------------------------
ALTER TABLE [dbo].[test] ADD CONSTRAINT [PK__test__3213E83FE905A5E7] PRIMARY KEY CLUSTERED ([id])
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    ON [PRIMARY]
    GO


-- ----------------------------
-- Primary Key structure for table test_2
-- ----------------------------
ALTER TABLE [dbo].[test_2] ADD CONSTRAINT [PK__test_2__3213E83F56AF7291] PRIMARY KEY CLUSTERED ([id])
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
    ON [PRIMARY]
    GO

