Monday, January 25, 2010

Insert query of datatable


DECLARE @PassTableName as varchar(1000)

SET @PassTableName = 'tbl_ReturnsEFileFieldMapping' --- 'Type Table Name'

BEGIN

DECLARE @SQL_Insert AS Varchar(8000)
DECLARE @SQL_Values AS Varchar(8000)

DECLARE @FieldName AS Varchar(200)
DECLARE @FieldType as int
DECLARE @FieldTypeName as varchar(100)
DECLARE @FieldLength as int
DECLARE @FieldIdentity as int

SET @SQL_Insert = 'SELECT ''INSERT INTO ' + @PassTableName + ' ( '
SET @SQL_Values = ' VALUES ( '

DECLARE Fields_Cursor CURSOR FOR
SELECT Name AS FieldName,
xType as FieldType,
Type_Name(xType) AS FieldTypeName,
Length as FieldLength,
ColStat & 1 AS IsIdentity
FROM SysColumns
WHERE ID IN ( SELECT ID FROM SysObjects WHERE Name = REPLACE( REPLACE( REPLACE( @PassTableName, 'dbo.', ''), 'Report.', ''), 'EQISAdmin.', '') )

OPEN Fields_Cursor
FETCH NEXT FROM Fields_Cursor INTO @FieldName, @FieldType, @FieldTypeName, @FieldLength, @FieldIdentity

WHILE @@FETCH_STATUS = 0
BEGIN

-- IF @FieldIdentity = 0 -- Identity is No
-- BEGIN

SET @SQL_Insert = @SQL_Insert + @FieldName + ', '

IF @FieldType = 48 OR @FieldType = 56 OR @FieldType = 127 OR @FieldType = 59 OR @FieldType = 52 -- Small Int OR Integer OR Big Integer OR Real OR SmallInt
BEGIN
SET @SQL_Values = @SQL_Values + ''' + Convert( varchar(20), ISNULL(' + @FieldName + ', '''' ) ) + '', '
END
ELSE IF @FieldType = 106 OR @FieldType = 108 -- Decimal OR Numeric
BEGIN
SET @SQL_Values = @SQL_Values + ''' + Convert( varchar(30), ISNULL(' + @FieldName + ', '''') ) + '', '
END
ELSE IF @FieldType = 60 OR @FieldType = 122 -- Money OR Small Money
BEGIN
SET @SQL_Values = @SQL_Values + ''' + Convert( varchar(30), Convert( Decimal(30,4), ISNULL(' + @FieldName + ', 0) )) + '', '
END
ELSE IF @FieldType = 62 -- Float
BEGIN
SET @SQL_Values = @SQL_Values + ''' + Convert( varchar(30), Convert( Decimal(30,10), ISNULL(' + @FieldName + ',0) )) + '', '
END
ELSE IF @FieldType = 104 -- Bit
BEGIN
SET @SQL_Values = @SQL_Values + ''' + Convert( varchar(1), ISNULL(' + @FieldName + ','''') ) + '', '
END
ELSE IF @FieldType = 61 OR @FieldType = 58 -- Datetime OR SmallDatetime
BEGIN
SET @SQL_Values = @SQL_Values + ''''''' + Convert( varchar(20), ISNULL(' + @FieldName + ','''') ) + '''''', '
END
ELSE IF @FieldType = 167 OR @FieldType = 175 OR @FieldType = 239 OR @FieldType = 231 -- Varchar OR Char OR nChar OR nVarchar
BEGIN
SET @SQL_Values = @SQL_Values + ''''''' + Replace( ISNULL(' + @FieldName + ', ''''), '''''''', '''''''''''' ) + '''''', '
END
ELSE IF @FieldType = 35 OR @FieldType = 99 -- Text OR nText
BEGIN
SET @SQL_Values = @SQL_Values + ''''''' + Replace( Convert( varchar(8000), ISNULL(' + @FieldName + ','''')), '''''''', '''''''''''' ) + '''''', '
END
ELSE IF @FieldType = 241 -- XML
BEGIN
SET @SQL_Values = @SQL_Values + ''''''' + Replace( Convert( varchar(8000), ISNULL(' + @FieldName + ','''')), '''''''', '''''''''''' ) + '''''', '
END

-- END


FETCH NEXT FROM Fields_Cursor INTO @FieldName, @FieldType, @FieldTypeName, @FieldLength, @FieldIdentity

END

CLOSE Fields_Cursor
DEALLOCATE Fields_Cursor

SET @SQL_Insert = LTRIM( RTRIM( @SQL_Insert ) )
IF LEN( @SQL_Insert ) > 1
BEGIN
SET @SQL_Insert = SUBSTRING( @SQL_Insert, 1, LEN( @SQL_Insert ) -1 )
END

SET @SQL_Values = LTRIM( RTRIM( @SQL_Values ) )
IF LEN( @SQL_Values ) > 1
BEGIN
SET @SQL_Values = SUBSTRING(@SQL_Values, 1, LEN( @SQL_Values ) -1 )
END

--PRINT ( @SQL_Insert + ' ) ' + @SQL_Values + ' ) '' FROM ' + @PassTableName )
EXEC ( @SQL_Insert + ' ) ' + @SQL_Values + ' ) '' FROM ' + @PassTableName )

END
GO

No comments: