Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Parameter value from select query

I have two parameter

DECLARE @nOdc_Id VARCHAR(50)
DECLARE @nProject_Id VARCHAR(50)

And a Select Query

Select nOdc_Id ,nProject_Id  From ProjectTable Where ProjectId='53'

Now I want to set the parameter's value as

Set @=nOdc_Id =nOdc_Id 
And @nProject_Id =nProject_Id 

Please any one help how can I implement this in StoredProcedure.

like image 699
Gulrej Avatar asked Jun 06 '12 10:06

Gulrej


People also ask

How do you assign a value to a variable in select query?

When a variable is first declared, its value is set to NULL. To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.

How do you assign a value to a parameter in SQL?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do you assign the result of a query to a variable in SQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

How do you DECLARE a variable in a select statement?

SELECT @local_variable is typically used to return a single value into the variable. However, when expression is the name of a column, it can return multiple values. If the SELECT statement returns more than one value, the variable is assigned the last value that is returned.


3 Answers

Select @nOdc_Id = nOdc_Id, 
       @nProject_Id = nProject_Id  
From ProjectTable 
Where ProjectId = 53
like image 150
juergen d Avatar answered Oct 24 '22 02:10

juergen d


You can do like this. There are more approaches.

SET @nOdc_Id = (select nOdc_Id From ProjectTable Where ProjectId='53');
SET @nProject_Id = (select nProject_Id From ProjectTable Where ProjectId='53');
like image 33
Simon Dorociak Avatar answered Oct 24 '22 02:10

Simon Dorociak


like this:

DECLARE @nOdc_Id VARCHAR(50) 
DECLARE @nProject_Id VARCHAR(50) 

Select @nOdc_Id = nOdc_Id From ProjectTable Where ProjectId='53' 
select @nProject_Id  = nProject_Id From ProjectTable Where ProjectId='53' 
like image 35
Rahul Avatar answered Oct 24 '22 01:10

Rahul