日期:2014-05-18  浏览次数:20515 次

我想把表里面的数据生成SQL脚本里面的插入语句。该怎么做?
我想把表里面的数据生成SQL脚本里面的插入语句。该怎么做?

------解决方案--------------------
--建立測試環境
declare @t table(CountryCode varchar(20),CountryName varchar(50))
insert @t select 'cn ', 'China '
union all select 'jp ', 'Japan '
union all select 'jp1 ', 'Japan1 '
union all select 'jp2 ', 'Japan2 '
union all select 'jp3 ', 'Japan3 '

--動態語句查詢或執行
declare @s varchar(8000)
set @s= ' '
select @s=@s + 'insert into @t(CountryCode,CountryName) values( ' ' '+CountryCode+ ' ' ', ' ' '+CountryName+ ' ' ') '+char(10)
from @t
print @s
--exec(@s) --用此句時,須將@t改為實際的表名

/*結果:
insert into @t(CountryCode,CountryName) values( 'cn ', 'China ')
insert into @t(CountryCode,CountryName) values( 'jp ', 'Japan ')
insert into @t(CountryCode,CountryName) values( 'jp1 ', 'Japan1 ')
insert into @t(CountryCode,CountryName) values( 'jp2 ', 'Japan2 ')
insert into @t(CountryCode,CountryName) values( 'jp3 ', 'Japan3 ')
*/