Thursday, November 3, 2016

Best Practice for Dimensional Modeling



Best Practice for Dimensional Modeling



1. Before designing Dimensional modeling, it should be keep in mind, that Datawarehouse should be fast and secure able. No matter it is ROLAP / MOLAP or HOLAP.
2. Dimensional model either be a star schema (simple and flexible) or snow flake (More complex and normalized as compare to star).
   But in the real it is combination of both star and snow-flake galaxy schema.
3. Datawareshoue should be designed in terms of group of multiple data marts depends on different-2 business process.
4. Confirmed dimension should be handled separately, that are re-usable for all the data marts.
5. Proper relationship defined in between fact and dimensions and fact and fact, if we use degenerated dimension.
6. Use junk dimension as a consolidation of all small-2 rarely changed dimensions like flag values, gender etc.
7. Proper and truly relationship defined between attributes and level and there hierarchy.
8. More focus to define attribute relationship that can either be fixed or Regid.
9. Use calculated member instead calculated column, as calculation in calculated members are based on aggregated data as well as no need of any extra space to store.
10. Different periodic measure groups like FactWeekly, FactMonthly, FactYearly for different-2 reporting purpose.
11. Avoid snapshoting in fact table if possible, Use additive or semi-additive facts.
12. Maintain dimensions based on different-2 SCDs required. Don't go to SCD2 (Historical) always. Try to use SCD4 (Rapidly Changing dimension)
    to separate rarely changing dimension attribute with frequently changing dimension attribute.
13. Define different-2 partitions per measure group, Proper aggregation.
14. Define separate process methods based on the nature of dimenson and fact. for example date or time dimension may process separately that can either be yearly or two.
    (Process full, Process update, Process clear, Process delete)
15. Design for Process incremental instead of full. Although there is no pre-defined functionality for incremental process in SSAS, but by adding some date column and maintain
    process control table, we can configure incremental process of our cube.




Best practice for ETL


Best practice for ETL



1. Analyze source data and fix the issues at source level before start extraction.
2. Extract only needed data instead all, ETL package should be design as incremental data extraction either be based on lastmodified and submit date or based on some key.
3. Configre Parllal processing within ETL package, it can either be at package level (MaxConcurrentProcess property that max by no of processor + 2) or at Dataflow task level Engine Thread
4. Avoid to use of Asynchronous tasks that block the data to complete there operation also need more then one buffer.
5. Avoid to use implicit type casting, use Data conversion task instead.
6. Indexes should be disable before pushing data into destination table. Even clustered index should be drooped if the clustered index is not auto-generated surrogate key that save time,
   that can be rebuild or reorganize. Clustered index can only be drop and re-create.
7. Use fast load if possible, fast load is only possible if source server is also a sql server.
8. Use SqlBulk.WriteToServer, if we need to store large amout of data.
9. Use bulkCopy or bcp utility to upload the data from flat file.
10. Data-reconcilation should be done after ETL that can either be based on Checksum, binarychecksum or Hash_Byte functions.
11. Avoid use of event handling
12. Proper logging at ETL level to track failure or any performance issue. And use sql server loging.
13. Avoid to use of SCD task.



Best Parctice for Database design or Modeling



Best Practice for Database design or Modeling



1. Use singular name for DB objects.
2. Don't use any space or special character or any reserve word in DB object.
3. Don't use any prefix like tbl / sp.
4. if we need to store password or any critical information in tables then it should be encrypted.
5. Create B-tree organizational structure instead of heap.
6. Try to clustered or non-clustered index on integer column. Index on varchar column may be causes of performance hit.
7. Double check before choosing datatype for a column like for flag use bit (0/1), for pre-defined list of column value use smallint, use date instead of datetime if possible.
8. Avoid to use sql_varient datatype that is equivalent of nvarchar(8000) for outside the world.
9. Create proper indexes that should be rebuild and re-created timely.
10. Use Includes keyword during Non-clustered index creation to prevent Key Lookup operator.
11. Define primary key and foreign keys in database tables.
12. Define partitions for large tables.
13. Define different-2 filegroups, we can arrange tables there based on frequently accessed, frequently joined, on there SCD Types, based on table partitions.
14. Put extra effort to decide fill factor value, in case if we have more update statements then fill factor value should be high.
15. Statistics should be set as auto update.
16. Separate Disk for temp db, mdf files, log files and backup files
17. proper set up for database security via schema, views, logins and roles
16. Set MaxDoop property based on no of CPUs for parllel processing.
17. Arrange tables at-least upto BCNF normalization form.
18. Create maintenance plans as well as backup plans for unexpected failure.
19. use filetables instead of file system to store document or picture file.








Tuesday, October 18, 2016

Compare 2 lists in Excel




Not related to DB or Tsql...but useful for every DBA



Compare 2 lists in Excel

To find A2 is missing in Column B
=IF(ISERROR(NOT(MATCH(A2,$B$1:$B$7494,0))),A2,"")


To find B2 is missing in Column A
=IF(ISERROR(NOT(MATCH(B2,$A$1:$A$7291,0))),B2,"")





Shrink all Databases in on go


Shrink All DBs




SELECT
      'USE [' + d.name + N']' + CHAR(13) + CHAR(10)
