| dhhung83Starting Member
 
 
                                        2 Posts | 
                                            
                                            |  Posted - 2010-08-17 : 12:03:18 
 |  
                                            | CREATE PROC [dbo].[InsertGenerator](@tableName varchar(100)) as--Declare a cursor to retrieve column specific information for the specified tableDECLARE cursCol CURSOR FAST_FORWARD FOR SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableNameOPEN cursColDECLARE @string nvarchar(3000) --for storing the first half of INSERT statementDECLARE @stringData nvarchar(3000) --for storing the data (VALUES) related statementDECLARE @dataType nvarchar(1000) --data types returned for respective columnsSET @string='INSERT '+@tableName+'('SET @stringData=''DECLARE @colName nvarchar(50)FETCH NEXT FROM cursCol INTO @colName,@dataTypeIF @@fetch_status<>0	begin	print 'Table '+@tableName+' not found, processing skipped.'	close curscol	deallocate curscol	returnENDWHILE @@FETCH_STATUS=0BEGINIF @dataType in ('varchar','char','nchar','nvarchar')BEGIN	--SET @stringData=@stringData+'''''''''+isnull('+@colName+','''')+'''''',''+'	SET @stringData=@stringData+''''+'''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+'ENDELSEif @dataType in ('text','ntext') --if the datatype is text or something else BEGIN	SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(2000)),'''')+'''''',''+'ENDELSEIF @dataType = 'money' --because money doesn't get converted from varchar implicitlyBEGIN	SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'ENDELSE IF @dataType='datetime'BEGIN	--SET @stringData=@stringData+'''convert(datetime,''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+''''''),''+'	--SELECT 'INSERT Authorizations(StatusDate) VALUES('+'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM Authorizations	--SET @stringData=@stringData+'''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+'	SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'  --                             'convert(datetime,'+isnull(''''+convert(varchar(200),StatusDate,121)+'''','NULL')+',121),)' FROM AuthorizationsENDELSE IF @dataType='image' BEGIN	SET @stringData=@stringData+'''''''''+isnull(cast(convert(varbinary,'+@colName+') as varchar(6)),''0'')+'''''',''+'ENDELSE --presuming the data type is int,bit,numeric,decimal BEGIN	--SET @stringData=@stringData+'''''''''+isnull(cast('+@colName+' as varchar(200)),''0'')+'''''',''+'	--SET @stringData=@stringData+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+'	SET @stringData=@stringData+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+'ENDSET @string=@string+@colName+','FETCH NEXT FROM cursCol INTO @colName,@dataTypeENDDECLARE @Query nvarchar(4000)SET @query ='SELECT '''+substring(@string,0,len(@string)) + ') VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '+@tableNameexec sp_executesql @query--select @queryCLOSE cursColDEALLOCATE cursColVisit this site to reference SQL command: [url]http://sql.entersources.com[/url] |  |