在SQL Server 2005中使用OUTPUT完成数据操作时的备份
来源:互联网 发布:ubuntu复制目录命令 编辑:程序博客网 时间:2024/04/27 18:20
use tempdb;
go
create table itest ( i int identity not null primary key, j int not null unique );
go
create trigger insert_itest on itest after insert
as
begin
insert into #new ( i, j )
select i, j
from inserted;
end
go
create table #new ( i int not null, j int not null );
insert into itest ( j )
select o.id from sysobjects as o;
go
create trigger insert_itest on itest after insert
as
begin
insert into #new ( i, j )
select i, j
from inserted;
end
go
create table #new ( i int not null, j int not null );
insert into itest ( j )
select o.id from sysobjects as o;
-- Newly inserted rows and identity values:
select * from #new;
-- #new can be used now to insert into a related table:
drop table #new, itest;
go
在SQL Server 2005中可以将上面的插入操作进行重新组合:
--即:插入数据和保存历史记录同时完成
--即:插入数据和保存历史记录同时完成
insert into itest (j)
output inserted.i, inserted.j into #new
output inserted.i, inserted.j into #new
使用OUTPUT可以简化代码和同时完成数据的更新删除和保存历史的操作,
如下例Update、Delete:
create table t ( i int not null );
create table t_audit ( old_i int not null, new_i int null );
insert into t (i) values( 1 );
insert into t (i) values( 2 );
create table t_audit ( old_i int not null, new_i int null );
insert into t (i) values( 1 );
insert into t (i) values( 2 );
update t
set i = i + 1
output deleted.i, inserted.i into t_audit
where i = 1;
set i = i + 1
output deleted.i, inserted.i into t_audit
where i = 1;
delete from t
output deleted.i, NULL into t_audit
where i = 2;
output deleted.i, NULL into t_audit
where i = 2;
select * from t;
select * from t_audit;
select * from t_audit;
go
附:OUTPUT语法
- 1、OUTPUT_CLAUSE定义(语法参Transact-SQL 语法约定):
- <OUTPUT_CLAUSE> ::=
- {
- [ OUTPUT <dml_select_list> INTO { @table_variable | output_table } [ ( column_list ) ] ]
- [ OUTPUT <dml_select_list> ]
- }
- <dml_select_list> ::=
- { <column_name> | scalar_expression } [ [AS] column_alias_identifier ]
- [ ,...n ]
- <column_name> ::=
- { DELETED | INSERTED | from_table_name } . { * | column_name }
- 2、OUTPUT_CLAUSE说明:
- 返回受 INSERT、UPDATE 或 DELETE 语句影响的每行的信息,或者返回基于上述每行的表达式。这些结果可以返回到处理应用程序,以供在确认消息、存档以及其他类似的应用程序要求中使用。此外,也可以将结果插入表或表变量。
- 3、典型应用:
- 1、根据当前表的数据有条件的生成历史或新的初始化数据;
- 2、把INSERT、UPDATE 或 DELETE 语句影响的每行的信息暂存处理或反馈给应用程序完成业务或逻辑的完整性;
- 3、OUTPUT 子句对于在 INSERT 或 UPDATE 操作之后检索标识列或计算列的值可能非常有用;
- 在SQL Server 2005中使用OUTPUT完成数据操作时的备份
- SQL Server中OutPut子句的使用
- 关于在[SQL Server]Store Procedure中使用OUTPUT 游标
- 在SQL Server应用中使用OUTPUT子句
- 在SQL Server应用中使用OUTPUT子句
- 在SQL Server应用中使用OUTPUT子句
- 在SQL Server中连接Oracle,完成查询、插入操作
- SQL Server在windows server下的自动备份操作
- SQL SERVER 2005中全新的OUTPUT子句添加数据记录详解(理论篇)
- SQL SERVER 2005中全新的OUTPUT子句添加数据记录详解(实战篇)
- 在SQL Server 2005中备份数据库时出错
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- 在SQL Server 2005中连接Oracle,完成查询、插入操作
- 在vb.net中使用程序备份sql server数据库
- Sql server 2005中output用法解析
- Sql server 2005中output用法解析
- Sql server 2005中output用法解析
- 面向对象设计的基本原则
- 一个女人究竟该花多少钱?
- linux export问题
- Tomcat报告——Tomcat中SSI的配置
- 有效的工作分解结构 笔记
- 在SQL Server 2005中使用OUTPUT完成数据操作时的备份
- 用ATL实现在COM接口中返回另一个COM类的接口
- Enumerating Windows Processes
- GUI architecture
- andriod系统的编译
- JavaScript应用:Iframe自适应其加载的内容高度
- 面对金融危机 你如何帮助企业度过难关?
- Fedora 8 升级后的显卡驱动问题(黑屏及fglrx)
- C#中调用执行其他应用程序