“跨库导数据”、“事故处理”、“创建索引”、“使用触发器”等常用操作
2012-11-30 19:09
661 查看
好久没写过博客了,梳理下基础知识,复习下。
--跨库导数据
declare @ID int
declare @DAYear char(4)
declare @DAMonth char(2)
declare @DADay char(2)
declare @DATime datetime
declare @DAHour char(2)
declare @TemQty decimal(18, 6)
declare @HumQty decimal(18, 6)
declare @Note nvarchar(50)
declare @Lable int
declare TemHumCursor cursor for select *
from openrowset('SQLOLEDB','192.168.250.66';'sa';'tjdx',newcems.dbo.TemHum)
where DATime >'2012-11-26 12:12:40.527' order by DATime desc
open TemHumCursor
fetch next from TemHumCursor into @ID,@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,@Lable
while @@fetch_status=0
begin
insert into TemHum (DAYear,DAMonth,DADay,DATime,DAHour,TemQty,HumQty,Note,Lable) values (@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,0)
fetch next from TemHumCursor into @ID,@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,@Lable
end
close TemHumCursor
deallocate TemHumCursor
set nocount on
select * from TemHum order by DATime desc
go
--字段值减一
update ResultList set mydatetime = cast(cast(mydatetime as int)-1 as varchar(50))
select * from ResultList
go
--事故处理
create table 自动转账机
(
卡号 varchar(19) primary key,
金额 money check(金额>=0)
)
insert into 自动转账机 values('6222023100051898533',120.00)
insert into 自动转账机 values('6222023100051898573',0.00)
go
create table 自动取款机
(
卡号 varchar(19) primary key,
金额 money check(金额>=100)
)
delete from 自动取款机
go
insert into 自动取款机 values('6222023100051898533',120.00)
insert into 自动取款机 values('6222023100051898573',110.00)
go
--转账 余额不足 数据回滚 转账失败
begin try
begin tran
update 自动转账机 set 金额 = 金额+500 where 卡号 = '6220203200051898533'
update 自动转账机 set 金额 = 金额-500 where 卡号 = '6220203200051898573'
commit tran --执行成功
end try
begin catch
rollback tran --余额不足 数据回滚
end catch
go
--取款 余额不足 数据回滚
begin try
begin tran
update 自动取款机 set 金额 = 金额-500 where 卡号 = '6220203200051898573'
commit tran --执行成功
end try
begin catch
rollback tran --余额不足 数据回滚
end catch
go
select * from 自动转账机
select * from 自动取款机
go
drop table 自动转账机
drop table 自动取款机
go
--建表
create table card1 (cardid int,remain float)
go
create procedure mypro1
@incount int,
@outcount int,
@amount float
as
begin
declare @total float
select @total=remain from card1 where cardid=@outcount
if @total>=@amount
begin
update card1
set remain=remain-@amount where cardid=@outcount
update card1
set remain=remain+@amount where cardid=@incount
end
end
go
insert card1 values(1,1000.0)
insert card1 values(2,500.0)
go
create table card2(cardid int,remain float)
go
create procedure mypro2
@outid int,
@inid int,
@money float
as
begin
declare @total float
select @total=remain from card2 where cardid=@outid
begin transaction t1--设回滚点
if @total>@money
begin
update card2
set remain=remain-@money where cardid=@outid
update card2
set remain=remain+@money where cardid=@inid
print'转账成功'
commit transaction t1--提交事务
end
else
begin
print '转账失败'
rollback transaction t1--回滚
end
end
go
insert card2 values(1,1000.0)
insert card2 values(2,1000.0)
go
exec mypro2 2,1,1689
go
select * from card2
go
--创建索引
create nonclustered(表示非聚集索引,索引的一种类型) index meterno_alertcode_index on tbl(meterno,alertcode)
索引的类型:
nonclustered--非聚集索引
clustered--聚集索引
unique--唯一索引
go
--触发器
inserted和deleted 是触发器中的临时表
inserted 存放的是插入的数据
deleted 存放的是删除数据
插入时inserted表中有数据,deleted 中无数据
更新时,inserted表存放的是更新后的数据,deleted保存的是更新前的数据
删除时,inserted表中无数据,deleted保存删除的数据
--创建触发器
ALTER TRIGGER [dbo].[EnergyDataSumByHourNForCOP]
ON [dbo].[EnergyDataSumByHourN]
for INSERT,DELETE,UPDATE
AS
BEGIN
END
--跨库导数据
declare @ID int
declare @DAYear char(4)
declare @DAMonth char(2)
declare @DADay char(2)
declare @DATime datetime
declare @DAHour char(2)
declare @TemQty decimal(18, 6)
declare @HumQty decimal(18, 6)
declare @Note nvarchar(50)
declare @Lable int
declare TemHumCursor cursor for select *
from openrowset('SQLOLEDB','192.168.250.66';'sa';'tjdx',newcems.dbo.TemHum)
where DATime >'2012-11-26 12:12:40.527' order by DATime desc
open TemHumCursor
fetch next from TemHumCursor into @ID,@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,@Lable
while @@fetch_status=0
begin
insert into TemHum (DAYear,DAMonth,DADay,DATime,DAHour,TemQty,HumQty,Note,Lable) values (@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,0)
fetch next from TemHumCursor into @ID,@DAYear,@DAMonth,@DADay,@DATime,@DAHour,@TemQty,@HumQty,@Note,@Lable
end
close TemHumCursor
deallocate TemHumCursor
set nocount on
select * from TemHum order by DATime desc
go
--字段值减一
update ResultList set mydatetime = cast(cast(mydatetime as int)-1 as varchar(50))
select * from ResultList
go
--事故处理
create table 自动转账机
(
卡号 varchar(19) primary key,
金额 money check(金额>=0)
)
insert into 自动转账机 values('6222023100051898533',120.00)
insert into 自动转账机 values('6222023100051898573',0.00)
go
create table 自动取款机
(
卡号 varchar(19) primary key,
金额 money check(金额>=100)
)
delete from 自动取款机
go
insert into 自动取款机 values('6222023100051898533',120.00)
insert into 自动取款机 values('6222023100051898573',110.00)
go
--转账 余额不足 数据回滚 转账失败
begin try
begin tran
update 自动转账机 set 金额 = 金额+500 where 卡号 = '6220203200051898533'
update 自动转账机 set 金额 = 金额-500 where 卡号 = '6220203200051898573'
commit tran --执行成功
end try
begin catch
rollback tran --余额不足 数据回滚
end catch
go
--取款 余额不足 数据回滚
begin try
begin tran
update 自动取款机 set 金额 = 金额-500 where 卡号 = '6220203200051898573'
commit tran --执行成功
end try
begin catch
rollback tran --余额不足 数据回滚
end catch
go
select * from 自动转账机
select * from 自动取款机
go
drop table 自动转账机
drop table 自动取款机
go
--建表
create table card1 (cardid int,remain float)
go
create procedure mypro1
@incount int,
@outcount int,
@amount float
as
begin
declare @total float
select @total=remain from card1 where cardid=@outcount
if @total>=@amount
begin
update card1
set remain=remain-@amount where cardid=@outcount
update card1
set remain=remain+@amount where cardid=@incount
end
end
go
insert card1 values(1,1000.0)
insert card1 values(2,500.0)
go
create table card2(cardid int,remain float)
go
create procedure mypro2
@outid int,
@inid int,
@money float
as
begin
declare @total float
select @total=remain from card2 where cardid=@outid
begin transaction t1--设回滚点
if @total>@money
begin
update card2
set remain=remain-@money where cardid=@outid
update card2
set remain=remain+@money where cardid=@inid
print'转账成功'
commit transaction t1--提交事务
end
else
begin
print '转账失败'
rollback transaction t1--回滚
end
end
go
insert card2 values(1,1000.0)
insert card2 values(2,1000.0)
go
exec mypro2 2,1,1689
go
select * from card2
go
--创建索引
create nonclustered(表示非聚集索引,索引的一种类型) index meterno_alertcode_index on tbl(meterno,alertcode)
索引的类型:
nonclustered--非聚集索引
clustered--聚集索引
unique--唯一索引
go
--触发器
inserted和deleted 是触发器中的临时表
inserted 存放的是插入的数据
deleted 存放的是删除数据
插入时inserted表中有数据,deleted 中无数据
更新时,inserted表存放的是更新后的数据,deleted保存的是更新前的数据
删除时,inserted表中无数据,deleted保存删除的数据
--创建触发器
ALTER TRIGGER [dbo].[EnergyDataSumByHourNForCOP]
ON [dbo].[EnergyDataSumByHourN]
for INSERT,DELETE,UPDATE
AS
BEGIN
END
相关文章推荐
- python数据库操作常用功能使用详解(创建表/插入数据/获取数据)
- python数据库操作常用功能使用详解(创建表/插入数据/获取数据)
- Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理
- 使用JavaScript操作DOM节点元素的常用方法(创建/删除/替换/复制等)
- java29.XML操作------使用java DOM方式创建XML数据
- AR Drone系列之:使用ROS catkin创建package并使用cv_bridge实现对ar drone摄像头数据的处理
- Redis常用数据类型介绍、使用场景及其操作命令
- IOS 之使用FMDB进行SQLite数据库操作——表的创建与修改,以及数据的增删改查和多线程操作数据库
- 申请专利使用Altium Designer处理电路图的常用操作
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- Redis常用数据类型介绍、使用场景及其操作命令
- oracle表空间,角色,权限,表,索引,序列号,视图,同义词,约束条件,存储函数和过程,常用数据字典,基本数据字典信息,查看VGA信息,维护表空间,创建表空间等信息
- Android 中 Handler 引起的内存泄露 在Android常用编程中,Handler在进行异步操作并处理返回结果时经常被使用。其实这可能导致内存泄露,代码中哪里可能导致内存泄露,又是如何
- 使用触发器实现数据库级守护,防止DDL操作
- IOS 之使用FMDB进行SQLite数据库操作——表的创建与修改,以及数据的增删改查和多线程操作数据库
- 使用触发器实现数据库级守护,防止DDL操作
- 使用 SQL SERVER 2005 新增功能--- DDL_DATABASE_LEVEL_EVENTS 数据库级的触发器,创建数据库日志表