+ 'Checkpoint;' + CHAR(13) + CHAR(10)
+ 'DBCC FREEPROCCACHE;' + CHAR(13) + CHAR(10)
+ 'GO' + CHAR(13) + CHAR(10)
+ 'DBCC SHRINKFILE (N''' + mf.name + N''' , 0, TRUNCATEONLY)' + CHAR(13) + CHAR(10)
+ 'GO'  + CHAR(13) + CHAR(10)
    + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
FROM
         sys.master_files mf
    JOIN sys.databases d
        ON mf.database_id = d.database_id
WHERE d.database_id > 4
And mf.name like '%log';






TSQL Fuzzy Lookup



I am really impressed with TSQL Fuzzy Look functionality......found somewhere during google.



-- ****************** Functions for Jaro **********************************************************************
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_GetCommonCharacters]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_GetCommonCharacters]
GO
CREATE FUNCTION [dbo].fn_GetCommonCharacters(@firstWord VARCHAR(MAX), @secondWord VARCHAR(MAX), @matchWindow INT)
RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE @CommonChars VARCHAR(MAX)
DECLARE @copy VARCHAR(MAX)
DECLARE @char CHAR(1)
DECLARE @foundIT BIT

DECLARE @f1_len INT
DECLARE @f2_len INT
DECLARE @i INT
DECLARE @j INT
DECLARE @j_Max INT

SET @CommonChars = ''
    IF @firstWord IS NOT NULL AND @secondWord IS NOT NULL
    BEGIN
SET @f1_len = LEN(@firstWord)
SET @f2_len = LEN(@secondWord)
SET @copy = @secondWord

SET @i = 1
WHILE @i < (@f1_len + 1)
BEGIN
SET @char = SUBSTRING(@firstWord, @i, 1)
SET @foundIT = 0

-- Set J starting value
IF @i - @matchWindow > 1
BEGIN
SET @j = @i - @matchWindow
END
ELSE
BEGIN
SET @j = 1
END
-- Set J stopping value
IF @i + @matchWindow <= @f2_len
BEGIN
SET @j_Max = @i + @matchWindow
END
ELSE
IF @f2_len < @i + @matchWindow
BEGIN
SET @j_Max = @f2_len
END

WHILE @j < (@j_Max + 1) AND @foundIT = 0
BEGIN
IF SUBSTRING(@copy, @j, 1) = @char
BEGIN
SET @foundIT = 1
SET @CommonChars = @CommonChars + @char
SET @copy = STUFF(@copy, @j, 1, '#')
END
SET @j = @j + 1
END
SET @i = @i + 1
END
    END

RETURN @CommonChars
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_calculateMatchWindow]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_calculateMatchWindow]
GO
CREATE FUNCTION [dbo].[fn_calculateMatchWindow](@s1_len INT, @s2_len INT)
RETURNS INT AS
BEGIN
DECLARE @matchWindow INT
SET @matchWindow = CASE WHEN @s1_len >= @s2_len
THEN (@s1_len / 2) - 1
ELSE (@s2_len / 2) - 1
END
RETURN @matchWindow
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_calculateTranspositions]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_calculateTranspositions]
GO
CREATE FUNCTION [dbo].[fn_calculateTranspositions](@s1_len INT, @str1 VARCHAR(MAX), @str2 VARCHAR(MAX))
RETURNS INT AS
BEGIN
DECLARE @transpositions INT
DECLARE @i INT

SET @transpositions = 0
SET @i = 0
WHILE @i < @s1_len
BEGIN
IF SUBSTRING(@str1, @i+1, 1) <> SUBSTRING(@str2, @i+1, 1)
BEGIN
SET @transpositions = @transpositions + 1
END
SET @i = @i + 1
END

SET @transpositions = @transpositions / 2
RETURN @transpositions
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_calculateJaro]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_calculateJaro]
GO
CREATE FUNCTION [dbo].[fn_calculateJaro](@str1 VARCHAR(MAX), @str2 VARCHAR(MAX))
RETURNS FLOAT AS
BEGIN
DECLARE @Common1 VARCHAR(MAX)
DECLARE @Common2 VARCHAR(MAX)
DECLARE @Common1_Len INT
DECLARE @Common2_Len INT
DECLARE @s1_len INT
DECLARE @s2_len INT
DECLARE @transpose_cnt INT
DECLARE @match_window INT
DECLARE @jaro_distance FLOAT
SET @transpose_cnt = 0
SET @match_window = 0
SET @jaro_distance = 0
Set @s1_len = LEN(@str1)
Set @s2_len = LEN(@str2)
SET @match_window = dbo.fn_calculateMatchWindow(@s1_len, @s2_len)
SET @Common1 = dbo.fn_GetCommonCharacters(@str1, @str2, @match_window)
SET @Common1_Len = LEN(@Common1)
IF @Common1_Len = 0 OR @Common1 IS NULL
BEGIN
RETURN 0
END
SET @Common2 = dbo.fn_GetCommonCharacters(@str2, @str1, @match_window)
SET @Common2_Len = LEN(@Common2)
IF @Common1_Len <> @Common2_Len OR @Common2 IS NULL
BEGIN
RETURN 0
END

SET @transpose_cnt = dbo.[fn_calculateTranspositions](@Common1_Len, @Common1, @Common2)
SET @jaro_distance = @Common1_Len / (3.0 * @s1_len) +
@Common1_Len / (3.0 * @s2_len) +
(@Common1_Len - @transpose_cnt) / (3.0 * @Common1_Len);

RETURN @jaro_distance
END
GO
-- ****************** Functions for Jaro **********************************************************************
-- ****************** Functions for Jaro-Winkler ***************************************************************
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_calculatePrefixLength]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_calculatePrefixLength]
GO
CREATE FUNCTION [dbo].[fn_calculatePrefixLength](@firstWord VARCHAR(MAX), @secondWord VARCHAR(MAX))
RETURNS INT As
BEGIN
DECLARE @f1_len INT
DECLARE @f2_len INT
    DECLARE @minPrefixTestLength INT
DECLARE @i INT
DECLARE @n INT
DECLARE @foundIT BIT

SET @minPrefixTestLength = 4
    IF @firstWord IS NOT NULL AND @secondWord IS NOT NULL
    BEGIN
SET @f1_len = LEN(@firstWord)
SET @f2_len = LEN(@secondWord)
SET @i = 0
SET @foundIT = 0
SET @n = CASE WHEN @minPrefixTestLength < @f1_len
AND @minPrefixTestLength < @f2_len
THEN @minPrefixTestLength
WHEN @f1_len < @f2_len
AND @f1_len < @minPrefixTestLength
THEN @f1_len
ELSE @f2_len
END
WHILE @i < @n AND @foundIT = 0
BEGIN
IF SUBSTRING(@firstWord, @i+1, 1) <> SUBSTRING(@secondWord, @i+1, 1)
BEGIN
SET @minPrefixTestLength = @i
SET @foundIT = 1
END
SET @i = @i + 1
END
END
    RETURN @minPrefixTestLength
END
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_calculateJaroWinkler]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_calculateJaroWinkler]
GO
CREATE FUNCTION [dbo].[fn_calculateJaroWinkler](@str1 VARCHAR(MAX), @str2 VARCHAR(MAX))
RETURNS float As
BEGIN
DECLARE @jaro_distance FLOAT
DECLARE @jaro_winkler_distance FLOAT
DECLARE @prefixLength INT
DECLARE @prefixScaleFactor FLOAT

SET @prefixScaleFactor = 0.1 --Constant = .1

SET @jaro_distance = dbo.fn_calculateJaro(@str1, @str2)
SET @prefixLength = dbo.fn_calculatePrefixLength(@str1, @str2)

SET @jaro_winkler_distance = @jaro_distance + ((@prefixLength * @prefixScaleFactor) * (1.0 - @jaro_distance))
RETURN @jaro_winkler_distance
END
GO
-- ****************** Functions for Jaro-Winkler ***************************************************************


-- ****************** Miscellaneous Functions *****************************************************************
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_StrClean]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_StrClean]
GO

CREATE Function [dbo].[fn_StrClean](@p_str1 VARCHAR(MAX))
RETURNS VARCHAR(MAX) as
BEGIN
 DECLARE @ret_value VARCHAR(MAX)
 SET @ret_value = @p_str1
 SET @ret_value = REPLACE(@ret_value, '.', '')
 SET @ret_value = REPLACE(@ret_value, ',', '')
 SET @ret_value = REPLACE(@ret_value, '-', '')
 SET @ret_value = REPLACE(@ret_value, ';', '')
 SET @ret_value = REPLACE(@ret_value, ':', '')

 RETURN @ret_value
END
GO
-- ****************** Miscellaneous Functions *****************************************************************



--For testing these functions run
--Example 1
declare @i float

select  @i=[dbo].[fn_calculateJaroWinkler]('Peter','Pete')

print @i

--Example 2
CREATE TABLE [dbo].[NameInput](
[Cust_Id] [nvarchar](10) NULL,
[Name_Input] [nvarchar](10) NULL,
[Last_Name] [nchar](10) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[NameLookup](
[name_group_id] [nvarchar](10) NULL,
[first_name] [nvarchar](10) NULL,
[first_name_normalized] [nchar](10) NULL
) ON [PRIMARY]

insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('11', 'Tomas', 'Jones ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('22', 'Peter', 'Jackson ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('33', 'Theresa', 'Smith ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('333', 'Therese', 'Smith ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('111', 'Tom', 'Jones ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('222', 'Pete', 'Jackson ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('55', 'Johnathan', 'Oconner ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('555', 'Johnatiag', 'Oconner ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('1111', 'Thhomas', 'Jones ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('2222', 'Petet', 'Jackson ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('3333', 'Theres', 'Smith ')
insert into NameInput (Cust_Id, Name_Input, Last_Name) values ('5555', 'Jonathan', 'Oconner ')

insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('3', 'Theresa', 'Theresa   ')
insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('4', 'Johnathan', 'Johnathan ')
insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('1', 'Tom', 'Thomas    ')
insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('2', 'Pete', 'Peter     ')
insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('1', 'Thomas', 'Thomas    ')
insert into NameLookup (name_group_id, first_name, first_name_normalized) values ('2', 'Peter', 'Peter     ')


SELECT NameLookup.name_group_id, NameInput.Cust_Id, NameInput.Name_Input, NameLookup.first_name ,NameLookup.first_name_normalized, NameInput.Last_Name
, dbo.[fn_calculateJaroWinkler](NameInput.Name_Input, NameLookup.first_name) AS Jaro3
, RANK() OVER (Partition BY NameLookup.name_group_id ORDER BY dbo.[fn_calculateJaroWinkler](NameInput.Name_Input, NameLookup.first_name) Desc) Jar02
FROM NameInput
CROSS JOIN NameLookup
Where dbo.[fn_calculateJaroWinkler](NameInput.Name_Input, NameLookup.first_name) > .95
order by NameLookup.name_group_id

--Example 3

DECLARE @SRC TABLE
(
    source_string varchar(50) NOT NULL
,   ref_id int identity(1,1) NOT NULL
);

-- Identify matches
DECLARE @WORK TABLE
(
    source_ref_id int NOT NULL
,   match_ref_id int NOT NULL
);

INSERT INTO
    @src
SELECT 'Jon Q'
UNION ALL SELECT 'John Q'
UNION ALL SELECT 'JOHN Q'
UNION ALL SELECT 'Jonn Q'
-- Oops on matching joan to jon
UNION ALL SELECT 'Joan Q'
UNION ALL SELECT 'june'
UNION ALL SELECT 'Mary W'
UNION ALL SELECT 'Marie W'
UNION ALL SELECT 'Matt H';

-- 2 problems to address
-- duplicates in our inbound set
-- duplicates against a reference set
--
-- Better matching will occur if names are split into ordinal entities
-- Splitting on whitespace is always questionable
--
-- Mat, Matt, Matthew

DECLARE CSR CURSOR
READ_ONLY
FOR
SELECT DISTINCT
    S1.source_string
,   S1.ref_id
FROM
    @SRC AS S1
ORDER BY
    S1.ref_id;

DECLARE @source_string varchar(50), @ref_id int
OPEN CSR

FETCH NEXT FROM CSR INTO @source_string, @ref_id
WHILE (@@fetch_status <> -1)
BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
        IF NOT EXISTS
        (
            SELECT * FROM @WORK W WHERE W.match_ref_id = @ref_id
        )
        BEGIN
            INSERT INTO
                @WORK
            SELECT
                @ref_id
            ,   S.ref_id
            FROM
                @src S
                -- If we have already matched the value, skip it
                LEFT OUTER JOIN
                    @WORK W
                    ON W.match_ref_id = S.ref_id
            WHERE
                -- Don't match yourself
                S.ref_id <> @ref_id
                -- arbitrary threshold, will need to examine this for sanity
                AND dbo.FuzzyLookUp(@source_string, S.source_string) > .95
        END
    END
    FETCH NEXT FROM CSR INTO @source_string, @ref_id
END

CLOSE CSR

DEALLOCATE CSR

------ Show me the list of all the unmatched rows
------ plus the retained

----;WITH MATCHES AS
----(
----    SELECT
----        S1.source_string
----    ,   S1.ref_id
----    ,   S2.source_string AS match_source_string
----    ,   S2.ref_id AS match_ref_id
----    FROM
----        @SRC S1
----        INNER JOIN
----            @WORK W
----            ON W.source_ref_id = S1.ref_id
----        INNER JOIN
----            @SRC S2
----            ON S2.ref_id = W.match_ref_id
----)
----, UNMATCHES AS
----(
----    SELECT
----        S1.source_string
----    ,   S1.ref_id
----    ,   NULL AS match_source_string
----    ,   NULL AS match_ref_id
----    FROM
----        @SRC S1
----        LEFT OUTER JOIN
----            @WORK W
----            ON W.source_ref_id = S1.ref_id
----        LEFT OUTER JOIN
----            @WORK S2
----            ON S2.match_ref_id = S1.ref_id
----    WHERE
----        W.source_ref_id IS NULL
----        and s2.match_ref_id IS NULL
----)
----SELECT
----    M.source_string
----,   M.ref_id
----,   M.match_source_string
----,   M.match_ref_id
----FROM
----    MATCHES M
----UNION ALL
----SELECT
----    M.source_string
----,   M.ref_id
----,   M.match_source_string
----,   M.match_ref_id
----FROM
----    UNMATCHES M;

------ To specifically solve your request

SELECT
    S.source_string AS Name
,   COALESCE(S2.source_string, S.source_string) As StdName
FROM
    @SRC S
    LEFT OUTER JOIN
        @WORK W
        ON W.match_ref_id = S.ref_id
    LEFT OUTER JOIN
        @SRC S2
        ON S2.ref_id = W.source_ref_id






Tuesday, March 29, 2016

Insert Statements Creator / Generator Method 2


Insert Statements Creator / Generator Method 2


Create Procedure dbo.usp_GBulkInsert1
(
@pNumOfRows Int,
@pTableName VarChar(500),
@pValueList VarChar(Max),
@pColumnList VarChar(Max) = Null,
@pIdentityInsert Bit = 0,
@pDebug Bit = 0,
@pErrorFlag Char(1) Output,
@pErrorDesp VarChar(100) Output
)
As
Begin
Set NoCount On
/*
--To Execute

Declare @lNumOfRows Int,
@lTableName VarChar(500),
@lValueList VarChar(Max),
@lColumnList VarChar(Max),
@lIdentityInsert Bit,
@lDebug Bit,
@lErrorFlag Char(1),
@lErrorDesp VarChar(100)
Select @lTableName ='Customer',
@lValueList ='ZZ,Z', --'Z, YY' --'YY, YY, 7'
@lColumnList ='', --'CustomerID, ContactName' --'CustomerID, ContactName, UniverstalID'
@lIdentityInsert =0,
@lDebug = 0
Exec usp_GInsertStatement @lTableName, @lValueList, @lColumnList, @lIdentityInsert, @lDebug, @lErrorFlag Output, @lErrorDesp Output
Select @lDebug DebugFlag, @lErrorFlag ErrorFlag, @lErrorDesp ErrorDescription

*/

Declare @lValueList VarChar(Max),
@lPointer Int,
@lValue VarChar(Max)

--To Save Transaction with TableName
If @@TranCount > 0
Exec ('Save Transaction BulkInsert' + @pTableName)
Select @lPointer = 0
While @lPointer <> @pNumOfRows
Begin
If @lPointer = 0
Begin
--To Insert Data as provided
Exec usp_GInsertStatement @pTableName, @pValueList, @pColumnList, @pIdentityInsert, @pDebug, @pErrorFlag Output, @pErrorDesp Output
If @pErrorFlag = 'Y'
GoTo ErrorHandler
End
Else
Begin
--To Insert Bulk Data
Select @lValueList = lTrim(rTrim(@pValueList)) + ' ,', @pValueList = ''
While PatIndex('%,%', @lValueList) > 0
Begin
Select @lValue = lTrim(rTrim(SubString(@lValueList, 1, (PatIndex('%,%', @lValueList) -1))))
Select @lValueList = SubString(@lValueList, (PatIndex('%,%', @lValueList) +1), Len(@lValueList))
If IsNumeric(@lValue) = 1
Select @lValue = Convert(Numeric(16,2), @lValue) + 1
Else If IsDate(@lValue) = 1
Select @lValue = DateAdd(d,1, @lValue)
Else
Select @lValue = @lValue + Char(@lPointer + 64)

Select @pValueList = @pValueList + @lValue + ' ,'
End

Exec usp_GInsertStatement @pTableName, @pValueList, @pColumnList, @pIdentityInsert, @pDebug, @pErrorFlag Output, @pErrorDesp Output
If @pErrorFlag = 'Y'
GoTo ErrorHandler
End
Select @lPointer = @lPointer + 1
End

Select @pErrorFlag = 'N', @pErrorDesp = ''
Set NoCount Off
Return
ErrorHandler:
--To Rollback Transaction
If @@TranCount > 0
Exec ('Rollback Transaction BulkInsert' + @pTableName)
Set NoCount Off
End



Insert Statements Creator / Generator Method 1




Insert Statements Creator
Method 1


CREATE Function dbo.udf_GetNoOfOccurence
(
@pSourceString VarChar(Max),
@pSearchString VarChar(Max)
)Returns Int
As
Begin
Declare @lCount Int
Select @pSourceString = @pSourceString + @pSearchString, @lCount = 0
While PatIndex('%'+ @pSearchString + '%', @pSourceString) > 0
Begin
Select @lCount = @lCount + 1
Select @pSourceString = SubString(@pSourceString, PatIndex('%'+ @pSearchString + '%', @pSourceString) + 1, Len(@pSourceString))
End
Return @lCount -1
End

Select dbo.udf_GetNoOfOccurence('R,A,B,C', ',')
*/


CREATE Procedure dbo.usp_GInsertStatement
(
@pTableName VarChar(500),
@pValueList VarChar(Max),
@pColumnList VarChar(Max) = Null,
@pIdentityInsert Bit = 0,
@pDebug Bit = 0,
@pErrorFlag Char(1) Output,
@pErrorDesp VarChar(100) Output
)
As
Begin
Set NoCount On
/*
--To Execute

Declare @lTableName VarChar(500),
@lValueList VarChar(Max),
@lColumnList VarChar(Max),
@lIdentityInsert Bit,
@lDebug Bit,
@lErrorFlag Char(1),
@lErrorDesp VarChar(100)
Select @lTableName ='State',
@lValueList ='ZZ,Z,Z', --'Z, YY' --'YY, YY, 7'
@lColumnList ='StateID, StateCode, StateName', --'CustomerID, ContactName, UniverstalID'
@lIdentityInsert =0,
@lDebug = 0
Exec usp_GInsertStatement @lTableName, @lValueList, @lColumnList, @lIdentityInsert, @lDebug, @lErrorFlag Output, @lErrorDesp Output
Select @lDebug DebugFlag, @lErrorFlag ErrorFlag, @lErrorDesp ErrorDescription

*/
Declare @lIdentityColumn VarChar(100),
@lNoOfColumns Int,
@lNoOfValues Int,
@lColumnID Int,
@lColumnDataType VarChar(500),
@lColumnList VarChar(Max),
@lValueList VarChar(Max),
@lColumnName VarChar(500),
@lValue VarChar(500),
@lIdInsertStatement VarChar(500)

--To Check TableName and ValueList is not blank.
If (lTrim(rTrim(@pTableName)) = '' Or lTrim(rTrim(@pValueList)) = '' )
Begin
Select @pErrorFlag = 'Y', @pErrorDesp = 'TableName/ ValueList can''t be blank.'
GoTo ErrorHandler
End

Select @pTableName = QuoteName(@pTableName)
--To Check Identity_Insert is On/Off
Select @lIdentityColumn = ''
Select @lIdentityColumn = Name From sys.identity_columns Where object_id = Object_ID(@pTableName)
If @lIdentityColumn <> ''
Begin
If @pIdentityInsert = 1 --Or PatIndex('%' + @lIdentityColumn + '%', @lColumnList) > 0
Begin
Select @lIdInsertStatement = 'SET IDENTITY_INSERT ' + @pTableName + ' On'
End
Else
Begin
Select @lIdInsertStatement = 'SET IDENTITY_INSERT ' + @pTableName + ' Off'
End
End
If @pDebug = 1
Select @lIdentityColumn 'IdentityColumn', @lIdInsertStatement '@lIdInsertStatement'

--To Replace single-code into double single-code from valuelist or columnlist, if exist
Select @pValueList = Replace(lTrim(rTrim(@pValueList)), '''', ''''''),
@pColumnList = Replace(lTrim(rTrim(IsNull(@pColumnList, ''))), '''', '''''')

--To Get the ColumnList, if user has not provide
If @pColumnList = ''
Begin
If (@lIdentityColumn <> '' OR @pIdentityInsert = 0)
Begin
Select @pColumnList = @pColumnList + QuoteName(Name) + ', '
From SysColumns
Where ID = Object_ID(@pTableName)
And [Name] <> @lIdentityColumn
Order By ColID
End
Else
Begin
Select @pColumnList = @pColumnList + QuoteName(Name) + ', '
From SysColumns
Where ID = Object_ID(@pTableName)
Order By ColID
End
Select @pColumnList = SubString(lTrim(rTrim(@pColumnList)), 1, Len(lTrim(rTrim(@pColumnList))) -1)
End
/*
Else
Begin
If (@lIdentityColumn <> '' And @pIdentityInsert = 0 And PatIndex('%' + @lIdentityColumn + '%', @pColumnList) >0)
Begin
Select 'Rahul', @pColumnList, @lIdentityColumn
Select @pColumnList = ' ' + @pColumnList
Select @pColumnList = SubString(@pColumnList, 1, (PatIndex('%' + @lIdentityColumn + '%', @pColumnList) -4)) +  SubString(@pColumnList, (PatIndex('%' + @lIdentityColumn + '%', @pColumnList) + Len(@lIdentityColumn) + 1), Len(@pColumnList))
End
End
*/

--Patch For Add/Remove Identity Column from ColumnList Or ValueList according to @pIdentityInsert
--===============================================================================================
Declare @lMaxValue Int,
@lDelPos Int,
@lTemp Int,
@lTempValueList VarChar(Max),
@lTempColumnList VarChar(Max)
Declare @lTblMaxValue Table
(
MaxValue Int
)

If (@lIdentityColumn <> '' And @pIdentityInsert = 0 And PatIndex('%' + @lIdentityColumn + '%', @pColumnList)>0)
Begin
--If Identity column exist in columnlist and identityinsert is set to 0, Remove identity column from columnlist and valuelist

--Remove QuoteName From ColumnList
--========================================
Select @lTempColumnList = '', @pColumnList = @pColumnList + ', '
While PatIndex('%,%', @pColumnList)> 0
Begin
If PatIndex('%]%', SubString(lTrim(rTrim(@pColumnList)), 1, (PatIndex('%,%', lTrim(rTrim(@pColumnList))) -1) )) > 0
Select @lTempColumnList = @lTempColumnList + SubString(SubString(lTrim(rTrim(@pColumnList)), 1, (PatIndex('%,%', lTrim(rTrim(@pColumnList))) -1) ), 2, (Len(SubString(lTrim(rTrim(@pColumnList)), 1, (PatIndex('%,%', lTrim(rTrim(@pColumnList))) -1) )) -2
)) + ', '
Select @pColumnList = SubString(@pColumnList, PatIndex('%,%', @pColumnList) + 1, Len(@pColumnList))
End
Select @pColumnList = SubString(@lTempColumnList, 1, Len(@lTempColumnList) -1)
--========================================

--Remove Value of ID Column From ValueList
--========================================
Select @lTemp = 1, @lTempValueList = ''
Select @lDelPos = dbo.udf_GetNoOfOccurence(SubString(@pColumnList, 1, (PatIndex('%' + @lIdentityColumn + '%', @pColumnList) - 1)), ',')
While @lTemp <= @lDelPos
Begin
Select @lTempValueList = @lTempValueList + SubString(@pValueList, 1, PatIndex('%,%', @pValueList))
Select @pValueList = SubString(@pValueList, PatIndex('%,%', @pValueList) + 1, Len(@pValueList))
Select @lTemp = @lTemp + 1
End
If PatIndex('%,%', @pValueList) > 0
Begin
Select @lTempValueList = @lTempValueList + SubString(@pValueList, PatIndex('%,%', @pValueList) + 1, Len(@pValueList))
End
Else
Begin
Select @lTempValueList = Reverse(lTrim(rTrim(@lTempValueList)))
Select @lTempValueList = Reverse(SubString(@lTempValueList, PatIndex('%,%', @lTempValueList) + 1, Len(@lTempValueList)))
End
Select @pValueList = @lTempValueList
--========================================


--Remove IdentityColumn from ColumnList
--=====================================
Select @pColumnList = SubString(@pColumnList, 1, (PatIndex('%' + @lIdentityColumn + '%', @pColumnList) - 1)) +
SubString(@pColumnList, (PatIndex('%' + @lIdentityColumn + '%', @pColumnList) + Len(@lIdentityColumn) + 1), (Len(@pColumnList) -1))
If SubString(lTrim(rTrim(Reverse(@pColumnList))), 1,1) = ','
Begin
Select @pColumnList = Reverse(SubString(lTrim(rTrim(Reverse(@pColumnList))), 2,Len(lTrim(rTrim(Reverse(@pColumnList))))))
End
Else
Begin
Select @pColumnList = lTrim(rTrim(@pColumnList))
End
--=====================================

--Add QuoteName in ColumnList
--=====================================
Select @lTempColumnList = '', @pColumnList = @pColumnList + ', '
While PatIndex('%,%', @pColumnList)> 0
Begin
Select @lTempColumnList = @lTempColumnList + QuoteName(lTrim(rTrim(SubString( @pColumnList, 1, (PatIndex('%,%', @pColumnList) -1))))) + ', '
Select @pColumnList = SubString(@pColumnList, PatIndex('%,%', @pColumnList) + 1, Len(@pColumnList))
End
Select @pColumnList = SubString(@lTempColumnList, 1, Len(@lTempColumnList) -2)
--=====================================

End
Else If (@lIdentityColumn <> '' And @pIdentityInsert = 1 And PatIndex('%' + @lIdentityColumn + '%', @pColumnList)=0)
Begin
--If Identity column not exist in columnlist and identityinsert is set to 1, Add identity column from columnlist and valuelist
Select @pColumnList = @pColumnList + ', ' + QuoteName(@lIdentityColumn)
Insert Into @lTblMaxValue(MaxValue)
Exec ('Select Case When Max(QuoteName(' + @lIdentityColumn + ')) Is Null Then 0 Else  Max(QuoteName(' + @lIdentityColumn + ')) + 1 End From ' + @pTableName)
Select @lMaxValue = MaxValue
From @lTblMaxValue
Select @pValueList = @pValueList + ', ' + Cast(@lMaxValue As VarChar)
Delete From @lTblMaxValue
End
--===============================================================================================

If @pDebug = 1
Select @pColumnList ColumnList, @pValueList ValueList, dbo.udf_GetNoOfOccurence(@pColumnList, ','), dbo.udf_GetNoOfOccurence(@pValueList, ',')
--To Check Column name or number of supplied values does not match table definition
Select @lNoOfColumns = 0, @lNoOfValues = 0
If dbo.udf_GetNoOfOccurence(@pColumnList, ',') <> dbo.udf_GetNoOfOccurence(@pValueList, ',')
Begin
Select @pErrorFlag = 'Y', @pErrorDesp = 'Column name or number of supplied values does not match table definition.'
GoTo ErrorHandler
End

--To Update the ValuList accourding to Insert statement
Select @lColumnDataType = '', @lColumnList = @pColumnList + ',', @lValueList = @pValueList + ',', @lColumnName = '', @pValueList = ''

While PatIndex('%,%',@lColumnList) > 0
Begin
Select @lColumnName = SubString(@lColumnList, 1, (PatIndex('%,%', @lColumnList) -1))
Select @lColumnList = SubString(@lColumnList, (PatIndex('%,%', @lColumnList) +1), Len(@lColumnList))

IF PatIndex('%]%', @lColumnName) > 0
Begin
Select @lColumnName = SubString(lTrim(rTrim(@lColumnName)), 2, Len(lTrim(rTrim(@lColumnName))) -2)
If @pDebug = 1
Select @lColumnName 'ColumnName'
End

Select @lValue = SubString(@lValueList, 1, (PatIndex('%,%', @lValueList) -1))
Select @lValueList = SubString(@lValueList, (PatIndex('%,%', @lValueList) +1), Len(@lValueList))

Select @lColumnDataType = lTrim(rTrim(ST.[Name]))
From SysColumns SC
Inner Join SysTypes ST On ST.XUsertype = SC.Xtype
Where Id = Object_ID(lTrim(rTrim(@pTableName)))
And SC.[Name] = lTrim(rTrim(@lColumnName))

If @pDebug = 1
Select @lColumnDataType DataType, lTrim(rTrim(@lColumnName)) ColumnName, lTrim(rTrim(@pTableName)) TableName

Select @pValueList = @pValueList +
CASE
WHEN @lColumnDataType IN ('char','varchar','nchar','nvarchar')
THEN
--'REPLACE(RTRIM(' + @lValue + '),'''''''','''''''''''')'
'REPLACE(RTRIM(''' + @lValue + '''),'''''''','''''''''''')'
WHEN @lColumnDataType IN ('datetime','smalldatetime')
THEN
'RTRIM(CONVERT(char,''' + @lValue + ''',112))'
WHEN @lColumnDataType IN ('uniqueidentifier')
THEN
'REPLACE(CONVERT(char(255),RTRIM(' + @lValue + ')),'''''''','''''''''''')'
WHEN @lColumnDataType IN ('text','ntext')
THEN
'REPLACE(CONVERT(char(8000),''' + @lValue + '''),'''''''','''''''''''')'
WHEN @lColumnDataType IN ('binary','varbinary')
THEN
'RTRIM(CONVERT(char,''' + 'CONVERT(int,''' + @lValue + ''')''))'
WHEN @lColumnDataType IN ('float','real','money','smallmoney')
THEN
'LTRIM(RTRIM(' + 'CONVERT(char, ''' +  @lValue  + ''',2)' + '))'
ELSE
'LTRIM(RTRIM(' + 'CONVERT(char, ''' +  @lValue  + ''')' + '))'
END +
', '
End
Select @pValueList = SubString(@pValueList, 1, Len(@pValueList) -2)

If @pDebug = 1
Select @lIdInsertStatement, 'Insert Into ' + @pTableName + ' (' + @pColumnList + ') Values( ' + @pValueList + ') '

--To Execute Insert Statement
If @lIdentityColumn <> ''
Begin
Exec(
@lIdInsertStatement + ' ' +
'Insert Into ' + @pTableName + ' (' + @pColumnList + ') Values( ' + @pValueList + ') '
)
If @@Error > 0
Begin
Select @pErrorFlag = 'Y', @pErrorDesp = 'Error on Executing Insert Statement.'
GoTo ErrorHandler
End
End
Else
Begin
Exec ('Insert Into ' + @pTableName + ' (' + @pColumnList + ') Values( ' + @pValueList + ') ')
If @@Error > 0
Begin
Select @pErrorFlag = 'Y', @pErrorDesp = 'Error on Executing Insert Statement.'
GoTo ErrorHandler
End
End

Select @pErrorFlag = 'N', @pErrorDesp = ''
Set NoCount Off
Return
ErrorHandler:

Set NoCount Off
End




Insert Statements Generator



Generate or Create Insert statements from a table


Purpose:    To generate INSERT statements from existing data.
        These INSERTS can be executed to regenerate the data at some other location.
        This procedure is also useful to create a database setup, where in you can
        script your data along with your table definitions.



SET NOCOUNT ON
GO

PRINT 'Using Master database'
USE master
GO

PRINT 'Checking for the existence of this procedure'
IF (SELECT OBJECT_ID('sp_generate_inserts','P')) IS NOT NULL --means, the procedure already exists
    BEGIN
        PRINT 'Procedure already exists. So, dropping it'
        DROP PROC sp_generate_inserts
    END
GO

CREATE PROC sp_generate_inserts
(
    @table_name varchar(776),       -- The table/view for which the INSERT statements will be generated using the existing data
    @target_table varchar(776) = NULL,  -- Use this parameter to specify a different table name into which the data will be inserted
    @include_column_list bit = 1,       -- Use this parameter to include/ommit column list in the generated INSERT statement
    @from varchar(800) = NULL,      -- Use this parameter to filter the rows based on a filter condition (using WHERE)
    @include_timestamp bit = 0,         -- Specify 1 for this parameter, if you want to include the TIMESTAMP/ROWVERSION column's data in the INSERT statement
    @debug_mode bit = 0,            -- If @debug_mode is set to 1, the SQL statements constructed by this procedure will be printed for later examination
    @owner varchar(64) = NULL,      -- Use this parameter if you are not the owner of the table
    @ommit_images bit = 0,          -- Use this parameter to generate INSERT statements by omitting the 'image' columns
    @ommit_identity bit = 0,        -- Use this parameter to ommit the identity columns
    @top int = NULL,            -- Use this parameter to generate INSERT statements only for the TOP n rows
    @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

)
AS
BEGIN

/*******************************

Example 1:  To generate INSERT statements for table 'titles':

        EXEC sp_generate_inserts 'titles'

Example 2:  To ommit the column list in the INSERT statement: (Column list is included by default)
        IMPORTANT: If you have too many columns, you are advised to ommit column list, as shown below,
        to avoid erroneous results

        EXEC sp_generate_inserts 'titles', @include_column_list = 0

Example 3:  To generate INSERT statements for 'titlesCopy' table from 'titles' table:

        EXEC sp_generate_inserts 'titles', 'titlesCopy'

Example 4:  To generate INSERT statements for 'titles' table for only those titles
        which contain the word 'Computer' in them:
        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

        EXEC sp_generate_inserts 'titles', @from = "from titles where title like '%Computer%'"

Example 5:  To specify that you want to include TIMESTAMP column's data as well in the INSERT statement:
        (By default TIMESTAMP column's data is not scripted)

        EXEC sp_generate_inserts 'titles', @include_timestamp = 1

Example 6:  To print the debug information:

        EXEC sp_generate_inserts 'titles', @debug_mode = 1

Example 7:  If you are not the owner of the table, use @owner parameter to specify the owner name
        To use this option, you must have SELECT permissions on that table

        EXEC sp_generate_inserts Nickstable, @owner = 'Nick'

Example 8:  To generate INSERT statements for the rest of the columns excluding images
        When using this otion, DO NOT set @include_column_list parameter to 0.

        EXEC sp_generate_inserts imgtable, @ommit_images = 1

Example 9:  To generate INSERT statements excluding (ommiting) IDENTITY columns:
        (By default IDENTITY columns are included in the INSERT statement)

        EXEC sp_generate_inserts mytable, @ommit_identity = 1

Example 10:     To generate INSERT statements for the TOP 10 rows in the table:

        EXEC sp_generate_inserts mytable, @top = 10

Example 11:     To generate INSERT statements with only those columns you want:

        EXEC sp_generate_inserts titles, @cols_to_include = "'title','title_id','au_id'"

Example 12:     To generate INSERT statements by omitting certain columns:

        EXEC sp_generate_inserts titles, @cols_to_exclude = "'title','title_id','au_id'"

Example 13: To avoid checking the foreign key constraints while loading data with INSERT statements:

        EXEC sp_generate_inserts titles, @disable_constraints = 1

Example 14:     To exclude computed columns from the INSERT statement:
        EXEC sp_generate_inserts MyTable, @ommit_computed_cols = 1
****************************/

SET NOCOUNT ON

--Making sure user only uses either @cols_to_include or @cols_to_exclude
IF ((@cols_to_include IS NOT NULL) AND (@cols_to_exclude IS NOT NULL))
    BEGIN
        RAISERROR('Use either @cols_to_include or @cols_to_exclude. Do not use both the parameters at once',16,1)
        RETURN -1 --Failure. Reason: Both @cols_to_include and @cols_to_exclude parameters are specified
    END

--Making sure the @cols_to_include and @cols_to_exclude parameters are receiving values in proper format
IF ((@cols_to_include IS NOT NULL) AND (PATINDEX('''%''',@cols_to_include) = 0))
    BEGIN
        RAISERROR('Invalid use of @cols_to_include property',16,1)
        PRINT 'Specify column names surrounded by single quotes and separated by commas'
        PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_include = "''title_id'',''title''"'
        RETURN -1 --Failure. Reason: Invalid use of @cols_to_include property
    END

IF ((@cols_to_exclude IS NOT NULL) AND (PATINDEX('''%''',@cols_to_exclude) = 0))
    BEGIN
        RAISERROR('Invalid use of @cols_to_exclude property',16,1)
        PRINT 'Specify column names surrounded by single quotes and separated by commas'
        PRINT 'Eg: EXEC sp_generate_inserts titles, @cols_to_exclude = "''title_id'',''title''"'
        RETURN -1 --Failure. Reason: Invalid use of @cols_to_exclude property
    END

--Checking to see if the database name is specified along wih the table name
--Your database context should be local to the table for which you want to generate INSERT statements
--specifying the database name is not allowed
IF (PARSENAME(@table_name,3)) IS NOT NULL
    BEGIN
        RAISERROR('Do not specify the database name. Be in the required database and just specify the table name.',16,1)
        RETURN -1 --Failure. Reason: Database name is specified along with the table name, which is not allowed
    END

--Checking for the existence of 'user table' or 'view'
--This procedure is not written to work on system tables
--To script the data in system tables, just create a view on the system tables and script the view instead

IF @owner IS NULL
    BEGIN
        IF ((OBJECT_ID(@table_name,'U') IS NULL) AND (OBJECT_ID(@table_name,'V') IS NULL))
            BEGIN
                RAISERROR('User table or view not found.',16,1)
                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.'
                PRINT 'Make sure you have SELECT permission on that table or view.'
                RETURN -1 --Failure. Reason: There is no user table or view with this name
            END
    END
ELSE
    BEGIN
        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)
            BEGIN
                RAISERROR('User table or view not found.',16,1)
                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.'
                PRINT 'Make sure you have SELECT permission on that table or view.'
                RETURN -1 --Failure. Reason: There is no user table or view with this name
            END
    END

--Variable declarations
DECLARE     @Column_ID int,
        @Column_List varchar(8000),
        @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
        @IDN varchar(128)       --Will contain the IDENTITY column's name in the table

--Variable Initialization
SET @IDN = ''
SET @Column_ID = 0
SET @Column_Name = ''
SET @Column_List = ''
SET @Actual_Values = ''

IF @owner IS NULL
    BEGIN
        SET @Start_Insert = 'INSERT INTO ' + '[' + RTRIM(COALESCE(@target_table,@table_name)) + ']'
    END
ELSE
    BEGIN
        SET @Start_Insert = 'INSERT ' + '[' + LTRIM(RTRIM(@owner)) + '].' + '[' + RTRIM(COALESCE(@target_table,@table_name)) + ']'
    END

--To get the first column's ID

SELECT  @Column_ID = MIN(ORDINAL_POSITION)
FROM    INFORMATION_SCHEMA.COLUMNS (NOLOCK)
WHERE   TABLE_NAME = @table_name AND
(@owner IS NULL OR TABLE_SCHEMA = @owner)

--Loop through all the columns of the table, to get the column names and their data types
WHILE @Column_ID IS NOT NULL
    BEGIN
        SELECT  @Column_Name = QUOTENAME(COLUMN_NAME),
        @Data_Type = DATA_TYPE
        FROM    INFORMATION_SCHEMA.COLUMNS (NOLOCK)
        WHERE   ORDINAL_POSITION = @Column_ID AND
        TABLE_NAME = @table_name AND
        (@owner IS NULL OR TABLE_SCHEMA = @owner)

        IF @cols_to_include IS NOT NULL --Selecting only user specified columns
        BEGIN
            IF CHARINDEX( '''' + SUBSTRING(@Column_Name,2,LEN(@Column_Name)-2) + '''',@cols_to_include) = 0
            BEGIN
                GOTO SKIP_LOOP
            END
        END

        IF @cols_to_exclude IS NOT NULL --Selecting only user specified columns
        BEGIN
            IF CHARINDEX( '''' + SUBSTRING(@Column_Name,2,LEN(@Column_Name)-2) + '''',@cols_to_exclude) <> 0
            BEGIN
                GOTO SKIP_LOOP
            END
        END

        --Making sure to output SET IDENTITY_INSERT ON/OFF in case the table has an IDENTITY column
        IF (SELECT COLUMNPROPERTY( OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name),SUBSTRING(@Column_Name,2,LEN(@Column_Name) - 2),'IsIdentity')) = 1
        BEGIN
            IF @ommit_identity = 0 --Determing whether to include or exclude the IDENTITY column
                SET @IDN = @Column_Name
            ELSE
                GOTO SKIP_LOOP
        END

        --Making sure whether to output computed columns or not
        IF @ommit_computed_cols = 1
        BEGIN
            IF (SELECT COLUMNPROPERTY( OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name),SUBSTRING(@Column_Name,2,LEN(@Column_Name) - 2),'IsComputed')) = 1
            BEGIN
                GOTO SKIP_LOOP
            END
        END

        --Tables with columns of IMAGE data type are not supported for obvious reasons
        IF(@Data_Type in ('image'))
            BEGIN
                IF (@ommit_images = 0)
                    BEGIN
                        RAISERROR('Tables with image columns are not supported.',16,1)
                        PRINT 'Use @ommit_images = 1 parameter to generate INSERTs for the rest of the columns.'
                        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.'
                        RETURN -1 --Failure. Reason: There is a column with image data type
                    END
                ELSE
                    BEGIN
                    GOTO SKIP_LOOP
                    END
            END

        --Determining the data type of the column and depending on the data type, the VALUES part of
        --the INSERT statement is generated. Care is taken to handle columns with NULL values. Also
        --making sure, not to lose any data from flot, real, money, smallmomey, datetime columns
        SET @Actual_Values = @Actual_Values  +
        CASE
            WHEN @Data_Type IN ('char','varchar','nchar','nvarchar')
                THEN
                    'COALESCE('''''''' + REPLACE(RTRIM(' + @Column_Name + '),'''''''','''''''''''')+'''''''',''NULL'')'
            WHEN @Data_Type IN ('datetime','smalldatetime') AND @Column_Name <> '[created_dt]'
                THEN
                    'COALESCE('''''''' + RTRIM(CONVERT(char,' + @Column_Name + ',121))+'''''''',''NULL'')'
            WHEN @Data_Type IN ('datetime','smalldatetime') AND @Column_Name = '[created_dt]'
                THEN
                    '''GETDATE()'''
            WHEN @Data_Type IN ('uniqueidentifier')
                THEN
                    'COALESCE('''''''' + REPLACE(CONVERT(char(255),RTRIM(' + @Column_Name + ')),'''''''','''''''''''')+'''''''',''NULL'')'
            WHEN @Data_Type IN ('text','ntext')
                THEN
                    'COALESCE('''''''' + REPLACE(CONVERT(char(8000),' + @Column_Name + '),'''''''','''''''''''')+'''''''',''NULL'')'
            WHEN @Data_Type IN ('binary','varbinary')
                THEN
                    'COALESCE(RTRIM(CONVERT(char,' + 'CONVERT(int,' + @Column_Name + '))),''NULL'')'
            WHEN @Data_Type IN ('timestamp','rowversion')
                THEN
                    CASE
                        WHEN @include_timestamp = 0
                            THEN
                                '''DEFAULT'''
                            ELSE
                                'COALESCE(RTRIM(CONVERT(char,' + 'CONVERT(int,' + @Column_Name + '))),''NULL'')'
                    END
            WHEN @Data_Type IN ('float','real','money','smallmoney')
                THEN
                    'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, ' +  @Column_Name  + ',2)' + ')),''NULL'')'
            ELSE
                'COALESCE(LTRIM(RTRIM(' + 'CONVERT(char, ' +  @Column_Name  + ')' + ')),''NULL'')'
        END   + '+' +  ''',''' + ' + '

        --Generating the column list for the INSERT statement
        SET @Column_List = @Column_List +  @Column_Name + ','    

        SKIP_LOOP: --The label used in GOTO

        SELECT  @Column_ID = MIN(ORDINAL_POSITION)
        FROM    INFORMATION_SCHEMA.COLUMNS (NOLOCK)
        WHERE   TABLE_NAME = @table_name AND
        ORDINAL_POSITION > @Column_ID AND
        (@owner IS NULL OR TABLE_SCHEMA = @owner)

    --Loop ends here!
    END

--To get rid of the extra characters that got concatenated during the last run through the loop
SET @Column_List = LEFT(@Column_List,len(@Column_List) - 1)
SET @Actual_Values = LEFT(@Actual_Values,len(@Actual_Values) - 6)

IF LTRIM(@Column_List) = ''
    BEGIN
        RAISERROR('No columns to select. There should at least be one column to generate the output',16,1)
        RETURN -1 --Failure. Reason: Looks like all the columns are ommitted using the @cols_to_exclude parameter
    END

--Forming the final string that will be executed, to output the INSERT statements
/*
IF (@include_column_list <> 0)
    BEGIN
        SET @Actual_Values =
            'SELECT ' +
            CASE WHEN @top IS NULL OR @top < 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +
            '''' + RTRIM(@Start_Insert) +
            ' ''+' + '''(' + 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
        SET @Actual_Values =
            'SELECT ' +
            CASE WHEN @top IS NULL OR @top < 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +
            '''' + RTRIM(@Start_Insert) +
            ' '' +''VALUES(''+ ' +  @Actual_Values + '+'')''' + ' ' +
            COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')
    END
