<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7332087911381398174</id><updated>2011-08-01T15:17:56.624-07:00</updated><title type='text'>Irudayaraj</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://irudaya-be.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://irudaya-be.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>irudayaraj</name><uri>http://www.blogger.com/profile/16475577521043343980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7332087911381398174.post-3366542832122267225</id><published>2009-06-16T03:02:00.001-07:00</published><updated>2009-06-16T03:02:42.902-07:00</updated><title type='text'>How to Generate Insert Script in sql server 2005</title><content type='html'>SET NOCOUNT ON&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;PRINT 'Using Master database'&lt;br /&gt;USE mytest&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;PRINT 'Checking for the existence of this procedure'&lt;br /&gt;IF (SELECT OBJECT_ID('sp_generate_inserts','P')) IS NOT NULL --means, the procedure already exists&lt;br /&gt;    BEGIN&lt;br /&gt;        PRINT 'Procedure already exists. So, dropping it'&lt;br /&gt;        DROP PROC sp_generate_inserts&lt;br /&gt;    END&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;CREATE PROC sp_generate_inserts&lt;br /&gt;(&lt;br /&gt;    @table_name varchar(776),          -- The table/view for which the INSERT statements will be generated using the existing data&lt;br /&gt;    @target_table varchar(776) = NULL,     -- Use this parameter to specify a different table name into which the data will be inserted&lt;br /&gt;    @include_column_list bit = 1,        -- Use this parameter to include/ommit column list in the generated INSERT statement&lt;br /&gt;    @from varchar(800) = NULL,         -- Use this parameter to filter the rows based on a filter condition (using WHERE)&lt;br /&gt;    @include_timestamp bit = 0,         -- Specify 1 for this parameter, if you want to include the TIMESTAMP/ROWVERSION column's data in the INSERT statement&lt;br /&gt;    @debug_mode bit = 0,            -- If @debug_mode is set to 1, the SQL statements constructed by this procedure will be printed for later examination&lt;br /&gt;    @owner varchar(64) = NULL,        -- Use this parameter if you are not the owner of the table&lt;br /&gt;    @ommit_images bit = 0,            -- Use this parameter to generate INSERT statements by omitting the 'image' columns&lt;br /&gt;    @ommit_identity bit = 0,        -- Use this parameter to ommit the identity columns&lt;br /&gt;    @top int = NULL,            -- Use this parameter to generate INSERT statements only for the TOP n rows&lt;br /&gt;    @cols_to_include varchar(8000) = NULL,    -- List of columns to be included in the INSERT statement&lt;br /&gt;    @cols_to_exclude varchar(8000) = NULL,    -- List of columns to be excluded from the INSERT statement&lt;br /&gt;    @disable_constraints bit = 0,        -- When 1, disables foreign key constraints and enables them after the INSERT statements&lt;br /&gt;    @ommit_computed_cols bit = 0        -- When 1, computed columns will not be included in the INSERT statement&lt;br /&gt;   &lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;/***********************************************************************************************************&lt;br /&gt;Procedure:    sp_generate_inserts  (Build 22)&lt;br /&gt;        (Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.)&lt;br /&gt;                                         &lt;br /&gt;Purpose:    To generate INSERT statements from existing data.&lt;br /&gt;        These INSERTS can be executed to regenerate the data at some other location.&lt;br /&gt;        This procedure is also useful to create a database setup, where in you can&lt;br /&gt;        script your data along with your table definitions.&lt;br /&gt;&lt;br /&gt;Written by:    Narayana Vyas Kondreddi&lt;br /&gt;            http://vyaskn.tripod.com&lt;br /&gt;&lt;br /&gt;Acknowledgements:&lt;br /&gt;        Divya Kalra    -- For beta testing&lt;br /&gt;        Mark Charsley    -- For reporting a problem with scripting uniqueidentifier columns with NULL values&lt;br /&gt;        Artur Zeygman    -- For helping me simplify a bit of code for handling non-dbo owned tables&lt;br /&gt;        Joris Laperre   -- For reporting a regression bug in handling text/ntext columns&lt;br /&gt;&lt;br /&gt;Tested on:     SQL Server 7.0 and SQL Server 2000 and SQL Server 2005&lt;br /&gt;&lt;br /&gt;Date created:    January 17th 2001 21:52 GMT&lt;br /&gt;&lt;br /&gt;Date modified:    May 1st 2002 19:50 GMT&lt;br /&gt;&lt;br /&gt;Email:         vyaskn@hotmail.com&lt;br /&gt;&lt;br /&gt;NOTE:        This procedure may not work with tables with too many columns.&lt;br /&gt;        Results can be unpredictable with huge text columns or SQL Server 2000's sql_variant data types&lt;br /&gt;        Whenever possible, Use @include_column_list parameter to ommit column list in the INSERT statement, for better results&lt;br /&gt;        IMPORTANT: This procedure is not tested with internation data (Extended characters or Unicode). If needed&lt;br /&gt;        you might want to convert the datatypes of character variables in this procedure to their respective unicode counterparts&lt;br /&gt;        like nchar and nvarchar&lt;br /&gt;&lt;br /&gt;        ALSO NOTE THAT THIS PROCEDURE IS NOT UPDATED TO WORK WITH NEW DATA TYPES INTRODUCED IN SQL SERVER 2005 / YUKON&lt;br /&gt;       &lt;br /&gt;&lt;br /&gt;Example 1:    To generate INSERT statements for table 'titles':&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts 'titles'&lt;br /&gt;&lt;br /&gt;Example 2:     To ommit the column list in the INSERT statement: (Column list is included by default)&lt;br /&gt;        IMPORTANT: If you have too many columns, you are advised to ommit column list, as shown below,&lt;br /&gt;        to avoid erroneous results&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts 'titles', @include_column_list = 0&lt;br /&gt;&lt;br /&gt;Example 3:    To generate INSERT statements for 'titlesCopy' table from 'titles' table:&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts 'titles', 'titlesCopy'&lt;br /&gt;&lt;br /&gt;Example 4:    To generate INSERT statements for 'titles' table for only those titles&lt;br /&gt;        which contain the word 'Computer' in them:&lt;br /&gt;        NOTE: Do not complicate the FROM or WHERE clause here. It's assumed that you are good with T-SQL if you are using this parameter&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts 'titles', @from = "from titles where title like '%Computer%'"&lt;br /&gt;&lt;br /&gt;Example 5:     To specify that you want to include TIMESTAMP column's data as well in the INSERT statement:&lt;br /&gt;        (By default TIMESTAMP column's data is not scripted)&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts 'titles', @include_timestamp = 1&lt;br /&gt;&lt;br /&gt;Example 6:    To print the debug information:&lt;br /&gt; &lt;br /&gt;        EXEC sp_generate_inserts 'titles', @debug_mode = 1&lt;br /&gt;&lt;br /&gt;Example 7:     If you are not the owner of the table, use @owner parameter to specify the owner name&lt;br /&gt;        To use this option, you must have SELECT permissions on that table&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts Nickstable, @owner = 'Nick'&lt;br /&gt;&lt;br /&gt;Example 8:     To generate INSERT statements for the rest of the columns excluding images&lt;br /&gt;        When using this otion, DO NOT set @include_column_list parameter to 0.&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts imgtable, @ommit_images = 1&lt;br /&gt;&lt;br /&gt;Example 9:     To generate INSERT statements excluding (ommiting) IDENTITY columns:&lt;br /&gt;        (By default IDENTITY columns are included in the INSERT statement)&lt;br /&gt;&lt;br /&gt;        EXEC sp_generate_inserts mytable, @ommit_identity = 1&lt;br /&gt;&lt;br /&gt;Example 10:     To generate INSERT statements for the TOP 10 rows in the table:&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts mytable, @top = 10&lt;br /&gt;&lt;br /&gt;Example 11:     To generate INSERT statements with only those columns you want:&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts titles, @cols_to_include = "'title','title_id','au_id'"&lt;br /&gt;&lt;br /&gt;Example 12:     To generate INSERT statements by omitting certain columns:&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts titles, @cols_to_exclude = "'title','title_id','au_id'"&lt;br /&gt;&lt;br /&gt;Example 13:    To avoid checking the foreign key constraints while loading data with INSERT statements:&lt;br /&gt;       &lt;br /&gt;        EXEC sp_generate_inserts titles, @disable_constraints = 1&lt;br /&gt;&lt;br /&gt;Example 14:     To exclude computed columns from the INSERT statement:&lt;br /&gt;        EXEC sp_generate_inserts MyTable, @ommit_computed_cols = 1&lt;br /&gt;***********************************************************************************************************/&lt;br /&gt;&lt;br /&gt;SET NOCOUNT ON&lt;br /&gt;&lt;br /&gt;--Making sure user only uses either @cols_to_include or @cols_to_exclude&lt;br /&gt;IF ((@cols_to_include IS NOT NULL) AND (@cols_to_exclude IS NOT NULL))&lt;br /&gt;    BEGIN&lt;br /&gt;        RAISERROR('Use either @cols_to_include or @cols_to_exclude. Do not use both the parameters at once',16,1)&lt;br /&gt;        RETURN -1 --Failure. Reason: Both @cols_to_include and @cols_to_exclude parameters are specified&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;--Making sure the @cols_to_include and @cols_to_exclude parameters are receiving values in proper format&lt;br /&gt;IF ((@cols_to_include IS NOT NULL) AND (PATINDEX('''%''',@cols_to_include) = 0))&lt;br /&gt;    BEGIN&lt;br /&gt;        RAISERROR('Invalid use of @cols_to_include property',16,1)&lt;br /&gt;        PRINT 'Specify column names surrounded by single quotes and separated by commas'&lt;br /&gt;        PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_include = "''title_id'',''title''"'&lt;br /&gt;        RETURN -1 --Failure. Reason: Invalid use of @cols_to_include property&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;IF ((@cols_to_exclude IS NOT NULL) AND (PATINDEX('''%''',@cols_to_exclude) = 0))&lt;br /&gt;    BEGIN&lt;br /&gt;        RAISERROR('Invalid use of @cols_to_exclude property',16,1)&lt;br /&gt;        PRINT 'Specify column names surrounded by single quotes and separated by commas'&lt;br /&gt;        PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_exclude = "''title_id'',''title''"'&lt;br /&gt;        RETURN -1 --Failure. Reason: Invalid use of @cols_to_exclude property&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Checking to see if the database name is specified along wih the table name&lt;br /&gt;--Your database context should be local to the table for which you want to generate INSERT statements&lt;br /&gt;--specifying the database name is not allowed&lt;br /&gt;IF (PARSENAME(@table_name,3)) IS NOT NULL&lt;br /&gt;    BEGIN&lt;br /&gt;        RAISERROR('Do not specify the database name. Be in the required database and just specify the table name.',16,1)&lt;br /&gt;        RETURN -1 --Failure. Reason: Database name is specified along with the table name, which is not allowed&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;--Checking for the existence of 'user table' or 'view'&lt;br /&gt;--This procedure is not written to work on system tables&lt;br /&gt;--To script the data in system tables, just create a view on the system tables and script the view instead&lt;br /&gt;&lt;br /&gt;IF @owner IS NULL&lt;br /&gt;    BEGIN&lt;br /&gt;        IF ((OBJECT_ID(@table_name,'U') IS NULL) AND (OBJECT_ID(@table_name,'V') IS NULL))&lt;br /&gt;            BEGIN&lt;br /&gt;                RAISERROR('User table or view not found.',16,1)&lt;br /&gt;                PRINT 'You may see this error, if you are not the owner of this table or view. In that case use @owner parameter to specify the owner name.'&lt;br /&gt;                PRINT 'Make sure you have SELECT permission on that table or view.'&lt;br /&gt;                RETURN -1 --Failure. Reason: There is no user table or view with this name&lt;br /&gt;            END&lt;br /&gt;    END&lt;br /&gt;ELSE&lt;br /&gt;    BEGIN&lt;br /&gt;        IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @table_name AND (TABLE_TYPE = 'BASE TABLE' OR TABLE_TYPE = 'VIEW') AND TABLE_SCHEMA = @owner)&lt;br /&gt;            BEGIN&lt;br /&gt;                RAISERROR('User table or view not found.',16,1)&lt;br /&gt;                PRINT 'You may see this error, if you are not the owner of this table. In that case use @owner parameter to specify the owner name.'&lt;br /&gt;                PRINT 'Make sure you have SELECT permission on that table or view.'&lt;br /&gt;                RETURN -1 --Failure. Reason: There is no user table or view with this name       &lt;br /&gt;            END&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;--Variable declarations&lt;br /&gt;DECLARE        @Column_ID int,        &lt;br /&gt;        @Column_List varchar(8000),&lt;br /&gt;        @Column_Name varchar(128),&lt;br /&gt;        @Start_Insert varchar(786),&lt;br /&gt;        @Data_Type varchar(128),&lt;br /&gt;        @Actual_Values varchar(8000),    --This is the string that will be finally executed to generate INSERT statements&lt;br /&gt;        @IDN varchar(128)        --Will contain the IDENTITY column's name in the table&lt;br /&gt;&lt;br /&gt;--Variable Initialization&lt;br /&gt;SET @IDN = ''&lt;br /&gt;SET @Column_ID = 0&lt;br /&gt;SET @Column_Name = ''&lt;br /&gt;SET @Column_List = ''&lt;br /&gt;SET @Actual_Values = ''&lt;br /&gt;&lt;br /&gt;IF @owner IS NULL&lt;br /&gt;    BEGIN&lt;br /&gt;        SET @Start_Insert = 'INSERT INTO ' + '[' + RTRIM(COALESCE(@target_table,@table_name)) + ']'&lt;br /&gt;    END&lt;br /&gt;ELSE&lt;br /&gt;    BEGIN&lt;br /&gt;        SET @Start_Insert = 'INSERT ' + '[' + LTRIM(RTRIM(@owner)) + '].' + '[' + RTRIM(COALESCE(@target_table,@table_name)) + ']'        &lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--To get the first column's ID&lt;br /&gt;&lt;br /&gt;SELECT    @Column_ID = MIN(ORDINAL_POSITION)    &lt;br /&gt;FROM    INFORMATION_SCHEMA.COLUMNS (NOLOCK)&lt;br /&gt;WHERE     TABLE_NAME = @table_name AND&lt;br /&gt;(@owner IS NULL OR TABLE_SCHEMA = @owner)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Loop through all the columns of the table, to get the column names and their data types&lt;br /&gt;WHILE @Column_ID IS NOT NULL&lt;br /&gt;    BEGIN&lt;br /&gt;        SELECT     @Column_Name = QUOTENAME(COLUMN_NAME),&lt;br /&gt;        @Data_Type = DATA_TYPE&lt;br /&gt;        FROM     INFORMATION_SCHEMA.COLUMNS (NOLOCK)&lt;br /&gt;        WHERE     ORDINAL_POSITION = @Column_ID AND&lt;br /&gt;        TABLE_NAME = @table_name AND&lt;br /&gt;        (@owner IS NULL OR TABLE_SCHEMA = @owner)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        IF @cols_to_include IS NOT NULL --Selecting only user specified columns&lt;br /&gt;        BEGIN&lt;br /&gt;            IF CHARINDEX( '''' + SUBSTRING(@Column_Name,2,LEN(@Column_Name)-2) + '''',@cols_to_include) = 0&lt;br /&gt;            BEGIN&lt;br /&gt;                GOTO SKIP_LOOP&lt;br /&gt;            END&lt;br /&gt;        END&lt;br /&gt;&lt;br /&gt;        IF @cols_to_exclude IS NOT NULL --Selecting only user specified columns&lt;br /&gt;        BEGIN&lt;br /&gt;            IF CHARINDEX( '''' + SUBSTRING(@Column_Name,2,LEN(@Column_Name)-2) + '''',@cols_to_exclude) &lt;&gt; 0&lt;br /&gt;            BEGIN&lt;br /&gt;                GOTO SKIP_LOOP&lt;br /&gt;            END&lt;br /&gt;        END&lt;br /&gt;&lt;br /&gt;        --Making sure to output SET IDENTITY_INSERT ON/OFF in case the table has an IDENTITY column&lt;br /&gt;        IF (SELECT COLUMNPROPERTY( OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name),SUBSTRING(@Column_Name,2,LEN(@Column_Name) - 2),'IsIdentity')) = 1&lt;br /&gt;        BEGIN&lt;br /&gt;            IF @ommit_identity = 0 --Determing whether to include or exclude the IDENTITY column&lt;br /&gt;                SET @IDN = @Column_Name&lt;br /&gt;            ELSE&lt;br /&gt;                GOTO SKIP_LOOP           &lt;br /&gt;        END&lt;br /&gt;       &lt;br /&gt;        --Making sure whether to output computed columns or not&lt;br /&gt;        IF @ommit_computed_cols = 1&lt;br /&gt;        BEGIN&lt;br /&gt;            IF (SELECT COLUMNPROPERTY( OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name),SUBSTRING(@Column_Name,2,LEN(@Column_Name) - 2),'IsComputed')) = 1&lt;br /&gt;            BEGIN&lt;br /&gt;                GOTO SKIP_LOOP                   &lt;br /&gt;            END&lt;br /&gt;        END&lt;br /&gt;       &lt;br /&gt;        --Tables with columns of IMAGE data type are not supported for obvious reasons&lt;br /&gt;        IF(@Data_Type in ('image'))&lt;br /&gt;            BEGIN&lt;br /&gt;                IF (@ommit_images = 0)&lt;br /&gt;                    BEGIN&lt;br /&gt;                        RAISERROR('Tables with image columns are not supported.',16,1)&lt;br /&gt;                        PRINT 'Use @ommit_images = 1 parameter to generate INSERTs for the rest of the columns.'&lt;br /&gt;                        PRINT 'DO NOT ommit Column List in the INSERT statements. If you ommit column list using @include_column_list=0, the generated INSERTs will fail.'&lt;br /&gt;                        RETURN -1 --Failure. Reason: There is a column with image data type&lt;br /&gt;                    END&lt;br /&gt;                ELSE&lt;br /&gt;                    BEGIN&lt;br /&gt;                    GOTO SKIP_LOOP&lt;br /&gt;                    END&lt;br /&gt;            END&lt;br /&gt;&lt;br /&gt;        --Determining the data type of the column and depending on the data type, the VALUES part of&lt;br /&gt;        --the INSERT statement is generated. Care is taken to handle columns with NULL values. Also&lt;br /&gt;        --making sure, not to lose any data from flot, real, money, smallmomey, datetime columns&lt;br /&gt;        SET @Actual_Values = @Actual_Values  +&lt;br /&gt;        CASE&lt;br /&gt;            WHEN @Data_Type IN ('char','varchar','nchar','nvarchar')&lt;br /&gt;                THEN&lt;br /&gt;                    'COALESCE('''''''' + REPLACE(RTRIM(' + @Column_Name + '),'''''''','''''''''''')+'''''''',''NULL'')'&lt;br /&gt;            WHEN @Data_Type IN ('datetime','smalldatetime')&lt;br /&gt;                THEN&lt;br /&gt;                    'COALESCE('''''''' + RTRIM(CONVERT(char,' + @Column_Name + ',109))+'''''''',''NULL'')'&lt;br /&gt;            WHEN @Data_Type IN ('uniqueidentifier')&lt;br /&gt;                THEN &lt;br /&gt;                    'COALESCE('''''''' + REPLACE(CONVERT(char(255),RTRIM(' + @Column_Name + ')),'''''''','''''''''''')+'''''''',''NULL'')'&lt;br /&gt;            WHEN @Data_Type IN ('text','ntext')&lt;br /&gt;                THEN &lt;br /&gt;                    'COALESCE('''''''' + REPLACE(CONVERT(char(8000),' + @Column_Name + '),'''''''','''''''''''')+'''''''',''NULL'')'                   &lt;br /&gt;            WHEN @Data_Type IN ('binary','varbinary')&lt;br /&gt;                THEN &lt;br /&gt;                    'COALESCE(RTRIM(CONVERT(char,' + 'CONVERT(int,' + @Column_Name + '))),''NULL'')' &lt;br /&gt;            WHEN @Data_Type IN ('timestamp','rowversion')&lt;br /&gt;                THEN &lt;br /&gt;                    CASE&lt;br /&gt;                        WHEN @include_timestamp = 0&lt;br /&gt;                            THEN&lt;br /&gt;                                '''DEFAULT'''&lt;br /&gt;                            ELSE&lt;br /&gt;                                'COALESCE(RTRIM(CONVERT(char,' + 'CONVERT(int,' + @Column_Name + '))),''NULL'')' &lt;br /&gt;                    END&lt;br /&gt;            WHEN @Data_Type IN ('float','real','money','smallmoney')&lt;br /&gt;                THEN&lt;br /&gt;                    'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, ' +  @Column_Name  + ',2)' + ')),''NULL'')'&lt;br /&gt;            ELSE&lt;br /&gt;                'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, ' +  @Column_Name  + ')' + ')),''NULL'')'&lt;br /&gt;        END   + '+' +  ''',''' + ' + '&lt;br /&gt;       &lt;br /&gt;        --Generating the column list for the INSERT statement&lt;br /&gt;        SET @Column_List = @Column_List +  @Column_Name + ','   &lt;br /&gt;&lt;br /&gt;        SKIP_LOOP: --The label used in GOTO&lt;br /&gt;&lt;br /&gt;        SELECT     @Column_ID = MIN(ORDINAL_POSITION)&lt;br /&gt;        FROM     INFORMATION_SCHEMA.COLUMNS (NOLOCK)&lt;br /&gt;        WHERE     TABLE_NAME = @table_name AND&lt;br /&gt;        ORDINAL_POSITION &gt; @Column_ID AND&lt;br /&gt;        (@owner IS NULL OR TABLE_SCHEMA = @owner)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    --Loop ends here!&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;--To get rid of the extra characters that got concatenated during the last run through the loop&lt;br /&gt;SET @Column_List = LEFT(@Column_List,len(@Column_List) - 1)&lt;br /&gt;SET @Actual_Values = LEFT(@Actual_Values,len(@Actual_Values) - 6)&lt;br /&gt;&lt;br /&gt;IF LTRIM(@Column_List) = ''&lt;br /&gt;    BEGIN&lt;br /&gt;        RAISERROR('No columns to select. There should at least be one column to generate the output',16,1)&lt;br /&gt;        RETURN -1 --Failure. Reason: Looks like all the columns are ommitted using the @cols_to_exclude parameter&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;--Forming the final string that will be executed, to output the INSERT statements&lt;br /&gt;IF (@include_column_list &lt;&gt; 0)&lt;br /&gt;    BEGIN&lt;br /&gt;        SET @Actual_Values =&lt;br /&gt;            'SELECT ' + &lt;br /&gt;            CASE WHEN @top IS NULL OR @top &lt; 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +&lt;br /&gt;            '''' + RTRIM(@Start_Insert) +&lt;br /&gt;            ' ''+' + '''(' + RTRIM(@Column_List) +  '''+' + ''')''' +&lt;br /&gt;            ' +''VALUES(''+ ' +  @Actual_Values  + '+'')''' + ' ' +&lt;br /&gt;            COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')&lt;br /&gt;    END&lt;br /&gt;ELSE IF (@include_column_list = 0)&lt;br /&gt;    BEGIN&lt;br /&gt;        SET @Actual_Values =&lt;br /&gt;            'SELECT ' +&lt;br /&gt;            CASE WHEN @top IS NULL OR @top &lt; 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +&lt;br /&gt;            '''' + RTRIM(@Start_Insert) +&lt;br /&gt;            ' '' +''VALUES(''+ ' +  @Actual_Values + '+'')''' + ' ' +&lt;br /&gt;            COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')&lt;br /&gt;    END   &lt;br /&gt;&lt;br /&gt;--Determining whether to ouput any debug information&lt;br /&gt;IF @debug_mode =1&lt;br /&gt;    BEGIN&lt;br /&gt;        PRINT '/*****START OF DEBUG INFORMATION*****'&lt;br /&gt;        PRINT 'Beginning of the INSERT statement:'&lt;br /&gt;        PRINT @Start_Insert&lt;br /&gt;        PRINT ''&lt;br /&gt;        PRINT 'The column list:'&lt;br /&gt;        PRINT @Column_List&lt;br /&gt;        PRINT ''&lt;br /&gt;        PRINT 'The SELECT statement executed to generate the INSERTs'&lt;br /&gt;        PRINT @Actual_Values&lt;br /&gt;        PRINT ''&lt;br /&gt;        PRINT '*****END OF DEBUG INFORMATION*****/'&lt;br /&gt;        PRINT ''&lt;br /&gt;    END&lt;br /&gt;       &lt;br /&gt;PRINT '--INSERTs generated by ''sp_generate_inserts'' stored procedure written by Vyas'&lt;br /&gt;PRINT '--Build number: 22'&lt;br /&gt;PRINT '--Problems/Suggestions? Contact Vyas @ vyaskn@hotmail.com'&lt;br /&gt;PRINT '--http://vyaskn.tripod.com'&lt;br /&gt;PRINT ''&lt;br /&gt;PRINT 'SET NOCOUNT ON'&lt;br /&gt;PRINT ''&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Determining whether to print IDENTITY_INSERT or not&lt;br /&gt;IF (@IDN &lt;&gt; '')&lt;br /&gt;    BEGIN&lt;br /&gt;        PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + QUOTENAME(@table_name) + ' ON'&lt;br /&gt;        PRINT 'GO'&lt;br /&gt;        PRINT ''&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;IF @disable_constraints = 1 AND (OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name, 'U') IS NOT NULL)&lt;br /&gt;    BEGIN&lt;br /&gt;        IF @owner IS NULL&lt;br /&gt;            BEGIN&lt;br /&gt;                SELECT     'ALTER TABLE ' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'&lt;br /&gt;            END&lt;br /&gt;        ELSE&lt;br /&gt;            BEGIN&lt;br /&gt;                SELECT     'ALTER TABLE ' + QUOTENAME(@owner) + '.' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'&lt;br /&gt;            END&lt;br /&gt;&lt;br /&gt;        PRINT 'GO'&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;PRINT ''&lt;br /&gt;PRINT 'PRINT ''Inserting values into ' + '[' + RTRIM(COALESCE(@target_table,@table_name)) + ']' + ''''&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--All the hard work pays off here!!! You'll get your INSERT statements, when the next line executes!&lt;br /&gt;EXEC (@Actual_Values)&lt;br /&gt;&lt;br /&gt;PRINT 'PRINT ''Done'''&lt;br /&gt;PRINT ''&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;IF @disable_constraints = 1 AND (OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name, 'U') IS NOT NULL)&lt;br /&gt;    BEGIN&lt;br /&gt;        IF @owner IS NULL&lt;br /&gt;            BEGIN&lt;br /&gt;                SELECT     'ALTER TABLE ' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' CHECK CONSTRAINT ALL'  AS '--Code to enable the previously disabled constraints'&lt;br /&gt;            END&lt;br /&gt;        ELSE&lt;br /&gt;            BEGIN&lt;br /&gt;                SELECT     'ALTER TABLE ' + QUOTENAME(@owner) + '.' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' CHECK CONSTRAINT ALL' AS '--Code to enable the previously disabled constraints'&lt;br /&gt;            END&lt;br /&gt;&lt;br /&gt;        PRINT 'GO'&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;PRINT ''&lt;br /&gt;IF (@IDN &lt;&gt; '')&lt;br /&gt;    BEGIN&lt;br /&gt;        PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + QUOTENAME(@table_name) + ' OFF'&lt;br /&gt;        PRINT 'GO'&lt;br /&gt;    END&lt;br /&gt;&lt;br /&gt;PRINT 'SET NOCOUNT OFF'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SET NOCOUNT OFF&lt;br /&gt;RETURN 0 --Success. We are done!&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;PRINT 'Created the procedure'&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Mark procedure as system object&lt;br /&gt;EXEC sys.sp_MS_marksystemobject sp_generate_inserts&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;PRINT 'Granting EXECUTE permission on sp_generate_inserts to all users'&lt;br /&gt;GRANT EXEC ON sp_generate_inserts TO public&lt;br /&gt;&lt;br /&gt;SET NOCOUNT OFF&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;PRINT 'Done'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7332087911381398174-3366542832122267225?l=irudaya-be.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://irudaya-be.blogspot.com/feeds/3366542832122267225/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://irudaya-be.blogspot.com/2009/06/how-to-generate-insert-script-in-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/3366542832122267225'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/3366542832122267225'/><link rel='alternate' type='text/html' href='http://irudaya-be.blogspot.com/2009/06/how-to-generate-insert-script-in-sql.html' title='How to Generate Insert Script in sql server 2005'/><author><name>irudayaraj</name><uri>http://www.blogger.com/profile/16475577521043343980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7332087911381398174.post-3960839259132238902</id><published>2009-06-16T02:57:00.000-07:00</published><updated>2009-06-16T03:10:24.763-07:00</updated><title type='text'>How to block mouse right click in your page</title><content type='html'>body tag you need to add this attribute oncontextmenu="return false"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7332087911381398174-3960839259132238902?l=irudaya-be.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://irudaya-be.blogspot.com/feeds/3960839259132238902/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://irudaya-be.blogspot.com/2009/06/how-to-block-mouse-right-click-in-your.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/3960839259132238902'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/3960839259132238902'/><link rel='alternate' type='text/html' href='http://irudaya-be.blogspot.com/2009/06/how-to-block-mouse-right-click-in-your.html' title='How to block mouse right click in your page'/><author><name>irudayaraj</name><uri>http://www.blogger.com/profile/16475577521043343980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7332087911381398174.post-4676565136551124322</id><published>2009-03-24T01:35:00.000-07:00</published><updated>2009-03-24T01:39:39.608-07:00</updated><title type='text'>How to hide ASP Server control  Using javascript</title><content type='html'>document.getElementById("label1").style.visibility='hidden';&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7332087911381398174-4676565136551124322?l=irudaya-be.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://irudaya-be.blogspot.com/feeds/4676565136551124322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://irudaya-be.blogspot.com/2009/03/how-to-hide-asp-server-control-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/4676565136551124322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7332087911381398174/posts/default/4676565136551124322'/><link rel='alternate' type='text/html' href='http://irudaya-be.blogspot.com/2009/03/how-to-hide-asp-server-control-using.html' title='How to hide ASP Server control  Using javascript'/><author><name>irudayaraj</name><uri>http://www.blogger.com/profile/16475577521043343980</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
