Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to declare and assign a variable using select statement in sql server. write query..
Posted

Hi, see this:

SQL
DECLARE @temp VARCHAR(30)
SET @temp = 'Example'

SELECT @temp

OR
SQL
DECLARE @temp VARCHAR(30)
SET @temp = 'Example'

SELECT *
FROM Table_Name
WHERE Table_Column = @temp

OR
SQL
DECLARE @temp VARCHAR(30)

SELECT @temp = name
FROM Table_Name
WHERE Id= 1 


Hope it helps! :D
 
Share this answer
 
v2
Comments
Bala Selvanayagam 23-Aug-13 5:26am    
Another option could be ?

DECLARE @temp VARCHAR(30)

SELECT @temp= name
FROM Table_Name
WHERE Id=<<Some value>>
berrymaria 23-Aug-13 5:29am    
Yes that can be too :) Thanks.
Already updated it.
Bala Selvanayagam 23-Aug-13 5:42am    
5ed
See this

SQL
DECLARE @temp VARCHAR(30)

SELECT @Temp = Column_Name
FROM   TABLE_1
WHERE  ID = 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900