sql function example with parameters

Example 1: 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 2: functions with parameters SQL

SET @OrderID = dbo.GetOrderID (default, default, default);

Example 3: functions with parameters SQL

EXEC @OrderID = dbo.GetOrderID
	@OrderType = @OrderType,
	@ServiceSpeed = @Qty,
	@Qty = @ServiceSpeed;

Tags:

Sql Example