【非常感谢】table里如何使用下一条记录更新上一条记录,不使用游标。
CREATE TABLE [dbo].Table1(
[TitleId] [int] IDENTITY(1,1) NOT NULL,
[Title] [varchar](200) COLLATE Chinese_PRC_CI_AS NULL,
[TitleCode] [uniqueidentifier] NULL,
[DirName] [varchar](100) COLLATE Chinese_PRC_CI_AS NULL,
[PagePre] [uniqueidentifier] NULL,
。。。
一个故事有多条记录,dirname就是故事名称。titlecode是每条记录的编号。
一个故事的所有记录的顺序是按照titleid排序的。
目的:更新PagePre字段。也就是更新当前记录的上一条记录的titlecode,如果该记录是但前故事的第一条记录,那么pagePre为NULL.
下面是我写的,这个语句我个人认为没问题,但是有错误的,根本查不出结果。
select b.titlecode
from Table1 as b
where b.dirname='芙蓉姐姐的故事' and b.titleid= (
select top 1 a.titleid
from Table1 as a where a.dirname='芙蓉姐姐的故事' and a.titleid>b.titleid
不想使用游标,谢谢大家的帮助,给出解决思路,最好能指出我的语句为什么是错误的。
再次表示感谢!
[解决办法]
楼主贴点数据出来
[解决办法]
下面是我写的,这个语句我个人认为没问题,但是有错误的,根本查不出结果。
select b.titlecode
from Table1 as b
where b.dirname= '芙蓉姐姐的故事 ' and b.titleid= (
select top 1 a.titleid
from Table1 as a where a.dirname= '芙蓉姐姐的故事 ' and a.titleid >b.titleid
select b.titlecode from Table1 as b where b.dirname= '芙蓉姐姐的故事 ' and exists ( select top 1 a.titleid from Table1 as a where a.dirname= '芙蓉姐姐的故事 ' and a.titleid >b.titleid
[解决办法]
select top 1
b.titlecode
from Table1 as b
where b.dirname= '芙蓉姐姐的故事'
and exists (select *
from Table1
where dirname= '芙蓉姐姐的故事' and titleid >b.titleid )
set nocount oncreate table table1( TitleID int Identity(1,1) not null, Title varchar(200) null, TitleCode uniqueidentifier null, DirName varchar(100) null, PagePre uniqueidentifier null)insert table1 (TitleCode, DirName)select newID(), 'FRJJ''s story'union all select newID(), 'FRMM''s story'union all select newID(), 'FRGG''s story'union all select newID(), 'FRDD''s story'union all select newID(), 'FRJJ''s story'union all select newID(), 'FRMM''s story'union all select newID(), 'FRGG''s story'union all select newID(), 'FRDD''s story'union all select newID(), 'FRJJ''s story'union all select newID(), 'FRMM''s story'union all select newID(), 'FRGG''s story'union all select newID(), 'FRDD''s story'GOupdate table1set PagePre = (select titlecode from table1where DirName = t.DirName AND titleid = (select max(titleID) from table1 where DirName = t.DirName AND titleid < t.titleid))from table1 tselect * from table1GOdrop table table1GO
[解决办法]
刚才在.net版玩,没看到...
借用 Generics 的测试数据
我将字段改了一下,方便我看替换后的结果是否正确.
create table table1( TitleID int Identity(1,1) not null, Title varchar(200) null, TitleCode varchar null, DirName varchar(100) null, PagePre varchar null)insert table1 (TitleCode, DirName)select 'a', 'FRJJ''s story'union all select 'b', 'FRMM''s story'union all select 'c', 'FRGG''s story'union all select 'd', 'FRDD''s story'union all select 'e', 'FRJJ''s story'union all select 'f', 'FRMM''s story'union all select 'g', 'FRGG''s story'union all select 'h', 'FRDD''s story'union all select 'i', 'FRJJ''s story'union all select 'j', 'FRMM''s story'union all select 'k', 'FRGG''s story'union all select 'l', 'FRDD''s story'update a set PagePre=b.TitleCode from table1 aleft join table1 b on a.DirName=b.DirName and a.titleID>b.titleID and not exists(select 1 from table1 c where c.dirname=a.dirname and c.titleID>b.titleID and c.titleID<a.TitleID)select * from table1 drop table table1/*1 NULL a FRJJ's story NULL2 NULL b FRMM's story NULL3 NULL c FRGG's story NULL4 NULL d FRDD's story NULL5 NULL e FRJJ's story a6 NULL f FRMM's story b7 NULL g FRGG's story c8 NULL h FRDD's story d9 NULL i FRJJ's story e10 NULL j FRMM's story f11 NULL k FRGG's story g12 NULL l FRDD's story h*/