Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions generate_inserts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ CREATE PROC sp_generate_inserts
@cols_to_include varchar(8000) = NULL, -- List of columns to be included in the INSERT statement
@cols_to_exclude varchar(8000) = NULL, -- List of columns to be excluded from the INSERT statement
@disable_constraints bit = 0, -- When 1, disables foreign key constraints and enables them after the INSERT statements
@ommit_computed_cols bit = 0 -- When 1, computed columns will not be included in the INSERT statement
@ommit_computed_cols bit = 0, -- When 1, computed columns will not be included in the INSERT statement
@whereClause varchar(8000) = NULL -- if only part of Table shall be exported, specify where clause excl. the WHERE

)
AS
BEGIN

/***********************************************************************************************************
Procedure: sp_generate_inserts (Build 22)
(Copyright 2002 Narayana Vyas Kondreddi. All rights reserved.)
(Copyright 2002 Narayana Vyas Kondreddi. All rights reserved.)

Purpose: To generate INSERT statements from existing data.
These INSERTS can be executed to regenerate the data at some other location.
Expand Down Expand Up @@ -198,11 +199,11 @@ ELSE

--Variable declarations
DECLARE @Column_ID int,
@Column_List varchar(8000),
@Column_List varchar(max),
@Column_Name varchar(128),
@Start_Insert varchar(786),
@Data_Type varchar(128),
@Actual_Values varchar(8000), --This is the string that will be finally executed to generate INSERT statements
@Actual_Values varchar(max), --This is the string that will be finally executed to generate INSERT statements
@IDN varchar(128) --Will contain the IDENTITY column's name in the table

--Variable Initialization
Expand Down Expand Up @@ -364,6 +365,7 @@ IF (@include_column_list <> 0)
' ''+' + '''(' + RTRIM(@Column_List) + '''+' + ''')''' +
' +''VALUES(''+ ' + @Actual_Values + '+'')''' + ' ' +
COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')

END
ELSE IF (@include_column_list = 0)
BEGIN
Expand All @@ -374,7 +376,12 @@ ELSE IF (@include_column_list = 0)
' '' +''VALUES(''+ ' + @Actual_Values + '+'')''' + ' ' +
COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')
END

if not @whereClause is null
begin
set @Actual_Values=@Actual_Values+' where '+@whereClause
PRINT 'Partial export limited by WHERE '+@whereClause
end

--Determining whether to ouput any debug information
IF @debug_mode =1
BEGIN
Expand Down