如何在select查询中使用set rowcount

时间:2016-10-07 08:02:51

标签: unix sybase

我有一个select查询语句,它将产生600k行。当我使用select语句盲目地提取结果时,它将影响db性能。是否可以使用Set rowcount来获取数据?我尝试了下面的代码,但它继续产生前50000行并最终进入无限循环。

#!/bin/ksh -x
trap "" 1
updrowcount=50000
while [ $updrowcount -eq 50000 ]
do
QUERY="set rowcount 50000
select subject into tempdb..extract from tablename where fldr_id=8"

runisql <<EOF > db_restenter code here
$QUERY
goenter code here
quit
EOF
updrowcount=`grep "rows affected" db_rest |cut -c2- | cut -f1 -d ' '`
done
exit

1 个答案:

答案 0 :(得分:0)

如果您的subject是唯一的,您可以尝试这样的事情:

set rowcount 50000

declare @subject varchar(...)
select @subject = max( subject ) from tempdb..extract

insert into tempdb..extract ( subject )
select subject 
from tablename
where fldr_id=8
and (subject > @subject OR @subject is null)
order by subject

否则,请使用唯一列,例如

set rowcount 50000

declare @maxId int
select @maxId = max( id ) from tempdb..extract

insert into tempdb..extract (id, subject )
select id, subject into tempdb..extract
from tablename
where fldr_id=8
and (id > @maxId OR @maxId is null)
order by id

(显然,您希望使用索引列。)