Search
 
SCRIPT & CODE EXAMPLE
 

SQL

CREATE PROCEDURE sql

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

SQL Creating a Procedure

CREATE PROCEDURE us_customers AS
SELECT customer_id, first_name
FROM Customers
WHERE Country = 'USA';
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql select all table that have field names 
Sql :: join multiple tables 
Sql :: cast float mysql 
Sql :: print pl sql 
Sql :: sqlite describe table 
Sql :: how to use db.execute 
Sql :: sql int size 
Sql :: pl/sql how to delete a trigger 
Sql :: raiserror sql 
Sql :: sql query by rahuldev 
Sql :: criteria builder select subset of column 
Csharp :: reload scene unity 
Csharp :: unity mouse lock 
Csharp :: convert system.byte a string c# 
Csharp :: c# how to run external program 
Csharp :: unity how to get y value 
Csharp :: check if string is email c# 
Csharp :: how to run an external program with c# 
Csharp :: Getting data from selected datagridview row and which event 
Csharp :: set width of rect transform unity 
Csharp :: Change fog setting Unity 
Csharp :: how to get the startup path in console app 
Csharp :: unity distance between 2 vectors 2d 
Csharp :: add two numbers in c# 
Csharp :: c# string to double 
Csharp :: how to convert angle to vector in c# 
Csharp :: how o remove .meta files visual studio code 
Csharp :: unity lookrotation lerp 
Csharp :: compute months c# 
Csharp :: unity how to add force 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =