修改SQL2000中表中的数据

lizhongen1986 2009-10-07 01:55:20
有一张表:
TrainTable
字段:
(车次) (站点) (站号)
cTrainName cStationName iStationNum
7173 北京 1
7173 焦作 2
7173 汉口 3
7173 武汉 4
......................(还有很多站)

1.若我要在‘北京’和‘焦作’之间添加一个站点 ‘天津’,怎样将它以下站点 的 站号各加 1

2.若我要删除‘焦作’怎样将它以下的站点的站号 各减 1
这个是我们正在进行的项目中的问题,希望高手给予详细解答一下,我试验了很多都不成功,请帮帮忙,很重要,谢谢。
我用的是SQL2000
...全文
163 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2009-10-07
  • 打赏
  • 举报
回复
-->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
*/
gsk09 2009-10-07
  • 打赏
  • 举报
回复

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

lizhongen1986 2009-10-07
  • 打赏
  • 举报
回复
是,可不可以不用存储过程做,因为我还要将其与C#相联系
百年树人 2009-10-07
  • 打赏
  • 举报
回复
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;
水族杰纶 2009-10-07
  • 打赏
  • 举报
回复
iStationNum 是不是每次都從1開始
依次遞增1
?
gsk09 2009-10-07
  • 打赏
  • 举报
回复

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
7761098 2009-10-07
  • 打赏
  • 举报
回复
不明白,你查一下上个贴了
navy887 2009-10-07
  • 打赏
  • 举报
回复
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
水族杰纶 2009-10-07
  • 打赏
  • 举报
回复
上帖不是回答了嗎

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