这种存储过程怎么写?

billqiang 2001-12-08 07:47:57
用sql server
我想遍例一个结果集,操作这个结果集的记录生成新纪录。然后输出这些新记录。
我想用create table ##tmp 是可以的,但用临时表好吗,用游标可以吗?
...全文
63 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanlean 2001-12-09
  • 打赏
  • 举报
回复
用游标可以的 一个例子 不过长了点(从一个表中sel出符合条件的 其中一个字段写到游标中,然后再调用游标 把游标中的记录取出 sel后写入另一个表中)

自己看吧!

CREATE PROCEDURE [P_payin]
@year varchar(20),
@year2 varchar(20),
@dept_code varchar(20)
AS
declare @code2 varchar(20)
declare code insensitive cursor for select ext_empid from v_pextendout where ext_date=@year and ext_empid in (select emp_id from v_employee where emp_dept like @dept_code+'%')
open code
fetch next from code into @code2
while @@fetch_status=0
begin
if not exists(select ext_empid from pextend where ext_date=@year2 and ext_empid=@code2 )
begin
insert into pextend select Ext_EmpId, @year2,ext_empcode, Ext_APay, Ext_SPay, Ext_Absent, Ext_Duty, Ext_Admin, Ext_Tech, Ext_Foreign, Ext_Live, Ext_Covertime, Ext_Prink, Ext_Drink, Ext_Temp, Ext_Overtime, Ext_Supper, Ext_Outlength, Ext_Inlength, Ext_Aged, Ext_Medicare,Ext_House, Ext_Dorm, Ext_Tax, Ext_Bail, Ext_Should, Ext_Lateleave, Ext_Add, Ext_Cut,null from v_pextendout where ext_date=@year and ext_empid=@code2 --in (select emp_id from v_employee where emp_dept like '@dept_code%')
end
fetch next from code into @code2
end
CLOSE code
DEALLOCATE code
GO

billqiang 2001-12-09
  • 打赏
  • 举报
回复
用游标怎么写,能给点提示吗。
深蓝兄能说的详细点吗
Corny 2001-12-08
  • 打赏
  • 举报
回复
如果你要生成新的结果集,用临时表还省事些,用游标当然可以,但代码会大得多,不过你善于用游标的话,还是可以节省一点点资源的
sp_2000 2001-12-08
  • 打赏
  • 举报
回复
用insert into table (select 条件记录 from table where condition)

34,623

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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