在 repeatable() 语句中使用当前日期?

Use the current date inside the repeatable() statement?

我目前正在构建一个存储过程,我们将每天 运行 一次对数据进行采样并查找异常情况。如果发现异常,我希望样本是可重复的,这样我就可以进行更深入的分析。

为此,我使用了 tablesample() repeatable() 语句:

select * 
from DataTable tablesample(50 rows) repeatable(cast(getdate() as int));

这给出了错误:

Invalid ROWS value or REPEATABLE seed in the TABLESAMPLE clause for table "DataTable". The value or seed must be an integer.

我也尝试过使用 convert(int, getdate()) 代替,但无济于事。我还确认在语句中使用普通整数文字是有效的。

你不能在那里使用变量或表达式。只有一个整数文字。您始终可以使用动态 SQL。例如

declare @seed int = cast(getdate() as int)
declare @sql nvarchar(max) = concat('SELECT * FROM Person.Person TABLESAMPLE (10 PERCENT) REPEATABLE (',@seed,');')

exec (@sql)