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 :: REMOVE DATE FROM DATE TIME SQL SERVER 
Sql :: cross join sl 
Sql :: learn sql 
Sql :: postgres create trigger if not exists 
Sql :: example database query 
Sql :: cte in sql server 
Sql :: long string type sql 
Sql :: violation of primary key constraint 
Sql :: how to install mssql on mac 
Sql :: clone row from another table mysql 
Sql :: mysql copy data from one table to another 
Sql :: mysql trim characters 
Sql :: sql recherche nom prenom 
Sql :: how to use join with 3 tables in sql server 
Sql :: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , , = or when the subquery is used as an expression. 
Sql :: import mysql db 
Sql :: update table sql 
Sql :: postgresql add not null and not empty constraint 
Sql :: mysql run file command 
Sql :: CREATE table schema using select 
Sql :: sql insert all 
Sql :: select sql 
Sql :: not in sql 
Sql :: postgresql cast string to int 
Sql :: sql offfset 
Sql :: make a socket server to detect changes in mysql 
Sql :: mysql create index lost connection 
Sql :: mysql set session timeout 
Sql :: difference between ltrim and rtrim in sql server 
Sql :: como leer datos de mysql esp32 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =