Convert table data to insert statement

Source: Internet
Author: User

How to convert table data into INSERT statements when there is data in the tables

(available with SQL Server R2 test from other people's perspective)

CREATEPROC Spgeninsertsql @TableNameAsVARCHAR(100)
As
DECLARE XcursorCURSOR
For
SELECTNAME
, Xusertype
From syscolumns
WHERE(ID=object_id(@TableName))

DECLARE @F1VARCHAR(100)
DECLARE @F2INTEGER
DECLARE @SQLVARCHAR(8000)

SET @sql=' SELECT ' INSERT into '+ @TableName+' VALUES (' '

OPEN Xcursor

FETCH Xcursor
Into @F1
, @F2

While@ @FETCH_STATUS= 0
BEGIN
SET @sql= @sql++Case
When @F2Inch(
35
, 58
, 99
, 167
, 175
, 231
, 239
, 61
)
Then' + Case '+ @F1+' Is NULL and then ' ' ' ' and ' ' ' ' End + '
ELSE+
END+' Replace (ISNULL (' Cast ('+ @F1+' as varchar (8000) '), ' NULL '), ' ' ' ' ', ' ' "') '+Case
When @F2Inch(
35
, 58
, 99
, 167
, 175
, 231
, 239
, 61
)
Then' + Case '+ @F1+' Is NULL and then ' ' ' ' and ' ' ' ' End + '
ELSE+
END+CHAR(13)+‘‘‘,‘‘‘

FETCHNEXT
From Xcursor
into @F1
end

Span class= "Sqlkeyword" >close xcursor

deallocate xcursor

set @sql = left (@sql len (@sql ) -5 + '     +     ') '     from    ' + @TableName

exec (@sql )
GO EXEC spgeninsertsql tbl_message --tablename

Convert table data to insert statement

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.