Splendonia
Splendonia

Reputation: 1369

Executing a SQL statement inside a stored procedure, how to store the result of that SQL statement into a variable for later use?

i need to execute several SQL statements inside an stored procedure, save the result into a variable for later use.

  SET NOCOUNT OFF
  DECLARE @P TABLE
  @P  = SELECT d.Periodo 
  from [SISACT].DEPRECIACIONES d, [SISACT].CONTROL_PERIODO c 
  where c.vigente = 1 AND d.Periodo = c.periodo

  IF @@ROWCOUNT > 0
     PRINT 'Periodo ya depreciado'

So later i can do something like this

UPDATE [SISACT].ACTIVOS_FIJOS set ultimo_periodo = @p.periodo ...

I know the @P = SELECT is wrong, i saw an example where they used another stored procedure instead of a SELECT statement, i want to know if this is possible without using a stored procedure. If so, how? I'm just going to use the query once in only one stored procedure so i see no point into putting it in another stored procedure. I'm new to T-SQL and SQL server , i'm learning on my own as well and i'm sorry if this is an stupid question.

P.S: The query in this case should return just one record. I'm using SQL SERVER 2008 Express.

Thank you for any help in advance.

Upvotes: 0

Views: 804

Answers (1)

Borik
Borik

Reputation: 438

I think that what you want.

  SET NOCOUNT OFF
  DECLARE @P as CHAR(6)
  SELECT p@ = d.Periodo from [SISACT].DEPRECIACIONES d, [SISACT].CONTROL_PERIODO c where c.vigente = 1 AND d.Periodo = c.periodo
  IF @@ROWCOUNT > 0
     PRINT 'Periodo ya depreciado'

and later you can use

UPDATE [SISACT].ACTIVOS_FIJOS set ultimo_periodo = @p 

Upvotes: 2

Related Questions