user12813863
user12813863

Reputation:

Extracting data to a variable from a query

I'm writing a database query where I want to get the id number of the folder into the @eFolderID variable

DECLARE @eFolderId varchar(30)
SET @eFolderId = NULL
  
SELECT TOP 1 *
FROM ProcessSupportMap
SET @eFolderId = EFOLDERID 
WHERE  Number = 'B0261109'

I want:

SET @eFolderId = EFOLDERID WHERE Number = 'B0261109'

I don't know where to put it

Upvotes: 0

Views: 50

Answers (2)

Zhorov
Zhorov

Reputation: 29943

You should use SELECT @local_variable statement to set a local variable to the value of an expression:

DECLARE @eFolderId varchar(30)
 
SELECT TOP 1  @eFolderId = EFOLDERID
FROM ProcessSupportMap
WHERE Number = 'B0261109'

Additional notes:

  • As is mentioned in the documentation, if the SELECT statement returns more than one value, the variable is assigned the last value that is returned and if the SELECT statement returns no rows, the variable retains its present value. If expression is a scalar subquery that returns no value, the variable is set to NULL
  • You may also consider using SET @local_variable.

Upvotes: 1

forpas
forpas

Reputation: 164064

You can assign the value returned by the subquery to the variable:

SET @eFolderId = (
  SELECT TOP 1 EFOLDERID 
  FROM ProcessSupportMap 
  WHERE Number = 'B0261109'
)

Upvotes: 0

Related Questions