parameters in sql code example
Example 1: set parameter sql server
USE AdventureWorks2012;
GO
EXEC dbo.uspGetWhereUsedProductID 819, '20050225';
GO
DECLARE @ProductID int, @CheckDate datetime;
SET @ProductID = 819;
SET @CheckDate = '20050225';
EXEC dbo.uspGetWhereUsedProductID @ProductID, @CheckDate;
GO
EXEC dbo.uspGetWhereUsedProductID 819, GETDATE();
GO
DECLARE @CheckDate datetime;
SET @CheckDate = GETDATE();
EXEC dbo.uspGetWhereUsedProductID 819, @CheckDate;
GO
Example 2: functions with parameters SQL
USE tempdb;
GO
DROP FUNCTION IF EXISTS dbo.GetOrderID;
DROP TABLE IF EXISTS OrdersTest;
GO
CREATE TABLE OrdersTest (OrderID int IDENTITY, OrderType int, Qty int, ServiceSpeed int);
GO
CREATE FUNCTION GetOrderID
(
@OrderType int = 0,
@ServiceSpeed int = 0,
@Qty int = 0
)
RETURNS int AS
BEGIN
RETURN
(
SELECT
TOP 1 OrderID
FROM OrdersTest
WHERE OrderType = @OrderType
AND ServiceSpeed = @ServiceSpeed
AND Qty = @Qty
);
END
GO
Example 3: functions with parameters SQL
SET @OrderID = dbo.GetOrderID (default, default, default);
Example 4: functions with parameters SQL
DECLARE
@OrderID int = NULL,
@OrderType int = 1,
@Qty int = 2,
@ServiceSpeed int = 3;
INSERT INTO OrdersTest (OrderType, Qty, ServiceSpeed)
VALUES (@OrderType, @Qty, @ServiceSpeed);
EXEC @OrderID = dbo.GetOrderID
@OrderType = @OrderType,
@Qty = @Qty,
@ServiceSpeed = @ServiceSpeed;
SELECT @OrderID 'Using EXEC Syntax';