How to assign column value to variable sql server 2008?

In this post, we will discuss how to assign column value to a variable in SQL Server 2008.

Also, you can check my previous post:

Caching in Asp.Net,

Post form data to 3rd party URL in Asp.Net

Working with enterprise library for data access in asp.net Part-3.

To create a variable in SQL Server 2008, we have to use keyword declare like below:

Declare @UserID INT

Declare @UserName VARCHAR(100)

Similarly, you can declare multiple variables at a time like below:

Declare @UserName VARCHAR(100), @FirstName VARCHAR(50)

To assign a column value to a variable we can use like below:

Select @UserID=UserID from UserMaster where FirstName=’AspDotNetHelp.com’

Also, you can assign multiple values like below:

Select @UserID=UserID, @UserName=UserName from UserMaster where FirstName=’AspDotNetHelp.com’

Below is a code that will first declare a variable, then it will assign some value and then it select the value. (MSDN)

DECLARE @Name VARCHAR(50);


SET @Name = ‘My Name is XYZ’;

SELECT @Name;


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply