sql server 存储过程实现表的字段拆分,拆分后插入另外一个表

laughboy 2008-09-02 03:27:43
例如我有一个表A,有字段id int,time datetime,log_id Int,banch_id int
现在我要把time字段拆分成年月日为一个字段time1,时分秒为另外一个字段time2。
把id,time1,time2,log_id,banch_id 插入表B中,同时在B表中还要增加一个num_id字段,
在插入b表的时候要比较id,time1,log_id,banch_id 4个字段如果相同的记录有可能有多条,多条的情况下
num_id递增插入(如果有5条相同的记录num_id 1,2,3,4,5),5个字段相同的记录只有一条num_id 为1。
请问用存储过程怎么实现。(用游标怎么实现)
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tigersong 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 laughboy 的回复:]
Tigersong :
你的临时表用的太多了吧,如果数据量大的话,太占空间了。


有没有人想过用游标实现呢?
[/Quote]
我只是提供一个方案 FYI, 而且很明显你可以在第一步就拆分而省去一个临时表#t,后面的逻辑自己改一下即可
如果这样的方案不可以的话, 以我的经验 用游标效率更差,IO和time都是需要考虑的问题
laughboy 2008-09-02
  • 打赏
  • 举报
回复
Tigersong :
你的临时表用的太多了吧,如果数据量大的话,太占空间了。


有没有人想过用游标实现呢?
行者无疆-Kevin 2008-09-02
  • 打赏
  • 举报
回复
insert into B(num_id, id, time1,time2,log_id,banch_id)
select id,convert(varchar(10),[time],112),convert(varchar(10),[time],108)
,log_id,count(aa.num_id) from a,aa
where aa.num_id=a.num_id
Tigersong 2008-09-02
  • 打赏
  • 举报
回复
试试下面的代码

select id,
time,
log_id,
banch_id
num_id = identity(int,1,1)
into #t
from table_A
order by id,time,log_id,banch_id
go

select id,
time,
log_id,
banch_id,
min(num_id) num_id
into #t1
from #t
group by id,
time,
log_id,
banch_id
go

update #t
set num_id = a.num_id - b.num_id+1
from #t a,#t1 b
where a.id = b.id
and a.time = b.time
and a.log_id = b.log_id
and a.banch_id = b.banch_id
go

select id,
convert(char(10),time,120) as time1,
convert(char(8) ,time,108) as time2,
log_id,
banch_id,
num_id
into table_B
from #t
go


laughboy 2008-09-02
  • 打赏
  • 举报
回复
我的想法通过游标能够实现同样的效果吗?
水族杰纶 2008-09-02
  • 打赏
  • 举报
回复
不需要游標~~
lgxyz 2008-09-02
  • 打赏
  • 举报
回复
过程你直接把那些语句拷进去就好了

游标:这游标有起到什么用处.
laughboy 2008-09-02
  • 打赏
  • 举报
回复
有人这样实现过。


--建立測試環境
create table A(id int,[time] datetime, log_id int, banch_id int)
insert into A select 1 ,'2008-8-20 14:30:00' ,1,1
insert into A select 1 ,'2008-8-20 14:30:00' ,1,1
insert into A select 1 ,'2008-8-20 14:30:00' ,1,1
insert into A select 2 ,'2008-8-20 15:30:00' ,1,1
insert into A select 3 ,'2008-8-20 16:30:00' ,1,1
insert into A select 3 ,'2008-8-20 17:30:00' ,1,1
GO
Create table B(id int, time1 varchar(10), time2 varchar(08), log_id int, banch_id int, num_id int)

GO
--建立存儲過程
Create proc usp_test
AS
select tmp=identity(int,1,1) ,* into #t from A

insert into B(num_id, id, time1,time2,log_id,banch_id)
select num_id=(select count(*) from #t where id=t1.id and [time]=t1.[time] and log_id=t1.log_id and banch_id=t1.banch_id and tmp<=t1.tmp)
, id, convert(char(10),[time],120) as time1, convert(char(08),[time] ,108) as time2, log_id,banch_id
from #t t1

GO
--執行
exec usp_test
--查看結果
select * from B
/*
id time1 time2 log_id banch_id num_id
----------- ---------- -------- ----------- ----------- -----------
1 2008-08-20 14:30:00 1 1 1
1 2008-08-20 14:30:00 1 1 2
1 2008-08-20 14:30:00 1 1 3
2 2008-08-20 15:30:00 1 1 1
3 2008-08-20 16:30:00 1 1 1
3 2008-08-20 17:30:00 1 1 1
*/

GO
drop proc usp_test
drop table A,B


现在需要通过游标的存储过程怎么实现?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