项目上有需求要使用存储来处理大量表操作,个人使用心得如下:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
(CREATE)ALTER procedure [dbo].[autoMoni] ---创建或者修改存储
(
@year int,
@quarter int,
@org_id varchar(10)
)
as
declare
@Moni_ID int ----------- 声明变量,可以在整个存储中使用的变量,相当于java中的全局变量
--删除XXX表
delete from XXX
where [email protected]_id and [email protected] and [email protected]
--查询xxx主键
select @Moni_ID=Moni_ID from xxx -------------将查询数据保存到变量中,以供其它语句使用
where [email protected]_id and [email protected] and [email protected]
---插入XXX 表
insert into XXX
select
ORG_ID, RPT_YEAR, RPT_QUARTER, @Moni_ID as 'Moni_ID' ---将上面查询到的数据做为变量插入到另外的表
from XXX
where [email protected]_id and [email protected] and [email protected]
------执行存储
exec automoni
@year=2012,@quarter=17,@org_id='ecer' ----传递参数