SQL Server database queries the number of records in a table by percentage _mssql

Source: Internet
Author: User

When querying a SQL Server database, can you query the number of records by percentage? The answer is yes. This article we introduce this implementation method.

The code to implement this feature is as follows:

CREATE PROCEDURE Pro_toppercent  
( 
 @ipercent [int] =0        --default does not return 
) as 
begin select Top 
(@ Ipercent) percent * from books 

Or

CREATE PROCEDURE Pro_toppercent  
( 
 @ipercent [int] =0  
) 
as 
begin 
Select Top (select COUNT (*) from Books) * (@ipercent)/100) * "From books end 
exec pro_toppercent '"-    Execute stored procedure

The syntax for creating stored procedures is similar to C # with pointers, when the parameter table is created with parentheses, output parameters are marked with the direction of transmission, input parameters are not used, parameter declaration format:

@studentname [nvarchar] (output)

The parameter table is not enclosed in parentheses when the stored procedure executes, and if there is an output parameter, it is declared first and executed in the following format:

DECLARE @studentname_1 
 
exec myprocedure 

' Input parameters ', @studentname_1 output, if the foreground is using. NET, you can add a delivery direction to an output SqlParameter parameter in Comand.parameters to receive the value.

About the SQL Server database to query the percentage of the number of records to the operation of the article here, I hope this introduction can bring you some harvest.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.