Professional Documents
Culture Documents
Stored Procedure Dan Dynamic SQL
Stored Procedure Dan Dynamic SQL
CREATE PROCEDURE
Production.Productproc
AS
SELECT productid,
productname,
categoryid,
unitprice,
discontinued
FROM Production.Products
ORDER BY productid;
GO
CREATE STORED PROCEDURE DENGAN
PARAMETER
• syntax
EXEC Production.ProductsbySuppliers
EXEC Production.ProductsbySuppliers
@supplierid = 1, @numrows = 2
MENJALANKAN STORED PROCEDURE
CREATE PROCEDURE Production.Productproc
AS
SELECT productid,
productname,
categoryid,
unitprice,
discontinued
FROM Production.Products
ORDER BY productid;
GO
EXEC Production.Productproc;
MENJALANKAN STORED PROCEDURE (1)
CREATE PROCEDURE Production.ProductsbySuppliers
(@supplierid AS INT)
AS
SELECT productid,
productname,
categoryid,
unitprice,
discontinued
FROM Production.Products
WHERE supplierid = @supplierid
ORDER BY productid;
GO
EXEC Production.ProductsbySuppliers
@supplierid = 1;
MENJALANKAN STORED PROCEDURE (2)
CREATE PROCEDURE Production.TopProductsbySuppliers
(@numrows AS INT, @supplierid AS INT)
AS
SELECT TOP (@numrows) productid,
productname,
categoryid,
unitprice,
discontinued
FROM Production.Products
WHERE supplierid = @supplierid
ORDER BY unitprice DESC;
GO
EXEC Production.TopProductsbySuppliers
@numrows = 2, @supplierid=1;
MENGHAPUS STORED PROCEDURE
• Menggunakan perintah DROP PROCEDURE
IF OBJECT_ID('Production.ProductsbySuppliers','P') IS NOT
NULL DROP PROCEDURE Production.ProductsbySuppliers;
SQL DINAMIS
• Terdapat 2 metode:
Kesimpulan: