创建脚本来复制表及其内容?

时间:2010-07-20 14:01:29

标签: sql sql-server

我知道您可以使用以下命令创建一个脚本来复制表:

右键点击表>脚本表为>创建到>新的查询编辑器窗口

但是如何为表中的每一行生成一个包含一堆insert命令的脚本?

Table1
Id1, Row1
Id2, Row2
Id3, Row3

Insert into Table1 values(Row1);
Insert into Table1 values(Row2);
Insert into Table1 values(Row3);

5 个答案:

答案 0 :(得分:1)

Select
  'Insert into Table (
     IntField1
     StringField2
     Column3)
   values (' +
     IntField1 + ',' +
     + '''' + StringField2 + ''',' +
     Column2 + ')'  as InsertQuery
From Table

像这样的东西,只要记住你的字符串是否包含一个引号,你需要确保像这样替换replace(stringfield, '''', '''''')

答案 1 :(得分:1)

我最终做了这个

右键单击数据库>任务>生成脚本...>选择表>在高级选项中,我将“脚本数据类型”设置为“架构和数据”

答案 2 :(得分:0)

假设Row是INT NOT NULL。您可以编写一个输出SQL的SELECT语句;

SELECT N'INSERT INTO Table1 VALUES (' + CAST(Row AS NVARCHAR(10)) + N');'
FROM Table1

然后将结果输出到文本。

答案 3 :(得分:0)

所以这不是非常漂亮的因为我有点拿了我的sp之一并为此砍了它。但基本上这将采用任何表并将一系列插入语句打印到名为tbl_text的表中(您需要创建)

参数是表名和sysobjects中的表ID

--this is how you get the tbl_id
SELECT id FROM sysobjects WHERE type = 'U' AND name = 'tablename'


CREATE PROCEDURE dbo.sp_export_table 
@tblhdr varchar(100), 
@tblID varchar(100)
AS

SET NOCOUNT ON

IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
   DROP TABLE ##temptable
END

DECLARE @identity bit
DECLARE @typestmt nvarchar(100)
DECLARE @typeval int
DECLARE @rowstmt nvarchar(1000)
DECLARE @rowID varchar(50)
DECLARE @orderby nvarchar(100)
DECLARE @clmnstmt varchar(200)
DECLARE @clmnhdr varchar(50)
DECLARE @clmnstring varchar(1000)
DECLARE @valuestmt nvarchar(200)
DECLARE @valuestring nvarchar(3000)
DECLARE @value nvarchar(1000)
DECLARE @insertstmt varchar(1000)
DECLARE @params nvarchar(100)
DECLARE @param2 nvarchar(100)
SELECT @rowstmt = N'SELECT TOP 1 @inside_var = name FROM syscolumns WHERE id = ' + @tblID + ' ORDER BY colorder'
SELECT @params = N'@inside_var  NVARCHAR(1000) OUTPUT'
EXEC sp_executesql @rowstmt, @params, @inside_var = @orderby OUTPUT

SELECT @rowstmt = 'SELECT *, ROW_NUMBER() OVER (ORDER BY ' + @orderby + ') AS row INTO ##temptable FROM ' + @tblhdr
exec(@rowstmt)


IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
    DECLARE row_cursor CURSOR FOR
    SELECT row FROM ##temptable

    OPEN row_cursor

    FETCH NEXT FROM row_cursor
    INTO @rowID

    --if table has identity and has records write identity_insert on
    SET @identity = 0
    IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
    WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME),
    'TableHasIdentity') = 1 AND TABLE_TYPE = 'BASE TABLE'
    AND TABLE_NAME = @tblhdr) AND EXISTS(SELECT * FROM ##temptable)
    BEGIN
        SET @identity = 1
        INSERT INTO dbo.tbl_text VALUES('SET IDENTITY_INSERT dbo.' + @tblhdr + ' ON')
    END

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT @clmnstmt = 'DECLARE column_cursor CURSOR FOR SELECT name FROM syscolumns WHERE id = ' + @tblID + ' ORDER BY colorder'
        exec(@clmnstmt)

        OPEN column_cursor

        FETCH NEXT FROM column_cursor
        INTO @clmnhdr

        SELECT @clmnstring = '('
        SELECT @valuestring = '('

        WHILE @@FETCH_STATUS = 0
        BEGIN
            IF @clmnhdr <> 'row'
            BEGIN
                SELECT @clmnstring = @clmnstring + @clmnhdr + ','
                SELECT @valuestmt = N'SELECT @inside_var = ' + @clmnhdr + ' FROM ##temptable WHERE row = ' + @rowID
                EXEC sp_executesql @valuestmt, @params, @inside_var = @value OUTPUT

                SELECT @typestmt = N'SELECT @inside_var2 = xtype FROM syscolumns WHERE name = ''' + @clmnhdr + ''' AND id = ' + @tblID
                SELECT @param2 = N'@inside_var2 INT OUTPUT'
                EXEC sp_executesql @typestmt, @param2, @inside_var2 = @typeval OUTPUT

                IF @typeval NOT IN (48,52,56,59,60,62,104,108,122,127)
                BEGIN
                    SET @value = REPLACE(@value,'''','''''')
                    SET @value  = '''' + @value + ''''
                    SET @value = ISNULL(@value, '''''')
                END

                IF NOT (@typeval = 34)
                BEGIN
                    SELECT @valuestring = @valuestring + @value + ','
                END 
                ELSE
                BEGIN
                    SELECT @valuestring = @valuestring + '''''' + ','
                END
            END

            FETCH NEXT FROM column_cursor
            INTO @clmnhdr

        END

        SET @clmnstring = LEFT(@clmnstring, LEN(@clmnstring) - 1)
        SET @valuestring = LEFT(@valuestring, LEN(@valuestring) - 1)

        INSERT INTO dbo.tbl_text VALUES('INSERT INTO dbo.' + @tblhdr + ' ' + @clmnstring + ') VALUES' + @valuestring + ')')


        FETCH NEXT FROM row_cursor
        INTO @rowID

        CLOSE column_cursor
        DEALLOCATE column_cursor

    END

    --if it wrote identity_insert on, turn it off
    IF (@identity = 1)
    BEGIN
            INSERT INTO dbo.tbl_text VALUES('SET IDENTITY_INSERT dbo.' + @tblhdr + ' OFF')
    END 

    CLOSE row_cursor
    DEALLOCATE row_cursor   
END

IF object_id('tempdb..##temptable') IS NOT NULL
BEGIN
    DROP TABLE ##temptable
END

GO

答案 4 :(得分:0)

如果您有SSC帐户,可以使用我去年发布的脚本。它没有游标,可以自定义过滤。

http://www.sqlservercentral.com/scripts/Script+Data/65998/

希望这有帮助