Script All Insert Satements of the table in sql server 2000 -
i wrote procedure creates insert statements passing table name, because of limitation sql server row size has, if statement becomes more row size, can not have complete statement run. want have 2 rows each statement , first row insert [tablename] (column list) , second row values (). if knows how that, please me that. thank
set quoted_identifier off go set ansi_nulls on go if exists (select * sysobjects type = 'p' , name ='insertgenerator') drop procedure insertgenerator go create proc insertgenerator (@tablename varchar(100)) declare curscol cursor fast_forward select column_name,data_type information_schema.columns table_name = @tablename open curscol declare @string nvarchar(3000) declare @stringdata nvarchar(3000) declare @datatype nvarchar(1000) set @string='insert '+@tablename+'(' set @stringdata='' declare @colname nvarchar(50) fetch next curscol @colname,@datatype if @@fetch_status<>0 begin print 'table '+@tablename+' not found, processing skipped.' close curscol deallocate curscol return end while @@fetch_status=0 begin if @datatype in ('varchar','char','nchar','nvarchar') begin set @stringdata=@stringdata+''''+'''+isnull('''''+'''''+'+@colname+'+'''''+''''',''n ull'')+'',''+' end else if @datatype in ('text','ntext') --if datatype text or else begin set @stringdata=@stringdata+'''''''''+isnull(cast('+@colname+' varchar(2000)),'''')+'''''',''+' end else if @datatype = 'money' begin set @stringdata=@stringdata+'''convert(money,''''''+isnull(cast('+@colname+' varchar(200)),''0.0000'')+''''''),''+' end else if @datatype='datetime' begin set @stringdata=@stringdata+'''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colname+',121)+'''''+''''',''null'')+'',121),''+' end else if @datatype='image' begin set @stringdata=@stringdata+'''''''''+isnull(cast(convert(varbinary,'+@colname+') varchar(6)),''0'')+'''''',''+' end else begin set @stringdata=@stringdata+''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colname+')+'''''+''''',''null'')+'',''+' end set @string=@string+@colname+',' fetch next curscol @colname,@datatype end declare @query nvarchar(4000) set @query ='select '''+substring(@string,0,len(@string)) + ') values(''+ ' + substring(@stringdata,0,len(@stringdata)-2)+'''+'')'' '+@tablename exec sp_executesql @query close curscol deallocate curscol go set quoted_identifier off go set ansi_nulls on go --exec insertgenerator 'abi_shobe'
Comments
Post a Comment