Published on

How to use variables in MSSQL

To use variables in MSSQL queries, simply declare and set them before using:

--
-- Declare and set variables
--
DECLARE @migrationDate VARCHAR(100), @groupName VARCHAR(100)
SET @migrationDate = '2014-08-15'
SET @groupName = 'SOME-GROUP-NAME'

--
-- Execute query using variables
--
SELECT DISTINCT [username]
    ,[groupname]
    ,[migrationdate]
FROM [MYDB].[dbo].[vw_my_special_view]
WHERE [groupname] LIKE @groupName
AND [migrationdate] LIKE @migrationDate