If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Concat variable in query string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-16-10, 18:15
raqueliii raqueliii is offline
Registered User
 
Join Date: Feb 2010
Posts: 7
Red face Concat variable in query string

Hi!
I have a SP and at the query i want to concatenate a input variable like:

FECHA = "AND IN('1','2','3','4')

CREATE PROCEDURE SP_BUSCAR ( IN FINICIO DATE, IN FFIN DATE, IN CADENA_FECHAS CHAR(100))

DYNAMIC RESULT SETS 1

LANGUAGE SQL

P1 : BEGIN

DECLARE CURSOR1 CURSOR WITH RETURN FOR

SELECT F . ID_047 , ID_049 , ASUNTO , HINICIO , HFIN , FECHA , ( DAYOFWEEK ( FECHA ) - 1 ) AS DSEMANA FROM SCHEMA . T_047 O, SCHEMA . T_049 F WHERE O . ID_047 = F . ID_047 AND (HINICIO BETWEEN FINICIO AND FFIN) OR (HFIN BETWEEN FINICIO AND FFIN) || FECHAS;

OPEN CURSOR1 ;

END P1 ;


BUT I HAVE AN ERROR:

QL State: 42601
Vendor Code: -104
Message: [SQL0104] Token || was not valid.

I READ TO USE CONCAT FUNCTION, BUT I DONT KNOW HOW TO USE, THX!

Last edited by raqueliii; 02-19-10 at 11:43.
Reply With Quote
  #2 (permalink)  
Old 02-17-10, 08:05
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Take a look at dynamically built statement and prepare statement.

Dave
Reply With Quote
  #3 (permalink)  
Old 02-17-10, 08:47
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
PREPARE, EXECUTE & EXECUTE IMMEDIATE are the keywords you have to search on

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 02-18-10, 01:15
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
Quote:
WHERE O . ID_047 = F . ID_047 AND (HINICIO BETWEEN FINICIO AND FFIN) OR (HFIN BETWEEN FINICIO AND FFIN) || FECHAS;
Grammar mistake in the where clause?
Reply With Quote
  #5 (permalink)  
Old 02-18-10, 09:26
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
verify that both sides of || are CHAR. if they are not, use CHAR(...)||CHAR(...)
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #6 (permalink)  
Old 02-18-10, 11:00
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,194
"HFIN BETWEEN FINICIO AND FFIN" is a predicate and "(HFIN BETWEEN FINICIO AND FFIN)" is a search condition.
Those cannot be converted to any data type.

Follow the recommendations of Dave and Sathyaram...
(They are practically same. )
Quote:
Take a look at dynamically built statement and prepare statement.
Quote:
PREPARE, EXECUTE & EXECUTE IMMEDIATE are the keywords you have to search on
Reply With Quote
  #7 (permalink)  
Old 02-18-10, 21:12
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Beyond the question you asked, you query (as written) has a serious flaw in it. (I have replace your concatenation attempt the the code you want there (which is also missing the column you want to compare to the IN list):
Code:
SELECT F . ID_047 , ID_049 , ASUNTO , HINICIO , HFIN , FECHA , ( DAYOFWEEK ( FECHA ) - 1 ) AS DSEMANA 
FROM TUMENMA . T_047 O
   , TUMENMA . T_049 F 
WHERE O . ID_047 = F . ID_047 
  AND (HINICIO BETWEEN FINICIO AND FFIN) 
   OR (HFIN    BETWEEN FINICIO AND FFIN)
  AND ??? IN('1','2','3','4');
As written the parenthesis do nothing as they enclose a single predicate. But the problem is you have your Join Predicate (O.ID_047 = F.ID_047) is on one side of an OR. Because of this the Join Predicate could be FALSE but you can still return the row(s) if the other side of the OR is True (HFIN BETWEEN FINICIO AND FFIN AND ??? IN ('1','2','3','4') ). This would result in a partial cartisian result. What you probably want is something like:

Code:
SELECT F . ID_047 , ID_049 , ASUNTO , HINICIO , HFIN , FECHA , ( DAYOFWEEK ( FECHA ) - 1 ) AS DSEMANA 
FROM TUMENMA . T_047 O
   , TUMENMA . T_049 F 
WHERE O . ID_047 = F . ID_047 
  AND (HINICIO BETWEEN FINICIO AND FFIN
   OR  HFIN    BETWEEN FINICIO AND FFIN)
  AND ??? IN('1','2','3','4');
Reply With Quote
  #8 (permalink)  
Old 02-18-10, 22:44
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
An example from one of my SP

Code:
:
:
--Statements
declare v_stmt statement;
--cursor
declare ct cursor for v_stmt;

Set v_sql='select 1 from '||p_TableName||' '||p_WhrClause;
 
prepare v_stmt from v_sql;

open ct;

:
:
regards

DBFinder
Reply With Quote
  #9 (permalink)  
Old 02-19-10, 11:38
raqueliii raqueliii is offline
Registered User
 
Join Date: Feb 2010
Posts: 7
Thxs!!! I try both of the solutions and its great! Thxs!
Reply With Quote
  #10 (permalink)  
Old 02-19-10, 17:21
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
"(HFIN BETWEEN FINICIO AND FFIN)" is also a predicate. I think the issue here is the difference between "expressions" and "predicates". An expression, more specifically a string expression, can be concatenated with another (string) expression. A predicate can only be combined using AND and OR keywords with other predicates.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Tags
concatenate, db2, stored procedure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On