問題描述
我正在使用 sp_MSforeachtable
來獲取數(shù)據(jù)庫中特定表的行數(shù).我想要這些按名稱排序.
I am using sp_MSforeachtable
to get a rowcount of specific tables in my database. I want these ordered by name.
如何向 sp_MSforeachtable
添加 ORDER BY
子句?
How do I add an ORDER BY
clause to sp_MSforeachtable
?
推薦答案
我知道這個(gè)問題已經(jīng)有 10 多年的歷史了,但它有超過 3000 次訪問和一堆錯(cuò)誤答案.我將重新調(diào)整 Chris R. 的答案,希望將其標(biāo)記為已接受的答案,而不是過于復(fù)雜的半頁 SQL 或你不能"的答案.我?guī)е耆嗤膯栴}來到這里,所以它仍然是相關(guān)的,顯然并不簡(jiǎn)單.
I understand this question is over 10 years old, but it has over 3000 visits and a bunch of wrong answers. I'm going to repurpose Chris R.'s answer in hopes of getting this marked as the accepted answer, instead of overly-complicated half-pages of SQL or "you can't" answers. I came here with the exact same question so it's still relevant and obviously not simple.
使用@whereand
參數(shù)指定一個(gè)ORDER BY
子句,該參數(shù)的內(nèi)容被附加到內(nèi)部SELECT
的末尾> 通過存儲(chǔ)過程中的簡(jiǎn)單 + @whereand
語句.而在 ORDER BY 1
中使用 1
表示按第一列排序.
Use the @whereand
parameter to specify an ORDER BY
clause, The contents of that parameter are tacked on to the end of the internal SELECT
statement via a simple + @whereand
in the stored proc. And using 1
in ORDER BY 1
means to order by the first column.
sp_MSforeachtable @command1='SELECT COUNT(*) AS ''?'' FROM ?', @whereand = 'ORDER BY 1'
這篇關(guān)于sp_MSforeachtable 排序依據(jù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!