In this post we will discuss about 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 and 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;
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;