SQL Server數(shù)據(jù)庫(kù)如何按百分比查詢出表中的記錄數(shù)
作者:蕭翎
本文我們主要介紹了SQL Server數(shù)據(jù)庫(kù)在一個(gè)表中按百分比查詢出記錄條數(shù)的方法及代碼示例,希望能夠?qū)δ兴鶐椭?/div>
SQL Server數(shù)據(jù)庫(kù)查詢時(shí),能否按百分比查詢出記錄的條數(shù)呢?答案是肯定的。本文我們就介紹這一實(shí)現(xiàn)方法。
實(shí)現(xiàn)該功能的代碼如下:
- create procedure pro_topPercent
- (
- @ipercent [int] =0 --默認(rèn)不返回
- )
- as
- begin
- select top (@ipercent ) percent * from books
- end
或
- create procedure pro_topPercent
- (
- @ipercent [int] =0
- )
- as
- begin
- select top((select COUNT (*) from books)*(@ipercent)/100) * from books
- end
exec pro_topPercent '10' --執(zhí)行存儲(chǔ)過(guò)程
創(chuàng)建存儲(chǔ)過(guò)程的語(yǔ)法類似帶指針的C#,創(chuàng)建時(shí)參數(shù)表用小括號(hào)括起,輸出參數(shù)帶傳遞方向的參數(shù)標(biāo)識(shí) OUTPUT,輸入?yún)?shù)不用,參數(shù)聲明格式:
(
@studentname [nvarchar] (50) output
)
存儲(chǔ)過(guò)程執(zhí)行時(shí)參數(shù)表不用加括號(hào),若有輸出參數(shù),先聲明,用如下格式執(zhí)行:
- declare @studentname_1
- exec myprocedure
'輸入?yún)?shù)',@studentname_1 output, 如果前臺(tái)用的是.net的話可以在comand.parameters中添加傳遞方向?yàn)閛utput的sqlparameter參數(shù)接收該值。
關(guān)于SQL Server數(shù)據(jù)庫(kù)按百分比查詢記錄條數(shù)的操作就介紹到這里,希望本次的介紹能夠給您帶來(lái)一些收獲,謝謝!
【編輯推薦】
責(zé)任編輯:趙鵬
來(lái)源:
博客園


相關(guān)推薦