*/

IF (@include_column_list <> 0)
    BEGIN
        SET @Actual_Values =
            'SELECT ' +
            CASE WHEN @top IS NULL OR @top < 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +
            '''' + 'SELECT ''+' +
            --' ''+' + '''(' + RTRIM(@Column_List) +  '''+' + ''')''' +
            +  @Actual_Values  + '+'' UNION ALL ''' + ' ' +
            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
        SET @Actual_Values =
            'SELECT ' +
            CASE WHEN @top IS NULL OR @top < 0 THEN '' ELSE ' TOP ' + LTRIM(STR(@top)) + ' ' END +
            '''' + 'SELECT ''+' +
            + @Actual_Values + '+'' UNION ALL ''' + ' ' +
            COALESCE(@from,' FROM ' + CASE WHEN @owner IS NULL THEN '' ELSE '[' + LTRIM(RTRIM(@owner)) + '].' END + '[' + rtrim(@table_name) + ']' + '(NOLOCK)')
    END
--Determining whether to ouput any debug information
IF @debug_mode =1
    BEGIN
        PRINT '/*****START OF DEBUG INFORMATION*****'
        PRINT 'Beginning of the INSERT statement:'
        PRINT @Start_Insert
        PRINT ''
        PRINT 'The column list:'
        PRINT @Column_List
        PRINT ''
        PRINT 'The SELECT statement executed to generate the INSERTs'
        PRINT @Actual_Values
        PRINT ''
        PRINT '*****END OF DEBUG INFORMATION*****/'
        PRINT ''
    END

--Determining whether to print IDENTITY_INSERT or not

IF (@IDN <> '')
    BEGIN
        PRINT 'DELETE FROM ' + @table_name
        PRINT 'DBCC CHECKIDENT('+@table_name+', RESEED, 0)'
        PRINT ''
        PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + QUOTENAME(@table_name) + ' ON'
        PRINT 'GO'
        PRINT ''
    END

IF @disable_constraints = 1 AND (OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name, 'U') IS NOT NULL)
    BEGIN
        IF @owner IS NULL
            BEGIN
                SELECT  'ALTER TABLE ' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'
            END
        ELSE
            BEGIN
                SELECT  'ALTER TABLE ' + QUOTENAME(@owner) + '.' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' NOCHECK CONSTRAINT ALL' AS '--Code to disable constraints temporarily'
            END

        PRINT 'GO'
    END

--All the hard work pays off here!!! You'll get your INSERT statements, when the next line executes!

SET @Column_Name = ''
SELECT @Column_Name = COALESCE(@Column_Name, '') + ISNULL(',' + COLUMN_NAME, '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table_name
SELECT @Column_Name = STUFF(@Column_Name, 1,1, '')

SELECT 'INSERT INTO ' + @table_name + '(' + @Column_Name + ')'

EXEC (@Actual_Values)

IF @disable_constraints = 1 AND (OBJECT_ID(QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + @table_name, 'U') IS NOT NULL)
    BEGIN
        IF @owner IS NULL
            BEGIN
                SELECT  'ALTER TABLE ' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' CHECK CONSTRAINT ALL'  AS '--Code to enable the previously disabled constraints'
            END
        ELSE
            BEGIN
                SELECT  'ALTER TABLE ' + QUOTENAME(@owner) + '.' + QUOTENAME(COALESCE(@target_table, @table_name)) + ' CHECK CONSTRAINT ALL' AS '--Code to enable the previously disabled constraints'
            END

        PRINT 'GO'
    END

PRINT ''
IF (@IDN <> '')
    BEGIN
        PRINT 'SET IDENTITY_INSERT ' + QUOTENAME(COALESCE(@owner,USER_NAME())) + '.' + QUOTENAME(@table_name) + ' OFF'
        PRINT 'GO'
    END

PRINT 'SET NOCOUNT OFF'

SET NOCOUNT OFF
RETURN 0 --Success. We are done!
END

GO

PRINT 'Created the procedure'
GO

--Marking this as a system procedure in master database
EXEC sys.sp_MS_marksystemobject sp_generate_inserts
GO

PRINT 'Granting EXECUTE permission on sp_generate_inserts to all users'
GRANT EXEC ON sp_generate_inserts TO public

SET NOCOUNT OFF
GO

PRINT 'Done'