Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / March 2004

Tip: Looking for answers? Try searching our database.

Multi SET & VALUES statements

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim.D - 09 Mar 2004 06:44 GMT
Folks,

Ive been trying to clean up a lot of old code that I have inherited
and make it more efficient. In the process I have been spending hours
with my nose buried in the sql reference manuals and here in the
forums learning all I can from you educated types.

My latest problem with multi row inserts was resolved very nicely
where I now know better, but in the process discovered that the same
principle applies to both the SET and VALUES statements with
particular regard to host variables.

In the Stored Procedures I am working on, there are a significant
number of SET statements such as : SET v_1 = 1; and SET v_2 = 'blah';
or SET v_1 = v_2 +1; and so on. From reading the SQL reference manual
I came across being able to combine the SET statements in much the
same way you would VALUES .. INTO or INSERT using multi row.

I figured this would make the code more efficient due to the large
number of SET statements in nested WHILE loops in the code. The manual
shows examples such as perhaps:

   SET v_1 = 2, v_2 = 3, v_4 = 4;
or  SET (v_1, v_2, v_4) = (2, 3, 4);

   VALUES (function1_name(v_date), function2_name(v_num) into (var_1,
var_2)

The same is true for VALUES and indeed I have seen many examples in
this group, however .. I'll be damned if I can get either to work
correctly for me. The multi SETs just give me plain SQL synntax errors
[expecting spaces etc] while the VALUES statements compile but fail in
the sp when called due to scalar results or multiple rows returned.
Ive tried ensuring the data types are all the same in a single
statement but it doesnt help.

Can anyone shed some light on this for me please ? Id appreciate the
input.

Many thanks,
Tim
Serge Rielau - 09 Mar 2004 14:30 GMT
>     SET v_1 = 2, v_2 = 3, v_4 = 4;
> or  SET (v_1, v_2, v_4) = (2, 3, 4);
This works only in inline SQL PL. One of the few places where inlien SQL
PL has fucntion regular SQL PL does not have. (On my to do-list for Vx)
>     VALUES (function1_name(v_date), function2_name(v_num) into (var_1,
> var_2)
You need braces around the "columns" to make them a tuple
VALUES (function1_name(v_date), function2_name(v_num))
   into var_1, var_2
If you don't add braces DB2 will add them implicitly around each
function giving you 2 types of one column (=> too many rows).
The fact that a scalar can also be a row is one of those SQL Standard
things we'll just have to live with :-(

Cheers
Serge

Signature

Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.