好久没写过博客了,梳理下基础知识,复习下。
--
跨库导数据
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