2012-08-07 153 views
2

我公司的部署過程需要用於創建過程和表等的SQL腳本。生成**裸** SQL Server中的CREATE TABLE和CREATE PROC語句

在SQL Server Management Studio中生成創建腳本給你的不僅僅是一個裸腳本,在這種情況下這是不幸的。例如,對於一個測試表這是對我產生SSMS:

USE [DemoDB] 
GO 

/****** Object: Table [dbo].[example] Script Date: 08/07/2012 15:46:32 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

CREATE TABLE [dbo].[example](
    [fake_row] [int] NULL 
) ON [PRIMARY] 

GO 

所以,我想知道的是:

我可以以某種方式運行在SSMS SQL Server上查詢那會給我一個腳本,字面上只是以下?

create table example (
    fake_row int 
) 
+3

因此,如果使用主鍵,非空約束或使用分區函數或者使用除dbo以外的模式創建表,那麼您不希望生成的腳本中包含任何這些表?你只想要沒有模式的表名,沒有其他約束的數據類型,沒有文件組信息,什麼都沒有?根據這些腳本的目的,您認爲這不會導致問題的發展嗎? – 2012-08-07 19:58:54

+2

如何最終達到這樣的要求,而不是維持原始腳本的平等要求,而不是從當前數據庫生成腳本? – 2012-08-07 20:03:10

+0

難道你不能刪除無關的信息嗎? – 2012-08-07 20:31:18

回答

3

道歉爲自我回答,標記其他好的答案+1。

事實證明,你可以在SSMS中使用實用程序的幾乎所有方式。

  1. 在對象資源管理器中右擊數據庫
  2. 點擊「任務」
  3. 點擊「生成腳本」
  4. 選擇「腳本特定對象」,然後選擇表,或任何其他
  5. 選擇「保存到文件」&「單個文件每個對象」(或只是後臺查詢窗口)
  6. 單擊「高級」,您可以啓用/禁用生成幾乎每個部分的狀態您正在生成的nts包括約束條件,使用語句等。
  7. 如果需要進一步移除(例如,刪除GO),它是最小的
1

我會創建從INFORMATION_SCHEMA視圖中讀取的存儲過程。 獲取存儲過程文本只是:

DECLARE @SQL VARCHAR(8000) 
    SET @SQL=' ' 

    select @SQL = @SQL + ROUTINE_DEFINITION 
    from INFORMATION_SCHEMA.ROUTINES 
    where ROUTINE_TYPE='PROCEDURE' 
    AND SPECIFIC_NAME = 'updateComments' 

    PRINT @SQL 

的一個腳本的表會看起來像:

DECLARE @tableName VARCHAR(50) 
SET @tableName = 'Location' 

-- Need to know how many columns 
DECLARE @NumColumns int 
SELECT @NumColumns = MAX(ORDINAL_POSITION) 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = @tableName 

DECLARE @SQL VARCHAR(8000) 
SET @SQL=' ' 

DECLARE @Results TABLE 
(LineNumber int, 
Line VARCHAR(1000)) 


INSERT INTO @Results 
    SELECT 0 AS ORDINAL_POSITION, 
    'CREATE TABLE '+TABLE_SCHEMA+'.'[email protected]+' (' AS Line 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_NAME = @tableName 

    UNION ALL 

    SELECT ORDINAL_POSITION, 
    '['+COLUMN_NAME+'] '+ DATA_TYPE + 
    CASE WHEN DATA_TYPE = 'varchar' OR DATA_TYPE = 'char' 
    THEN '('+ CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) +')' 
    ELSE '' END + 
    CASE WHEN IS_NULLABLE = 'NO' 
    THEN ' NULL ' 
    ELSE ' NOT NULL' END + 
    CASE WHEN ORDINAL_POSITION < @NumColumns 
    THEN ', ' 
    ELSE ' ' END 
    AS LINE 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = @tableName 

    UNION ALL 

    SELECT 999, ')' 


SELECT Line 
FROM @Results 
ORDER BY LineNumber 


SELECT @SQL = @SQL + Line 
FROM @Results 
ORDER BY LineNumber 

SELECT @SQL 
PRINT @SQL 

這將創建所需的簡單的表腳本,但它是不完整的 - 不會做數字精度,主鍵等,但這足以讓你開始。

我只是把它們中的每一個放到一個存儲過程中,該存儲過程將對象名稱作爲參數。

4

您可以使用information_schema(Aaron ......我知道,INFORMATION_SCHEMA;)創建您自己的「create table」語句。

我已經爲此寫了一些代碼,但它沒有使用INFORMATION_SCHEMA。我知道,如果使用較舊的系統表,可能會被刪除。它還增加了一些額外的信息(你可以刪除,因爲你不需要它)。只需將表的列表放在@INCLUSIONLIST中並在表所在的數據庫中運行。

SET @INCLUSIONLIST = '|table1|table2|'; 

SELECT (CASE WHEN rownum = 1 THEN 'CREATE TABLE ['+a.t_name+'] (' 
      WHEN c.column_id IS NOT NULL 
      THEN ' ['+c.c_name+'] ' + 
        (CASE WHEN CHARINDEX('CHAR', datatype) > 0 THEN datatype+'('+(case when length < 0 then 'max' else cast(length as varchar) end)+')' 
         WHEN CHARINDEX('BINARY', datatype) > 0 THEN datatype+'('+cast(length as varchar)+')' 
         WHEN datatype = 'float' AND precision <> 24 THEN datatype+'('+cast(precision as varchar)+')' 
         WHEN datatype IN ('numeric', 'decimal') AND scale = 0 THEN datatype+'('+cast(precision as varchar)+')' 
         WHEN datatype IN ('numeric', 'decimal') AND scale > 0 THEN datatype+'('+cast(precision as varchar)+','+cast(scale as varchar)+')' 
         ELSE datatype END)+' '+ 
        (CASE WHEN c.identity_seed IS NOT NULL 
         THEN 'IDENTITY(' + CAST(identity_seed AS VARCHAR) + ',' + CAST(identity_increment AS VARCHAR) + ') ' 
         ELSE '' END) + 
        (CASE WHEN c.is_nullable = 0 THEN 'NOT NULL ' ELSE '' END) + 
        (CASE WHEN c.default_definition IS NOT NULL 
         THEN 'DEFAULT '+default_definition ELSE '' END) + 
        (CASE WHEN max_column_id = column_id AND pk.pk_name IS NULL THEN '' ELSE ',' END) 
      WHEN rownum = max_column_id + 2 and pk.pk_name IS NOT NULL 
      THEN ' PRIMARY KEY ('+pk.pk_columns+')' 
      WHEN rownum = max_column_id + 3 THEN ') /* CREATE TABLE '+a.t_name+' */' 
      WHEN rownum = max_column_id + 4 THEN 'GO' 
      WHEN rownum = max_column_id + 5 THEN '' 
     END) 
