怎么样把数据库中的记录变成sql语句
发布网友
发布时间:2022-05-05 20:26
我来回答
共2个回答
热心网友
时间:2022-06-28 01:24
为了你这个问题 我花了一天的时间,大哥啊,为了你那5分不容易。这么复杂一个问题,你才给我5分。下面是我写的存储过程:
调用方法 exec jixingfei '表名'
create procere jixingfei(@table_name varchar(200)) as
begin
declare @sql varchar(5000),@s_mid varchar(100),@str varchar(2000),@new varchar(5000),@jixingfei varchar(5000)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[temp]
set @sql='select '
set @str='insert into '+@table_name
declare Tmp_cur cursor for select name from syscolumns where id=(select id from sysobjects where name=@table_name)
Open Tmp_cur
fetch Tmp_cur into @s_mid
set @sql=@sql+char(39)+char(39)+char(39)+char(39)+'+convert(varchar(100),'+@s_mid+')'+'+'+char(39)+char(39)+char(39)+char(39)
set @str=@str+'('+@s_mid
fetch Tmp_cur into @s_mid
while @@fetch_status=0
begin
set @sql=@sql+'+'+char(39)+','+char(39)+'+'+char(39)+char(39)+char(39)+char(39)+'+'+'convert(varchar(100),'+@s_mid+')'+'+'+char(39)+char(39)+char(39)+char(39)
set @str=@str+','+@s_mid
fetch Tmp_cur into @s_mid
end
set @sql=@sql+' as new into temp from '+@table_name
set @str=@str+') values('
--print @sql
--print @str
close Tmp_cur
deallocate Tmp_cur
exec(@sql)
declare c cursor for
select new from [temp]
open c
fetch c into @new
while @@fetch_status=0
begin
set @jixingfei=@str
set @jixingfei=@jixingfei+@new+')'
print @jixingfei
fetch c into @new
end
close c
deallocate c
end
参考资料:帮助别人是快乐之本,请加分吧!你的分是我答题的动力!
热心网友
时间:2022-06-28 01:25
能说清楚点吗?