How to Declare Variables in MS-SQL Server Management Studio

Most of the projects I work on have some kind of database interaction and some of them require variables to be the passed to the queries at runtime.

I typically test my queries against the database using Microsoft SQL Server Management Studio and being able to use variable on my queries there is a big help.

Here is a quick hint to use variables with MS-SQL Server Management Studio:


-- Database to use
USE OSBC;

-- Declare the variable you need
DECLARE @AcctNumber varchar;

-- Set the value of the variable
BEGIN
SET @AcctNumber = 1
END

-- Run the query testing the paramenters
select * from contacts
WHERE AccountNumber = @AcctNumber


Here is a screenshot of my test:



Pretty simple stuff yet useful...

Popular posts from this blog

Calling Web Services with Apache Camel

Using HTTP-based endpoints with Apache Camel