declare @t table (CODE varchar(10)) insert @t select 'AAA' union all select 'AAA' union all select 'AAA' union all select 'BBB' union all select 'BBB' union all select 'BBB' union all select 'CCC' union all select 'CCC' union all select 'CCC' select 'select * into '+code+' from @t where CODE='+CODE as 生成语句 from @T group by code/* 生成语句 ------------------------------------------------------ select * into AAA from @t where CODE=AAA select * into BBB from @t where CODE=BBB select * into CCC from @t where CODE=CCC */
declare @t table (CODE varchar(10)) insert @t select 'AAA' union all select 'AAA' union all select 'AAA' union all select 'BBB' union all select 'BBB' union all select 'BBB' union all select 'CCC' union all select 'CCC' union all select 'CCC' select 'select * into '+code+' from @t where CODE='+CODE as 生成语句 from @T group by code/* 生成语句 ------------------------------------------------------ select * into AAA from @t where CODE=AAA select * into BBB from @t where CODE=BBB select * into CCC from @t where CODE=CCC */
现在是使用一个程序来处理,如果不能用一条SQL语句解决的问题,就复杂很多。
MASTER.DBO.XP_EXECRESULTSET 'SELECT DISTINCT ''SELECT * INTO STOCK_''+code+'' FROM STOCK WHERE CODE=''''''+CODE+'''''''' FROM STOCK s ',N'库名'
insert @t
select 'AAA' union all
select 'AAA' union all
select 'AAA' union all
select 'BBB' union all
select 'BBB' union all
select 'BBB' union all
select 'CCC' union all
select 'CCC' union all
select 'CCC' select 'select * into '+code+' from @t where CODE='+CODE as 生成语句 from @T group by code/*
生成语句
------------------------------------------------------
select * into AAA from @t where CODE=AAA
select * into BBB from @t where CODE=BBB
select * into CCC from @t where CODE=CCC
*/
insert @t
select 'AAA' union all
select 'AAA' union all
select 'AAA' union all
select 'BBB' union all
select 'BBB' union all
select 'BBB' union all
select 'CCC' union all
select 'CCC' union all
select 'CCC' select 'select * into '+code+' from @t where CODE='+CODE as 生成语句 from @T group by code/*
生成语句
------------------------------------------------------
select * into AAA from @t where CODE=AAA
select * into BBB from @t where CODE=BBB
select * into CCC from @t where CODE=CCC
*/
',N'库名'