assign value to variable in sql code example
Example 1: sql declare variable
DECLARE @MyCounter int;
SET @MyCounter = 0;
Example 2: 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 3: variables in select statement in sql server
USE AdventureWorks2014;
GO
DECLARE @MyVariable int;
SET @MyVariable = 1;
GO
SELECT BusinessEntityID, NationalIDNumber, JobTitle
FROM HumanResources.Employee
WHERE BusinessEntityID = @MyVariable;