Search
 
SCRIPT & CODE EXAMPLE
 

SQL

stored procedure sql

-- without parameters; create
CREATE [OR ALTER] PROCEDURE <pname>
AS
BEGIN
<PROCEDURE BODY/ STATEMENTS>
END

-- call procedure
EXECUTE / EXEC <pname>;
(OR)
EXECUTE / EXEC <pname> VALUE / (S)
Comment

Explain the Stored Procedure

Stored Procedure :
A stored procedure is a prepared SQL code that you can save, 
so the code can be reused over and over again.

So if you have an SQL query that you write over and over again, 
save it as a stored procedure, and then just call it to execute it.

You can also pass parameters to a stored procedure, 
so that the stored procedure can act based on the parameter value(s) 
that is passed.
Comment

stored procedure sql

-- without parameters; create
CREATE [OR ALTER] PROCEDURE <pname>
AS
BEGIN
<PROCEDURE BODY/ STATEMENTS>
END

-- call procedure
EXECUTE / EXEC <pname>;
(OR)
EXECUTE / EXEC <pname> VALUE / (S)
Comment

stored procedure

CREATE TABLE Products
(
    Id INT IDENTITY PRIMARY KEY,
    ProductName NVARCHAR(30) NOT NULL,
    Manufacturer NVARCHAR(20) NOT NULL,
    ProductCount INT DEFAULT 0,
    Price MONEY NOT NULL
);

--1
GO
CREATE PROCEDURE AddProduct
    @name NVARCHAR(20),
    @manufacturer NVARCHAR(20),
    @count INT,
    @price MONEY
AS
INSERT INTO Products(ProductName, Manufacturer, ProductCount, Price) 
VALUES(@name, @manufacturer, @count, @price)

DECLARE @prodName NVARCHAR(20), @company NVARCHAR(20);
DECLARE @prodCount INT, @price MONEY
SET @prodName = 'Galaxy C7'
SET @company = 'Samsung'
SET @price = 22000
SET @prodCount = 5 
EXEC AddProduct @prodName, @company, @prodCount, @price 
SELECT * FROM Products

--2
GO
CREATE PROCEDURE AddProductWithOptionalCount
    @name NVARCHAR(20),
    @manufacturer NVARCHAR(20),
    @price MONEY,
    @count INT = 1
AS
INSERT INTO Products(ProductName, Manufacturer, ProductCount, Price) 
VALUES(@name, @manufacturer, @count, @price)
DECLARE @prodName NVARCHAR(20), @company NVARCHAR(20), @price MONEY
SET @prodName = 'Redmi Note 5A'
SET @company = 'Xiaomi'
SET @price = 22000 
EXEC AddProductWithOptionalCount @prodName, @company, @price 
SELECT * FROM Products

--3
GO
CREATE PROCEDURE GetPriceStats
    @minPrice MONEY OUTPUT,
    @maxPrice MONEY OUTPUT
AS
SELECT @minPrice = MIN(Price),  @maxPrice = MAX(Price)
FROM Products
DECLARE @minPrice MONEY, @maxPrice MONEY 
EXEC GetPriceStats @minPrice OUTPUT, @maxPrice OUTPUT 
PRINT 'Минимальная цена ' + CONVERT(VARCHAR, @minPrice)
PRINT 'Максимальная цена ' + CONVERT(VARCHAR, @maxPrice)

--4
GO
 
CREATE PROCEDURE CreateProduct
    @name NVARCHAR(20),
    @manufacturer NVARCHAR(20),
    @count INT,
    @price MONEY,
    @id INT OUTPUT
AS
    INSERT INTO Products(ProductName, Manufacturer, ProductCount, Price)
    VALUES(@name, @manufacturer, @count, @price)
    SET @id = @@IDENTITY
    
 
DECLARE @id INT 
EXEC CreateProduct 'LG V30', 'LG', 3, 28000, @id OUTPUT 
PRINT @id

--5
GO
CREATE PROCEDURE GetAvgPrice AS
DECLARE @avgPrice MONEY
SELECT @avgPrice = AVG(Price)
FROM Products
RETURN @avgPrice;

DECLARE @result MONEY 
EXEC @result = GetAvgPrice
PRINT @result
Comment

stored procedure

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace AutomationApp
{
    class Program
    {
        public void RunStoredProc()
        {
            SqlConnection conn = null;
            SqlDataReader rdr  = null;

            Console.WriteLine("
Top 10 Most Expensive Products:
");

            try
            {
                conn = new SqlConnection("Server=(local);DataBase=master;Integrated Security=SSPI");
                conn.Open();
                SqlCommand cmd = new SqlCommand("dbo.test", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                rdr = cmd.ExecuteReader();
                /*while (rdr.Read())
                {
                    Console.WriteLine(
                        "Product: {0,-25} Price: ${1,6:####.00}",
                        rdr["TenMostExpensiveProducts"],
                        rdr["UnitPrice"]);
                }*/
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Program p= new Program();
            p.RunStoredProc();      
            Console.Read();
        }
    }
}
Comment

what is a stored procedure

SQL Stored Procedure
Comment

PREVIOUS NEXT
Code Example
Sql :: SQL sort on a calculation 
Sql :: oracle temp tablespace size 
Sql :: is not null mysql 
Sql :: condition in orderby mysql 
Sql :: mysql view command 
Sql :: convert google analytics dathourminute to time stamp? sql 
Sql :: bigquery routine 
Sql :: Adding a new table psql 
Sql :: mysql stand for 
Sql :: select all users sql 
Sql :: kill mysqld_safe process mariadb 
Sql :: jooq finding lastest value in table 
Csharp :: c# mark as deprecated 
Csharp :: how to make a hello world program in c# 
Csharp :: c# store byte array as string 
Csharp :: how to detect a mouse click in unity 
Csharp :: how to edit postprocessing through script 
Csharp :: object spin unity 
Csharp :: c# main file 
Csharp :: how to disable and enable rigidbody unity 
Csharp :: rigidbody2d freeze position 
Csharp :: Animator.GotoState: State could not be found 
Csharp :: c# get directory of executable 
Csharp :: c# create new thread 
Csharp :: open website c# 
Csharp :: random from list c# 
Csharp :: check if number is even or odd c# 
Csharp :: random number generator c# 
Csharp :: unity main texture not working 
Csharp :: c# require administrator permissions 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =