|
Here's the trick that most people use. You do a range on the parameters you may, or may not care about. If you just want a single value you put in the value for the low and the high. If you want all you put in *LOVAL and *HIVAL. This is a working subprocedure: P*----------------------------------- P* Prototype for procedure: CHECKDB P* Checks for existance. P* If you pass it 1 parameter it checks for the existance of the library. P* If you pass it 2 parameters it checks for the existance of the file. P* If you pass it 3 parameters it checks for the existance of the field. P* Returns: Logical P* Parameter: library => Library name P* Parameter: File => File name P* Parameter: field => Field name P*-------------------------------------- P CHECKDB B EXPORT D CHECKDB PI like(vCheckDB) D library 10A CONST D File 10A CONST OPTIONS(*NOPASS) D field 10A CONST OPTIONS(*NOPASS) D* Local fields D vCheckDB S n D wCheckDB s 1a D sqlstring s 200a D loFile s like(File) D hiFile s like(File) D loField s like(Field) D hiField s like(Field) /free // Please understand the following performance considerations. // There are millions of records in the qsys2/syscolumns file. // There are thousands of records in the qsys2/syscolumns file for every // BPCS database. So we borrowed a trick used by Oracle and elsewhere. // We just want to see if a particular record exists, even if there may // be many matches. The 'where exists' clause will stop upon the first // hit. So we created a dummy file, routines/checkexist, to link the // 'where exists' clause to. This file has one row and one column. The // value of that entry is '1'. sqlstring = 'Select found from routines/checkexist ' + 'where exists(' + 'Select dbname from qsys2/syscolumns + where DBNAME = ? + and TBNAME between ? and ? + and NAME between ? and ?)'; if %parms>1; loFile=File; hiFile=File; else; loFile=*loval; hiFile=*hival; endif; if %parms>2; loField=Field; hiField=Field; else; loField=*loval; hiField=*hival; endif; vCheckDB=*off; wCheckDB=*off; /end-free C/EXEC SQL C+ Prepare stmt from :sqlString C/END-EXEC C/EXEC SQL C+ declare C1 cursor for stmt C/END-EXEC C/EXEC SQL C+ Open C1 using :library, :loFile, :hiFile, :loField, :hiField C/END-EXEC C/EXEC SQL C+ Fetch C1 into :wCheckDB C/END-EXEC C/EXEC SQL C+ Close C1 C/END-EXEC /free vCheckDB=wCheckDB; return vCheckDB; /end-free P CHECKDB E Rob Berendt -- "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." Benjamin Franklin "mlazarus@ttec.co m" <mlazarus To: "rpg400-l@midrange.com" <rpg400-l@midrange.com> Sent by: cc: rpg400-l-admin@mi Fax to: drange.com Subject: Re: Dynamic SQL question 03/19/2002 12:04 PM Please respond to rpg400-l Phil, Here's a snippet of code. I'm building SQLString dynamically. I will have from 1 to X parameter markers (determined at runtime) on the WHERE clause. My question refers more to the Open Using statement. In this case I have 4 host variables specified, but since I won't know until runtime how many I need, how would you code the Open Using dynamically? Or is there another approach that I can take? Thanks. -mark C/Exec SQL C+ Prepare SQLStatement From :SQLString C/End-Exec C/Exec SQL C+ Declare C1 Cursor For C+ SQLStatement C/End-Exec C/Exec Sql C+ Open C1 Using :S$SESN, :S$RRCD, :PURC, :S$SRS C/End-Exec C/Exec Sql C+ Fetch C1 Into :HostStr C/End-Exec * DoW Not EOF C DOW SQLStt <> '02000' C WRITE FWSCHWKR C/Exec Sql C+ Fetch C1 Into :HostStr C/End-Exec C ENDDO C/Exec Sql C+ Close C1 C/End-Exec ---------------- below is a subprocedure which builds a host variable called SQLStmt (a 1024 varying character field), prepares the statement using that host variable, declares the cursor using the prepared statement, then opens the cursor. Is this what you're looking for? Phil P SQL_PrpStmt b /FREE SQLStmt = 'select * from prmorg + where companyNumber = ' + %editc(cpy:'X') + ' order by companyNumber, organizationName'; /END-FREE C/exec sql C+ PREPARE @SELECT FROM :SQLStmt C/end-exec C C/exec sql C+ DECLARE @CURSOR DYNAMIC SCROLL CURSOR FOR @SELECT C/end-exec C C/exec sql C+ open @cursor C/end-exec P SQL_PrpStmt e > > >>> mlazarus@ttec.com 03/18/02 06:21PM >>> > I would like to run a dynamic SQL statement with > an undetermined (at > compile time) number of placeholder variables. > > Building the PREPARE string w/ the placemarkers is > not a problem. > My > question is how do I specify the "OPEN cursor INTO" > dynamically? -------------------------------------------------------------------- mail2web - Check your email from the web at http://mail2web.com/ . _______________________________________________ This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list To post a message email: RPG400-L@midrange.com To subscribe, unsubscribe, or change list options, visit: http://lists.midrange.com/cgi-bin/listinfo/rpg400-l or email: RPG400-L-request@midrange.com Before posting, please take a moment to review the archives at http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.