Search
 
SCRIPT & CODE EXAMPLE
 

SQL

Create Stored Procedure

CREATE PROCEDURE [dbo].[GetCoursesByStudentId]
    -- Add the parameters for the stored procedure here
    @StudentId int = null
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
select c.courseid, c.coursename,c.Location, c.TeacherId
from student s 
left outer join studentcourse sc on sc.studentid = s.studentid 
left outer join course c on c.courseid = sc.courseid
where s.studentid = @StudentId
END
Comment

Create Procedure

DELIMITER //
 CREATE PROCEDURE GetTop10Customers()
   BEGIN
	SELECT c.first_name, c.last_name, SUM(p.amount) as total_payments
	FROM customer c
	JOIN payment p
		ON c.customer_id = p.customer_id
	GROUP BY c.customer_id
	ORDER BY total_payments DESC
	limit 10;
   END //
 DELIMITER ;
Comment

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

create procedure

CREATE [OR REPLACE] PROCEDURE procedure_name 
[(parameter_name [IN | OUT | IN OUT] type [, ...])] 
{IS | AS} 
BEGIN 
  < procedure_body > 
END procedure_name;
Comment

PREVIOUS NEXT
Code Example
Sql :: sql function 
Sql :: run postgres docker 
Sql :: nested if in mysql 
Sql :: store select query result in variable sql server 
Sql :: sql server output parameter 
Sql :: sql get the name of user pc 
Sql :: download mysql 64 bit 
Sql :: mysql remove html tag 
Sql :: mysql set last_insert_id 
Sql :: mysql dump specific tables 
Sql :: postgres list databases 
Sql :: check postgresql version in rails console 
Sql :: create mysql database on windows 
Sql :: change mysql version to 5.7 in ubuntu 
Sql :: sql server change schema of a table 
Sql :: update with select postgresql 
Sql :: alter table name sql 
Sql :: postgres set null 
Sql :: oracle apex charging debug 
Sql :: incompatible sql_mode=only_full_group_by 
Sql :: how to inner join 4 tables in sql 
Sql :: SQL Multiple Cases 
Sql :: how to know the character set of an oracle databes 
Sql :: sqlite3 import csv 
Sql :: SQL server how to see user permissions on objects 
Sql :: truncate all tables 
Sql :: sql value exists in column 
Sql :: get number of columns sql 
Sql :: oracle list proxy users 
Sql :: update sqlite 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =