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...

Comments

  1. Hi - The tag line under your blog title says, "Thoughts about what I think it's interesting..."

    Should that say "it's" or "is"?

    Just wondering.
    Just trying to help.
    H

    ReplyDelete

  2. Thanks for providing this informative information. it is very useful you may also refer-http://www.s4techno.com/blog/category/database/sql/

    ReplyDelete
  3. I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.
    SQL Server Training in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

Calling Web Services with Apache Camel

Using HTTP-based endpoints with Apache Camel