日期:2014-05-18  浏览次数:20692 次

【非常感谢】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 

SQL code
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

------解决方案--------------------
SQL code
select top 1  
  b.titlecode
from Table1 as b
where b.dirname= '芙蓉姐姐的故事'
  and exists (select *
        from Table1 
        where dirname= '芙蓉姐姐的故事' and titleid >b.titleid )

------解决方案--------------------
不是非常明白LZ的意思, 如果是利用前一条同样DirName的记录来更新后一条记录(PagePre存前一条titleCode的值?),那么下面的query可以做到这一点(因为我的SQL是英文系统的, 所以弄不了中文):
SQL code

set nocount on
create 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'
GO

update table1
set PagePre = (select titlecode from table1
where DirName = t.DirName AND titleid = 
(select max(titleID) from table1 where DirName = t.DirName AND titleid < t.titleid))
from table1 t

select * from table1

GO
drop table table1
GO

------解决方案--------------------
刚才在.net版玩,没看到...
借用 Generics 的测试数据
我将字段改了一下,方便我看替换后的结果是否正确.

SQL code

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 s