18. Stored Procedure--sql

Source: Internet
Author: User

good text to top :

    • Advantages and disadvantages of SQL stored procedures
    • SQL Server Stored Procedures

I. Creating and using Stored procedures

Oracle version:

CREATE PROCEDUREMailinglistcount (ListCount outINTEGER) isv_rowsINTEGER;BEGINSELECT COUNT(*) intov_rows fromCustomersWHERE  notCust_email is NULL; ListCount:=v_rows;END;

Analysis
This stored procedure has a parameter named ListCount. This parameter returns a value from the stored procedure instead of passing a value to the stored procedure. The keyword out is used to indicate this behavior.
Oracle supports in (pass value to stored procedure), out (return value from stored procedure, as here), INOUT (pass value to stored procedure also returns value from stored procedure) type parameter
Number. The code for the stored procedure is enclosed in the begin and end statements, where a simple SELECT statement is executed that retrieves the customer with the mail address. Then use the number of rows retrieved to set the
ListCount (the output parameter to be passed).

Use:

var  Number EXEC Mailinglistcount (: returnvalue); SELECT returnvalue;

Analysis
This code declares a variable to hold any value returned by the stored procedure, executes the stored procedure, and then uses the SELECT statement to display the returned value.

SQL Server version:

 create  procedure   Mailinglistcount  as  declare  Span style= "color: #008000;" > @cnt  integer  select   @cnt  =  count  (*  )  from   where  not  Cust_email Span style= "color: #0000ff;" >is  null  ;  return   @cnt ; 

Analysis
There are no parameters for this stored procedure. The calling program retrieves the values supported by the return code of SQL Server. Where a local variable named @cnt is declared with the DECLA RE statement (in SQL Server
All local variable names begin with @), then use this variable in the SELECT statement to include the value returned by the count () function, and finally, return @cnt statement to return the count to
Back to the calling program.

Call:

DECLARE @ReturnValue INT EXECUTE @ReturnValue = Mailinglistcount; SELECT @ReturnValue;

18. Stored Procedure--sql

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.