4
Answers

how to save sql string result in a variable ?

Subin Thomas

Subin Thomas

6y
877
1
 i have a text box from where the id of the user is taken 
i wrote a sql query which return the id passed in text box is a teacher or student 
i want to store the resulted string in a variable
 
eg. if  pass 101 in text box the query gives me a record saying role teacher
this teacher value i want to store in a variable 
 
Answers (4)
1
Jignesh Kumar

Jignesh Kumar

30 39.5k 2.9m 6y
Hi Subin,
 
You can use out parameter in SQL procedure
 
  1. CREATE PROCEDURE dbo.uspGetRole @Id int, @UserRole int OUTPUT  
  2. AS  
  3. SELECT @UserRole = columnsName   
  4. FROM TableName   
  5. WHERE Id = @Id  
 
1
Subin Thomas

Subin Thomas

NA 4.9k 132.7k 6y
hey thanks piyush for the response i got the code eventually
1
Piyush Pansuriya

Piyush Pansuriya

196 10.1k 762.2k 6y
Hi Subin, Please share the code. So, we can help you..
Thanks 
0
Shraddha Patel

Shraddha Patel

NA 167 26.9k 6y
you can try this code......
  1. DECLARE @sqlCommand nvarchar(1000)  
  2. DECLARE @city varchar(75)  
  3. declare @counts int  
  4. SET @city = 'New York'  
  5. SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city'  
  6. EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @cnt=@counts OUTPUT  
  7. select @counts as Counts