set RowCount 與 top n,setrowcounttop

來源:互聯網
上載者:User

set RowCount 與 top n,setrowcounttop

有的時候,使用top n中的n是一個變數,那就需要用()來完成:

declare @count1 intset @count1 = 8select top <strong>(@count1)</strong> * from MyTable


如果用set RowCount一樣可以達到相同的效果

declare @count1 intset @count1 = 8        set RowCount @count1select * from MyTable

但注意,使用set RowCount有副作用的,一旦設定了,對於後面的語句都會影響到,而且SQL Server在後續的版本中對此有更改,所以要小心使用。
重要提示:
在 SQL Server 的下一個版本中,使用 SET ROWCOUNT 將不會影響 DELETE、INSERT 和 UPDATE 語句。在新的開發工作中,避免將 SET ROWCOUNT 語句與 DELETE、INSERT 和 UPDATE 語句一起使用,並計劃修改當前使用該語句的應用程式。另外,對於當前使用 SET ROWCOUNT 的 DELETE、INSERT 和 UPDATE 語句,建議您使用 TOP 文法重寫它們。有關詳細資料,請參閱 DELETE (Transact-SQL)、INSERT (Transact-SQL) 或 UPDATE (Transact-SQL)。


SQL文法中as與set

as為定義名稱,就是這個欄位的別名,頁首上顯示的名稱.
set為變數賦值.
 
sqlserver預存程序中,set rowcount 0是什?

一般在語句中使用set rowcount是為了使後續的查詢、更新、刪除操作隻影響指定的行數
比如 一起執行如下語句

set rowcount 1
SELECT * FROM sysobjects
結果只返回一行,而如果不加set rowcount 1或者使用set rowcount 0
就會返回所有結果

我想你見到的預存程序裡包含set rowcount 0的情況是因為之前應該包含set rowcount 1之類大於0的set rowcount 設定,是為了使set rowcount 0後邊的語句受影響的行數為全部記錄。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.