In MSSQL, the data in the table is exported to insert

Source: Internet
Author: User
Tags mssql rowcount

Use mastergoif exists (select name from sysobjects where name = ' Sp_generate_insert_script ') Begin drop proc Sp_generate_i                 Nsert_script print ' old version of Sp_generate_insert_script dropped ' endgocreate procedure Sp_generate_insert_script @tablename_mask varchar = Nullasbegin DECLARE @tablename varchar (DECLARE) @tablename_max VA  Rchar (+) DECLARE @tableid int DECLARE @columncount numeric (7,0) declare @columncount_max numeric (7,0) DECLARE @columnname varchar (DECLARE) @columntype int DECLARE @string varchar (DECLARE @left Part varchar (8000) DECLARE @rightpart varchar (8000) DECLARE @hasident int SET NOCOUNT ON-  -Take all tables when no mask is given (!) if (@tablename_mask is NULL) begin select @tablename_mask = '% ' End--create table Columninfo now, because it would be used several times CREATE TABLE #columninfo (num numeric (7,0) identity, name varchar(usertype smallint) select name, id into #tablenames from sysobjects where type in (' U ', ' S ') and name like @tablename_mask-loop through the table #tablenames Select @tablename_max = MAX (name), @tablen ame = MIN (name) from #tablenames while @tablename <= @tablename_max begin SELECT @tableid = ID fro M #tablenames WHERE name = @tablename if (@ @rowcount <> 0) Begin-Find out whether the table Contai      NS An identity column Select @hasident = Max (Status & 0x80) from syscolumns where id = @tableid        TRUNCATE TABLE #columninfo INSERT INTO #columninfo (name,usertype) Select name, type from syscolumns C WHERE id = @tableid and type <> PNS--do not include timestamps--Fill @leftpart Wit H The first part of the desired insert-statement, with the FieldNames select @leftpart = ' SELECT ' Insert INTO ' [Emai L protected] SElect @leftpart = @leftpart + ' (' Select @columncount = MIN (num), @columncount_max = max (num)               From #columninfo while @columncount <= @columncount_max begin Select @columnname = name,        @columntype = usertype from #columninfo where num = @columncount if (@ @rowcount <> 0) Begin IF (@columncount < @columncount_max) BEGIN Select @leftpart = @leftpart + @colum          Nname + ', ' End else begin select @leftpart = @leftpart + @columnname + ') '      End End Select @columncount = @columncount + 1 End Select @leftpart = @leftpart + ' values ' -Now fill @rightpart with the statement-retrieve the values of the fields, correctly formatted select @column Count = MIN (num), @columncount_max = max (num) from #columninfo Select @rightpart = ' W Hile @columncount <=@columncount_max begin Select @columnname = name, @columntype = usertype from #columnin          fo where num = @columncount if (@ @rowcount <> 0) begin if @columntype in (39,47) Begin Select @rightpart = @rightpart + ' + ' Select @rightpart = @rightpart + ' ISNULL (' + replica Te (char), 4) + ' +replace (' + @columnname + ', ' +replicate (char), 4) + ', ' + Replicate (char), 6) + ') + ' + Rep                                            Licate (char (), 4) + ', ' NULL ') ' end else if @columntype = 35 Begin Select @rightpart = @rightpart + ' + ' Select @rightpart = @rightpart + ' ISNULL (' + replica Te (char (+replace), 4) + ' (CONVERT (varchar), ' + @columnname + ') ' + ', ' + Replicate (char), 4) + ', ' + replicat          E (char (), 6) + ') + ' + Replicate (char (4) + ', ' NULL ') ' End else if @columntype in (58,61,111)          Begin  Select @rightpart = @rightpart + ' + ' Select @rightpart = @rightpart + ' ISNULL (' + Replicate (char (39), 4) +            ' +convert (varchar), ' + @columnname + ') + ' + Replicate (char (), 4) + ', ' NULL ') ' End else Begin Select @rightpart = @rightpart + ' + ' Select @rightpart = @rightpart + ' ISNULL (varchar            (in), ' + @columnname + '), ' NULL ') ' End if (@columncount < @columncount_max) begin Select @rightpart = @rightpart + ' + ', ' end End Select @columncount = @columncount + 1 E nd End Select @rightpart = @rightpart + ' + ') ' + ' from ' + @tablename--Order The select-statements by the fi RST column so, the same order for--different database (easy for comparisons between databases with different Creation orders) Select @rightpart = @rightpart + ' ORDER by 1 '--tables which contain an identity column we tu   RN Identity_insert ON --so we get exactly the same content if @hasident > 0 select ' SET identity_insert ' + @tablename + ' on '  EXEC (@leftpart + @rightpart) If @hasident > 0 select ' SET identity_insert ' + @tablename + ' OFF ' Select @tablename = MIN (name) from #tablenames where name > @tablename endend

Select the database to which you want to export the statement, select "Save results as File ..." in query, execute EXEC sp_generate_insert_script ' table name ', and if you do not write the table name,

Exports the contents of all tables in the database. The saved file is an INSERT SQL statement.

In MSSQL, the data in the table is exported to insert

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.