当前位置: 代码迷 >> Sql Server >> 小弟我想把表里面的数据生成SQL脚本里面的插入语句。该如何做
  详细解决方案

小弟我想把表里面的数据生成SQL脚本里面的插入语句。该如何做

热度:79   发布时间:2016-04-27 16:27:52.0
我想把表里面的数据生成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 @[email protected] + 'insert into @t(CountryCode,CountryName) values( ' ' '+CountryCode+ ' ' ', ' ' '+CountryName+ ' ' ') '+char(10)
from @t
print @s
--exec(@s) --用此句時,[email protected]

/*結果:
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 ')
*/
  相关解决方案