FROM (SELECT t.t_name, rownum, max_column_id 
     FROM (SELECT t.name as t_name, MAX(c.column_id) as max_column_id 
      FROM sys.columns c join 
        (SELECT * FROM sys.tables WHERE CHARINDEX('|'+name+'|', @INCLUSIONLIST) > 0) t 
        ON c.object_id = t.object_id 
      GROUP BY t.name) t join 
      (SELECT ROW_NUMBER() OVER (ORDER BY object_id) as rownum FROM sys.columns c) ctr 
      ON ctr.rownum <= t.max_column_id + 5 
    ) a LEFT OUTER JOIN 
     (SELECT t.name as t_name, c.column_id, c.name AS c_name, u.name as datatype, 
       ISNULL(baset.name, N'') AS systemtype, 
       CAST(CASE WHEN baset.name IN (N'nchar', N'nvarchar') AND c.max_length <> -1 
         THEN c.max_length/2 ELSE c.max_length END AS INT) AS length, 
       c.precision AS precision, 
       c.scale as scale, 
       c.is_nullable, 
       dc.definition as default_definition, 
       idc.seed_value as identity_seed, idc.increment_value as identity_increment 
     FROM sys.tables t JOIN 
      sys.all_columns AS c 
      ON c.object_id = t.object_id LEFT OUTER JOIN 
      sys.types u 
      ON u.user_type_id = c.user_type_id LEFT OUTER JOIN 
      sys.types baset 
      ON baset.user_type_id = c.system_type_id AND 
       baset.user_type_id = baset.system_type_id LEFT OUTER JOIN 
      sys.default_constraints dc 
      ON c.object_id = dc.parent_object_id AND 
       c.column_id = dc.parent_column_id LEFT OUTER JOIN 
      sys.identity_columns idc 
      ON c.object_id = idc.object_id AND 
       c.column_id = idc.column_id 
    ) c 
     ON a.t_name = c.t_name AND 
     c.column_id + 1 = a.rownum LEFT OUTER JOIN 
     (SELECT t.name as t_name, kc.name as pk_name, 
       (MAX(CASE WHEN index_column_id = 1 THEN '['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 2 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 3 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 4 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 5 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 6 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 7 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 8 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 9 THEN ','+'['+c.name+']' ELSE '' END) + 
       MAX(CASE WHEN index_column_id = 10 THEN ','+'['+c.name+']' ELSE '' END) 
      ) as pk_columns 
     FROM sys.indexes i JOIN 
      sys.key_constraints kc 
      ON i.name = kc.name AND 
       kc.type = 'PK' JOIN 
      sys.tables t 
      ON i.object_id = t.object_id JOIN 
      sys.index_columns ic 
      ON i.object_id = ic.object_id AND 
      i.index_id = ic.index_id JOIN 
      sys.columns c 
      ON ic.index_column_id = c.column_id AND 
       ic.object_id = c.object_id 
     GROUP BY t.name, kc.name 
    ) pk 
     ON pk.t_name = a.t_name 
ORDER BY a.t_name, rownum