简体   繁体   English

有没有办法限制受UPDATE语句影响的行数?

[英]is there a way to limit the amount of rows that get affected by an UPDATE statement?

just wondering if there is any way for me to limit the amount of rows which will be affected by an UPDATE statement....I was thinking of using something like a LIMIT statement but there's no such thing in PERVASIVE, any tips? 只是想知道我是否有办法限制受UPDATE语句影响的行数....我在考虑使用像LIMIT语句这样的东西但在PERVASIVE中没有这样的东西,任何提示?

thank you 谢谢

It seems that this would work... 看来这会起作用......

UPDATE TableName SET ColumnName = value WHERE ID IN (SELECT TOP 100 ID FROM TableName WHERE CONDITION) UPDATE TableName SET ColumnName = value WHERE ID IN(从TableName WHERE CONDITION中选择前100个ID)

Make TOP 100 whatever your "limit" is. 无论你的“限制”是什么,都要获得TOP 100。 Then, just update the WHERE clause appropriately. 然后,只需适当更新WHERE子句。

The TOP 100 idea can work but depending on your data, it may skew either testing the correctness or performance. TOP 100想法可以工作,但根据您的数据,它可能会扭曲测试正确性或性能。 It can also be hard to write. 它也很难写。 I think adding a RANDOM -based clause works a little better. 我认为添加一个基于RANDOM的条款可以更好一些。

UPDATE t SET c = blah 
  WHERE (/* whatever bunch of stuff */) 
  AND Random()<0.01 /* 1 percent */

This works if your DB's random gives a value between zero and one, and can be modified for different random functions and densities. 如果您的数据库随机给出0到1之间的值,并且可以针对不同的随机函数和密度进行修改,则此方法有效。

If you're just being careful, why not just count the rows you would update? 如果你只是小心,为什么不计算你要更新的行?

select count(*) from MYTABLE WHERE ...some condition...;

If the count is sufficiently low, this is practically cut & paste to the update: 如果计数足够低,这实际上是剪切并粘贴到更新:

update MYTABLE set col1 = val1, ... WHERE ...some condition...;

Put into your scripting language, pl/sql, or stored procedure as is appropriate. 根据需要加入脚本语言,pl / sql或存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM