if OBJECT_ID('tb1_A','u') is not null
drop table tb1_A
create table tb1_A
(
ip int,
type int,
id int
)
go
insert into tb1_A
select 1,1,2 union all
select 2,1,3 union all
select 3,2,1 union all
select 4,3,3 union all
select 5,3,3 union all
select 6,2,1
create table a_1
(
ip int,
type int,
id int
)
create table a_2
(
ip int,
type int,
id int
)
create table a_3
(
ip int,
type int,
id int
)
create proc text_tb1
as
insert into a_1 select *From tb1_A where [type]=1
insert into a_2 select *From tb1_A where [type]=2
insert into a_3 select *From tb1_A where [type]=3
go
exec text_tb1
create procedure sp_name()
begin
insert into a_1 select * from a_temp where type=1
insert into a_2 select * from a_temp where type=2
insert into a_3 select * from a_temp where type=3 end
--执行存储过程
exec 存储过程名
--创建测试数据
if OBJECT_ID('a_temp') is not null drop table a_temp
create table a_temp(ip int identity(1,1),[type] int,id int)
go
insert into a_temp([type],id)
select 1,2 union all
select 1,3 union all
select 2,1 union all
select 3,3 union all
select 3,3 union all
select 2,1 union all
select 7,3 union all
select 8,2
--创建存储过程
if OBJECT_ID('[dbo].[createTableByType_20121202]') is not null drop procedure [dbo].[createTableByType_20121202]
go
create procedure [dbo].[createTableByType_20121202]
as
declare @number int,--最大的类型值
@i int,--临时变量
@sql nvarchar(max)--存放动态拼接的sql语句(用于后面动态插入)
set @i=1
select @number=MAX([type]) from a_temp
while @i<=@number
begin
if isnull(@number,0)>=@i
begin
set @sql=isnull(@sql,'')+'if exists(select * from a_temp where [type]='+convert(nvarchar(4),@i)+') begin'
set @sql=@sql+' if OBJECT_ID(''a_'+convert(nvarchar(4),@i)+''') is not null drop table a_'
+convert(nvarchar(4),@i)+' select * into a_'+convert(nvarchar(4),@i)+' from a_temp where [type]='+convert(nvarchar(4),@i)+' end '
set @i=@i+1
end
end
if(isnull(@sql,'')<>'')
--print @sql
exec(@sql)
go
--调用存储过程
exec [dbo].[createTableByType_20121202]
/* 查看新增表的查询结果
select * from a_1
select * from a_2
select * from a_3
select * from a_7
select * from a_8
--结果如下
ip type id
----------- ----------- -----------
1 1 2
2 1 3
(2 行受影响)
ip type id
----------- ----------- -----------
3 2 1
6 2 1
(2 行受影响)
ip type id
----------- ----------- -----------
4 3 3
5 3 3
(2 行受影响)
ip type id
----------- ----------- -----------
7 7 3
(1 行受影响)
ip type id
----------- ----------- -----------
8 8 2
(1 行受影响)
*/
--以上面的例子为例存储过程代码备注:(其实上面的存储过程就是动态生成下面的sql语句)
/*if exists(select * from a_temp where [type]=1)--判断a_temp表中是否存在[type]=1的数据
begin --如果有
if OBJECT_ID('a_1') is not null drop table a_1 --看表a_1(这里的1就是上面的[type])是否存在,存在则删除
select * into a_1 from a_temp where [type]=1 --查询表a_temp中[type]=1的数据新增表a_1并将数据插入到a_1
end
--下面同上
if exists(select * from a_temp where [type]=2)
begin
if OBJECT_ID('a_2') is not null drop table a_2
select * into a_2 from a_temp where [type]=2
end
--......下面还有很多组
*/
--创建测试数据
if OBJECT_ID('a_temp') is not null drop table a_temp
create table a_temp(ip int identity(1,1),[type] int,id int)
go
insert into a_temp([type],id)
select 1,2 union all
select 1,3 union all
select 2,1 union all
select 3,3 union all
select 3,3 union all
select 2,1 union all
select 7,3 union all
select 8,2
--创建存储过程
if OBJECT_ID('[dbo].[createTableByType_20121202]') is not null drop procedure [dbo].[createTableByType_20121202]
go
create procedure [dbo].[createTableByType_20121202]
as
declare @sql varchar(max) =''
--表存在则删除
select @sql=@sql+'if exists(select * from a_temp where [type]='+ltrim([type])+') begin'
+' if OBJECT_ID(''a_'+ltrim([type])+''') is not null drop table a_'
+ltrim([type])+' end ' from (select distinct [type] from a_temp) t
--插入
select @sql = @sql +'
select * into a_'+ltrim([type])+' from a_temp where type='+ltrim([type]) from (select distinct [type] from a_temp) t
exec(@sql)
go
--调用
exec [dbo].[createTableByType_20121202]
--创建存储过程
if OBJECT_ID('[dbo].[createTableByType_20121202]') is not null drop procedure [dbo].[createTableByType_20121202]
go
create procedure [dbo].[createTableByType_20121202]
as
declare @sql varchar(max) =''
--表存在则删除
select @sql=@sql+' if OBJECT_ID(''a_'+ltrim([type])+''') is not null drop table a_'
+ltrim([type]) from (select distinct [type] from a_temp) t
--插入
select @sql = @sql +'
select * into a_'+ltrim([type])+' from a_temp where type='+ltrim([type]) from (select distinct [type] from a_temp) t
exec(@sql)
USE test
GO
-->生成表a_temp
if object_id('a_temp') is not null
drop table a_temp
Go
Create table a_temp([ip] smallint,[type] smallint,[id] smallint)
Insert into a_temp
Select 1,1,2
Union all Select 2,1,3
Union all Select 3,2,1
Union all Select 4,3,3
Union all Select 5,3,3
Union all Select 6,2,1
Union all Select 7,4,1 -- for test
Union all Select 8,3,1 -- for test
Union all Select 9,4,1 -- for test
DECLARE @sql NVARCHAR(MAX)
SELECT @sql=ISNULL(@sql+';','')+'Select * into a_'+LTRIM(type)+' FROM a_temp Where type='+LTRIM(type) FROM a_temp GROUP BY type
EXEC (@sql)
--SELECT * FROM a_1
--SELECT * FROM a_2
--SELECT * FROM a_3
--SELECT * FROM a_4
--
--if object_id('a_1') is not null
-- drop table a_1
--Go
--if object_id('a_2') is not null
-- drop table a_2
--Go
--if object_id('a_3') is not null
-- drop table a_3
--Go
--if object_id('a_4') is not null
-- drop table a_4
--Go
create proc up_test
as
select * into a_1 from tb1_A where [type]=1
select * into a_2 from tb1_A where [type]=2
select * into a_3 from tb1_A where [type]=3
go
exec up_test
-->测试数据a_temp
if object_id('a_temp') is not null
drop table a_temp
Go
Create table a_temp([ip] smallint,[type] smallint,[id] smallint)
Insert into a_temp
Select 1,1,2 Union all
Select 2,1,3 Union all
Select 3,2,1 Union all
Select 4,3,3 Union all
Select 5,3,3 Union all
Select 6,2,1
DECLARE @sql NVARCHAR(MAX)
SELECT @sql=ISNULL(@sql+';'+CHAR(10),'')+'Select * into a_'+LTRIM(type)+' FROM a_temp Where type='+LTRIM(type) FROM a_temp GROUP BY type
PRINT @sql
/*
Select * into a_1 FROM a_temp Where type=1;
Select * into a_2 FROM a_temp Where type=2;
Select * into a_3 FROM a_temp Where type=3;
Select * into a_4 FROM a_temp Where type=4
*/
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。