0
点赞
收藏
分享

微信扫一扫

PLSQL ORACLE 数据库 update 操作 存储过程实现数据更新

--------------------------------------------------------------------------------------------------------------------------------

1.创建存储过程

create or replace 存储过程名 (输出参数 in or out )

is/as

--定义参数

v_name varchar2(10):= ‘某某’;

--可以进行赋值

v_number T1_student.number1%type;

--%type 可以便捷获取数据类型

begin

select 表名.列名 into 变量名 from 表 where 条件

dbms_output.put.line( 变量名 )

--赋值并且输出变量名

------------------------------------------------------------------------------------------------------------------------------

-- 更新数据

select 表名.列名 into 变量名 from 表 where 条件

select 临时表名 from 数据

--传入参数后

--变量名 = v_name

update 临时表.数据列 from  数据来源表  set   变量名 = ‘’ ;

设置或更改 触发器

create  triigit  name 

        

数据更新

并且输出日志 

log  

可以确保数据出现失误时候查看日志 方便管理

---------------------------------------------------------------------------------------------------------------------------

--实例

- Function   :      sps_log_add

 -- Description       :      record all sps change log

 --1 get all new sps (find sps which in new table not in old table)

 --2 get all deleted sps (find sps which in old table not in new table)

 --3 get all updated sps (find sps which in old talbe are not same in new table)

 -- any change will record into sps_log table

------------------------------------------------------------------------------------------------------*/

CREATE procedure [dbo].[sps_log_add]

as

-- clear out sps_new

truncate table sps_new

-- fill into sps_new

insert into       sps_new

(sp_id,colid,sp_name,sp_content,record_date)

select

       a.id,

       a.colid,

       b.name,

       a.[text],

       getdate()

from

       syscomments a

left join

       sysobjects b

 on a.id = b.id

where b.type='P' and b.name not like 'dt_%'

-- Find new sp

insert into

       sps_log

select distinct

       sp_id,

       getdate(),

       'Added',

       sp_name

from

       sps_new

where

       sp_id not in (select distinct sp_id from sps_old)

-- Find deleted sp

insert into

       sps_log

select distinct

       sp_id,

       getdate(),

       'Removed',

       sp_name

from

       sps_old

where

       sp_id not in (select distinct sp_id from sps_new)

-- compare existing sp

DECLARE @ProcID int

declare @count_new int

declare @count_old int

declare @text_new varchar(4000)

declare @text_old varchar(4000)

declare @name varchar(150)

declare @i int

DECLARE SearchProc CURSOR FOR

select distinct

       sp_id

from

       sps_new

where

       sp_id in (select distinct sp_id from sps_old)

order by

       sp_id

open SearchProc

FETCH NEXT FROM SearchProc

INTO @ProcID

WHILE @@FETCH_STATUS >=0

BEGIN

       -- colid quantity

       select @count_new=count(colid) from sps_new where sp_id = @ProcID

      select @count_old=count(colid) from sps_old where sp_id = @ProcID

-- if count of colid is unequal, sp was updated.

       if @count_new <> @count_old

              begin

                     -- Record change

                     insert into       sps_log(sp_id,change_date,action_type) values(@ProcID,getdate(),'Update')

              end

       else -- @count_new = @count_old, if count of colid is equal

              begin

                     set @i=1 -- Reset Counter

                     while @i<=@count_new -- colid

                     begin

                                   -- sp content

select @text_new = sp_content from sps_new

where sp_id = @ProcID and colid = @i

                                   select @text_old = sp_content from sps_old

where sp_id = @ProcID and colid = @i

-- if content is different, sp was updated.

                                   if @text_new <> @text_old        

                                          begin      

                                                 -- Record change

                                                 select @name = [name] from sysobjects where id=@ProcID

                                                 insert into sps_log(sp_id,change_date,action_type,sp_name)

values(@ProcID,getdate(),'Update',@name)

                                          end

                                   set @i= @i+1 -- Counter + 1

                     end

              end

       FETCH NEXT FROM SearchProc

    INTO @ProcID

END

CLOSE SearchProc

DEALLOCATE SearchProc

-- clear out sps_new

truncate table sps_old

-- fill into sps_old with sps_new

insert into sps_old select * from sps_new

----------------------------------------------------------------------------------------------------------------------

3创建一个存储过程从sps_log中获取指定时间内的数据

 

/*

 -- Function   : sps_log_get

 -- Description       : Show sps change in a period of time

*/

CREATE PROCEDURE [dbo].[sps_log_get]

@from_date datetime,

@to_date datetime

as

       select

              sp_name,

              action_type,

              CONVERT(varchar(10),change_date,102) as change_date

       from

              sps_log

       where

              change_date between @from_date and @to_date

       group by

              sp_name,action_type,CONVERT(varchar(10),change_date,102)

       order by

              CONVERT(varchar(10),change_date,102) asc

 

注意数据的对应关系

以及数据中实现多表的连接

数据的可行性

以及系统的性能优化问题

----------------------------------------------------------------------------------------------------------------------

举报

相关推荐

0 条评论