22,298
社区成员
发帖
与我相关
我的任务
分享-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-07 14:08:12
set nocount on
if not object_id('tb') is null
drop table tb
Go
Create table tb([cTrainName] varchar(20),[cStationName] nvarchar(2),[iStationNum] int)
Insert tb
select '7173',N'北京',1 union all
select '7173',N'焦作',2 union all
select '7173',N'汉口',3 union all
select '7173',N'武汉',4
Go
if object_id('tri_test')is not null drop trigger tri_test
go
create trigger tri_test on tb
for insert,delete
as
if exists(select 1 from deleted)
update t set t.[iStationNum]=t.[iStationNum]-1 from tb t,deleted d where t.[iStationNum]>d.[iStationNum]
else
update t set t.[iStationNum]=t.[iStationNum]+1 from tb t,inserted i where t.[iStationNum]>=i.[iStationNum]
and t.[cStationName]!=i.[cStationName]
go
--添加
insert tb select '7173',N'天津',2
go
select * from tb order by iStationNum
/*
cTrainName cStationName iStationNum
-------------------- ------------ -----------
7173 北京 1
7173 天津 2
7173 焦作 3
7173 汉口 4
7173 武汉 5
*/
go
--刪除
delete tb where iStationNum=2
select * from tb order by iStationNum
/*
cTrainName cStationName iStationNum
-------------------- ------------ -----------
7173 北京 1
7173 焦作 2
7173 汉口 3
7173 武汉 4
*/
declare @TrainTable table ([cTrainName] int,[cStationName] varchar(10),[iStationNum] int)
insert into @TrainTable
select 7173,'北京',1 union all
select 7173,'焦作',2 union all
select 7173,'口',3 union all
select 7173,'武',4
declare @c as varchar(10),@s as varchar(10) ,@perS as varchar(10),@afterS as varchar(10)
,@snPer smallint,@snAfter smallint,@snAdd smallint
select @c='7173'--车次
,@perS = ''--前站
,@s='TianJing2'--插入或删除的站
,@afterS = '武'--后站
set @snPer =(select iStationNum from @TrainTable where cTrainName = @c and cStationName = @perS)
set @snAfter = (select iStationNum from @TrainTable where cTrainName = @c and cStationName = @afterS)
if (@snPer is not null) or (@afterS is not null)
begin
set @snAfter=isnull(@snAfter,isnull(@snPer,0)+1)
set @snPer=isnull(@snPer,@snAfter-1)
set @snAdd = (@snPer+@snAfter+1)/2
--Add
update @TrainTable set iStationNum = iStationNum + 1 where iStationNum >= @snAdd and cTrainName = @c
insert into @TrainTable values(@c,@s,@snAdd)
--delete
delete @TrainTable where cTrainName = @c and iStationNum = @snAdd
update @TrainTable set iStationNum = iStationNum - 1 where iStationNum > @snAdd and cTrainName = @c
end
create table [TrainTable]([cTrainName] int,[cStationName] varchar(4),[iStationNum] int)
insert [TrainTable]
select 7173,'北京',1 union all
select 7173,'焦作',2 union all
select 7173,'汉口',3 union all
select 7173,'武汉',4
go
create proc sp_test
(
@op varchar(1), --i-插入;d-删除
@st varchar(50), --要删除的站或者在哪个站前插入新站
@instr varchar(50) --插入的新站
)
as
declare @tnum int;
if @op='i'
begin
select @tnum=iStationNum from traintable where cStationName=@st;
update traintable set iStationNum=iStationNum+1
where iStationNum>=(select iStationNum from traintable where cStationName=@st)
insert traintable
select cTrainName,@instr,@tnum from traintable where cStationName=@st
end
if @op='d'
begin
select @tnum=iStationNum from traintable where cStationName=@st;
delete traintable where cStationName=@st;
update traintable set iStationNum=iStationNum-1
where iStationNum>@tnum;
end
go
exec sp_test 'i','焦作','天津'
go
select * from traintable order by iStationNum
/**
cTrainName cStationName iStationNum
----------- ------------ -----------
7173 北京 1
7173 天津 2
7173 焦作 3
7173 汉口 4
7173 武汉 5
(5 行受影响)
**/
exec sp_test 'd','焦作',''
go
select * from traintable order by iStationNum
/**
cTrainName cStationName iStationNum
----------- ------------ -----------
7173 北京 1
7173 天津 2
7173 汉口 3
7173 武汉 4
(4 行受影响)
**/
drop table traintable;
drop proc sp_test;
declare @TrainTable table ([cTrainName] int,[cStationName] varchar(10),[iStationNum] int)
insert into @TrainTable
select 7173,'北京',1 union all
select 7173,'焦作',2 union all
select 7173,'口',3 union all
select 7173,'武',4
declare @n smallint ,@cn as varchar(10) ,@Name as varchar(10)
select @cn='7173',@name='TianJing', @n = 2
--Add
update @TrainTable set iStationNum = iStationNum + 1 where iStationNum >= @n and cTrainName = @cn
insert into @TrainTable values(@cn,@name,@n)
select * from @TrainTable order by [iStationNum]
--delete
delete @TrainTable where cTrainName = @cn and iStationNum = @n
update @TrainTable set iStationNum = iStationNum - 1 where iStationNum > @n and cTrainName = @cn
select * from @TrainTable order by [iStationNum]
cTrainName cStationName iStationNum
----------- ------------ -----------
7173 北京 1
7173 TianJing 2
7173 焦作 3
7173 口 4
7173 武 5
cTrainName cStationName iStationNum
----------- ------------ -----------
7173 北京 1
7173 焦作 2
7173 口 3
7173 武 4
1.
Declare @cStationName varchar(20),@iStationNum int
SET @cStationName='北京' --这里输入添加的站点名称前一个站点的名称
select @iStationNum=iStationNum from TrainTable=@cStationName
update TrainTable set iStationNum=iStationNum+1 where iStationNum>@iStationNum
2.
Declare @cStationName varchar(20),@iStationNum int
SET @cStationName='焦作' --这里输入要删除站点名称
select @iStationNum=iStationNum from TrainTable=@cStationName
update TrainTable set iStationNum=iStationNum-1 where iStationNum>@iStationNum