如果TOP后取得行数是动态的,这时需要用变量实现动态数值,但如果直接写成:
DECLARE @LineCount INT =10
SELECT TOP @LineCount * FROM sys.tables AS t
会返回错误:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '@LineCount'.
DECLARE @LineCount INT =2
SELECT TOP (@LineCount*2) * FROM sys.tables AS t
将能正常返回四行数据