Alex
Alex

Reputation: 103

How to store the result of a select query into a variable(IBM DB2)?

I am trying to save the result of a query into a variable. I am using IBM DB2, but I can only store the result if I am declaring the variable inside a procedure.

My code is:

DECLARE @myvar INTEGER; SET @myvar = (SELECT MAX(ID) FROM S0SCSQMS.S0SRPTCNAME);

and I receive the following errors: For the first line:"SQL0104N An unexpected token "INTEGER" was found following "DECLARE @myvar ". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601"

The error code does not tell me much. I looked for it on the IBM documentation.

Looking forward for an answer.

Thank you.

Upvotes: 5

Views: 17248

Answers (1)

Esperento57
Esperento57

Reputation: 17492

try this (work on iseries db2 v7r1)

CREATE OR REPLACE VARIABLE myvar INTEGER ;

SET myvar = (SELECT max( id_xp_dossier) FROM cilgprod.xp_dossier);

DROP VARIABLE myvar;

Upvotes: 9

Related Questions