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

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 :: join sql 
Sql :: what is auto increment in sql 
Sql :: sql count * vs count 1 
Sql :: stuff in sql 
Sql :: test connection to sql server 
Sql :: timestamp type in sql 
Sql :: oracle insert multiple rows into same table 
Sql :: oracle alter table 
Sql :: Example SQL Test 
Sql :: sql from 
Sql :: lumen 
Sql :: sql table contains 
Sql :: sql Top 5 sutradara dengan filem terbanyak 
Sql :: break too long line yaml 
Sql :: online compiler for sql plus 
Sql :: mysql create link between tablesdatabase 
Sql :: concat column value of same user in mysql 
Sql :: mssql + bit + in python orm 
Sql :: storing RGBA in mysql db 
Sql :: fetching data from mysqldb 
Sql :: mysql use password error 
Sql :: <connectionStrings <add name="MainDB" connectionString="Data Source=multidc02.its.com.pk,47328;Initial Catalog=ITSGeneralApplications;User ID=ITSGeneralAdmin;Password=TDsHn6TTyJohXCe"/ </connectionStrings 
Sql :: no customers ordered 
Sql :: postgresql < ALL very slow 
Sql :: automated psql csv export script on windows 
Sql :: sql transact create cursor with dynamic tables 
Sql :: oracle sql date summer time 
Sql :: mysql beautifier terminla 
Sql :: mysql view command does not work - privileges problem 
Sql :: linq to sql converter online 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =