SQL Server INSERT statement that generates data publications

Source: Internet
Author: User
Tags coding standards

The issue DB structure is published, SQL Server Manager can generate scripts, and how is the data released?

"Train of thought" generate INSERT INTO ... VALUES ... Statement

/*--=============================================--Author:yew--Create date:2012-05-18--Description: Born Insert script for data release----Testcode---------------------------EXEC [TOOL]. [Script_insert] @Schema = ' COMMON ', @Table = ' Config ', @Filter = ' [Key] like ' test% ', @Fi
     
eld0 = ' [Key] ', @Field1 = ' subkey ', @Field2 = ' Value ', @Field3 = null, @Field4 = NULL EXEC [TOOL]. [Script_insert] @Schema = ' COMMON ', @Table = ' Masterdata ', @Filter = ' Category = ' Sheetitem_prel Oadtype ', @Field0 = ' Category ', @Field1 = ' Code ', @Field2 = ' Name ', @Field3 = ' Ord ', @F IELD4 = null----History-----------------------------=============================================*/ALTER PROCEDURE [TOOL].    [Script_insert] @Schema sysname = ' dbo ', @Table sysname, @Filter sysname = null, @Field0 sysname, @Field1 sysname = null, @Field2 sysname = null, @Field3 sysname = null, @Field4 sysname = null as BEGIN SET NOCOUNT O  
     
    N --0.   Define Const DECLARE @NL varchar (2)--newline SET @NL = char (+ char) DECLARE @vSql NVARCHAR (max), @objName sysname, @fieldList sysname-1. Prepare the statement IF @Schema = ' Set @Schema = ' dbo ' Set @objName = @Sch  
     
Ema + '. ' + @Table Set @vSql = ' SET QUOTED_IDENTIFIER off; Select ' INSERT into ' + @objName + ' (' + @Field0 IF @Field1 is not NULL SET @vSql = @vSql + ', ' + @Fi Eld1 if @Field2 is not null set @vSql = @vSql + ', ' + @Field2 IF @Field3 are not null set @v  
              
    SQL = @vSql + ', ' + @Field3 IF @Field4 is not NULL SET @vSql = @vSql + ', ' + @Field4 SET @vSql = @vSql + ') ' + @NL + ' VALUES ' + QuoteName ('+ @Field0 + ', Char () ' IF @Field1 is not NULL SET @vSql = @vSql + @NL + ' + ', ' + QuoteName (IsNull ('  + @Field1 + ', ' ""), Char () ' IF @Field2 is not NULL SET @vSql = @vSql + @NL + ' + ', ' + QuoteName (IsNull (' + @Field2 + ', ""), Char () ' IF @Field3 is not NULL SET @vSql = @vSql + @NL + ' + ', ' + QuoteName (IsNull (' + @Field3 + ', ""), Char () ' IF @Field4 is not NULL SET @vSql = @vSql + @NL + ' + ', ' + QuoteName (IsNull (' + @ Field4 + ', ""), Char () ' SET @vSql = @vSql + @NL + ' + ') ' + @NL + ' from ' + @objName IF @Filte  
    R is not NULL SET @vSql = @vSql + @NL + ' WHERE ' + @Filter--2.run it PRINT @vSql EXEC (@vSql) End

"Note 1" Originally a small tool, but also spent 2 hours, not worth writing blog. But one, this tool many people need, and this is a bad cold to do it today (when I wrote this blog half, the cold disappeared), more precious.

"Note 2" for this ' sharing ':

Want to do: but I found that most of the new people do not pay much attention to coding standards, cattle are not willing to balance the function and cost, this code contains a lot of reference value;

Can do: This is a pure tool, does not involve the secret;

Can do: This tool is small enough to make it easy to speak clearly and understand.

Readers are also asked to comment on the work itself.

This article comes from "propelled" (Magic Guru) blog, please be sure to keep this source http://davyyew.blog.51cto.com/1084625/868414

Related Article

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.