stored procedures in SQL Server

Source: Internet
Author: User

Three ways that stored procedures in SQL Server are returned (including the creation of stored procedures, calls in stored procedures, methods called in VS)
There are three types of stored procedures to return:
1. Return numeric data with return
2. Returns the result with the return parameter, can return various data types (through the cursor to iterate the result each row)
3. Directly in the stored procedure with a select to return the result set, can be any SELECT statement, which means that it is arbitrary to return the result set

Method One: Return the numeric data with return
1. Create a stored procedure
--sqlserver 2005 Sample Database
Use AdventureWorks
GO
CREATE PROCEDURE CheckState
@param VARCHAR (11)
As
IF (
SELECT StateProvince
From Person.vadditionalcontactinfo
WHERE ContactID = @param
) = ' WA '
RETURN 1
ELSE
RETURN 2;
GO
2. Calling in a stored procedure
DECLARE @return_status INT;
EXEC @return_status = CheckState ' 9 '; --Assign the stored procedure return value to @return_status
SELECT ' Return Status ' = @return_status;
GO
3. Call in VS
List<dbparameter> para = new list<dbparameter> ();
Para. ADD (New SqlParameter ("@param", 9));
SqlParameter pa = new SqlParameter ();
Pa. ParameterName = "@return";
Pa. SqlDbType = SqlDbType.Int;
Pa. Direction = ParameterDirection.ReturnValue;
Para. ADD (PA);
int i = dbhelper.executesql ("CheckState", CommandType.StoredProcedure, para);
Response.Write (Para[1]. Value.tostring ());


Method Two: Returns the result with the return parameter, can return various data types (through the cursor to iterate the query result each row)
1. Create a stored procedure
--sqlserver 2005 Sample Database
Use AdventureWorks
GO
CREATE PROCEDURE Outputvalue
@param VARCHAR (11),
@param2 VARCHAR (one) OUTPUT
As
IF (
SELECT StateProvince
From Person.vadditionalcontactinfo
WHERE ContactID = @param
) = ' WA '
SET @param2 = ' good '
ELSE
SET @param2 = ' bad '
GO
2. Calling in a stored procedure
DECLARE @param1 NVARCHAR (100)
DECLARE @param2 NVARCHAR (100)
SET @param1 = ' 9 '
EXEC outputvalue ' 9 ', @param2 OUTPUT
SELECT @param2
3. Call in VS
List<dbparameter> para = new list<dbparameter> ();
Para. ADD (New SqlParameter ("@param", "9"));
SqlParameter pa = new SqlParameter ();
Pa. Direction = ParameterDirection.Output;
Pa. ParameterName = "@param2";
Pa. Size = 11;
Para. ADD (PA);
int i = dbhelper.executesql ("Outputvalue", CommandType.StoredProcedure, para);
Output return value
Response.Write (Para[1].  Value.tostring ()); Output return value


Method Three: Directly in the stored procedure with the Select return result set, can be any SELECT statement, which means that it is arbitrary to return the result set
1. Create a stored procedure
--sqlserver 2005 Sample Database
Use AdventureWorks
GO
CREATE PROCEDURE returndatatable
As
BEGIN
SELECT * from Person.vadditionalcontactinfo
END
GO
2. Calling in a stored procedure
EXEC returndatatable
3. Call in VS
Stored procedure Returns a result set that can be stored in a DataTable
DataTable dt = dbhelper.getdatatable ("returndatatable", CommandType.StoredProcedure);

stored procedures in SQL Server

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.