如何在不使用临时表的情况下对一个主键未知的表用一条SQL查出第100条到最后的所有纪录

progame 2004-03-10 08:58:39
如题,如果有主键,可以用not in
不知道主键呢? 怎么办?
查第100~200是可以,可是我要查100到最后的所有纪录呢

请指教
...全文
61 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyoumei 2004-03-17
  • 打赏
  • 举报
回复
N_chow(xml beginner) 做的是不是可以??
这种方法我没见过~但是我知道用定义变量,做循环~当变量为空回滚就知道他的列数了~
whysun 2004-03-17
  • 打赏
  • 举报
回复

Jianli2004 2004-03-16
  • 打赏
  • 举报
回复
好久不见飘香了。。。。
progame 2004-03-16
  • 打赏
  • 举报
回复
大家注意是一条SQL语句完成
因为这个语句必须可以被其它SQL语句嵌套
chludlf 2004-03-16
  • 打赏
  • 举报
回复

alter table b add [ident] [int] IDENTITY (1, 1) NOT NULL
select * from b where ident>100
alter table drop column ident
N_chow 2004-03-15
  • 打赏
  • 举报
回复
try this.


DECLARE @p1 int
SET @p1=null
DECLARE @p2 int
SET @p2=98305
DECLARE @p3 int
SET @p3 =311300
DECLARE @p4 int

DECLARE @cnt int


EXEC sp_cursoropen @p1 OUT,N'SELECT * FROM tableName,@p2 OUTPUT,@p3 OUTPUT,@p4 OUTPUT

SET @cnt=@p4-100

EXEC sp_cursorfetch @p1,16,101,@cnt

EXEC sp_cursorclose @p1
zheninchangjiang 2004-03-15
  • 打赏
  • 举报
回复
错了,rollback后就连#tmp也没了
zheninchangjiang 2004-03-15
  • 打赏
  • 举报
回复
begin tran
set rowcount 2
delete from t
set rowcount 0
select * into #tmp from t
rollback
select * from #tmp
--这样不知可不可以,我没调试,没有好办法就歪的来
progame 2004-03-15
  • 打赏
  • 举报
回复
把所有字段列出来这个条件在我的要求中难以满足
因为我并不知道它的所有字段(不过极端一点来说,查系统表是可以得到的,可是这样就得动态sql语句了,这种同样是不行的)
所以Jianli2004(健力)(乡村工程师) 的方法是不可行的
不过还是谢谢你
---涛声依旧--- 2004-03-14
  • 打赏
  • 举报
回复
學習
Jianli2004 2004-03-14
  • 打赏
  • 举报
回复
标准SQL基本语句不支持top,麻烦,则要把TOP改写,根据排序字段
select top 4 * from #1
==》
select * from #1 a where
(select count(*) from #1 where id<=a.id and num<=a.num ) <=4
(要没有重复记录,而且按照排序选)

根据排序字段,这样可以吗?
select * from (
select *,seq=(select count(*) from #1 where id<=a.id and num<=a.num ) from #1 as a) as a
where seq>5
order by id,num
Jianli2004 2004-03-14
  • 打赏
  • 举报
回复
不难,不过效率是不可能高的

以pubs 下的sales表为例,查找第5条到结束的

select a.* from sales a left join (select top 4 * from sales) b on
a.stor_id=b.stor_id and
a.ord_num=b.ord_num and
a.ord_date=b.ord_date and
a.qty=b.qty and
a.payterms=b.payterms and
a.title_id=b.title_id
where b.stor_id is null and
b.ord_num is null and
b.ord_date is null and
b.qty is null and
b.payterms is null and
b.title_id is null


需要把所有列都要列举出来!


自编测试数据:(可适用于有重复记录的)
create table #1(id int,num int)
insert #1 select 1,1
insert #1 select 1,1
insert #1 select 2,2
insert #1 select 3,3
insert #1 select 4,4
insert #1 select 5,5
insert #1 select 6,6


查第5到结束:

select a.* from #1 a left join (select top 4 * from #1) b on
a.id=b.id and
a.num=b.num
where b.id is null and
b.num is null

结果:
id num
----------- -----------
4 4
5 5
6 6

(3 row(s) affected)

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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