Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested While...Do loops?

Status
Not open for further replies.

mike777

Programmer
Jun 3, 2000
387
US
Does Interbase allow this?
If you REALLY want to help, can you tell me why this procedure isn't working?
Code:
create procedure prc_TestingSubStr(vcStrIn varchar(10),iBeg integer,iNumChar integer) returns (vcStrOut varchar(256)) as
     declare variable vcTempStr1 varchar(10); /*Wildcard place holder*/
     declare variable vcTempStr2 varchar(10); /*Singular read value for testing*/
     declare variable iCounter1 integer;      /*Used as a value to step through all potential characters.*/
     declare variable iCounter2 integer;      /*Counts the number of characters up to first character in return value*/
     declare variable iCounter3 integer;      /*Counts the number of characters in the return value*/
     declare variable iCounter4 integer;      /*Emergency exit counter*/
     begin /*main body*/
          vcStrOut=''; /*return value*/
          vcTempStr1='';
          vcTempStr2='';
          iCounter1=0;
          iCounter2=1;
          iCounter3=0;
          if (iBeg is null) then exit;
          if (iNumChar is null) then exit;
          while (1=1) do
               begin /*infinite loop*/
                    while (iCounter1 < 10) do
                         begin /*assign value to test with*/
                              vcTempStr2=cast(iCounter1 as char(1));
                              if (vcStrIn like vcTempStr1||vcTempStr2||'%') then
                                   begin
                                        iCounter1=10;
                                        if (iCounter2 >= iBeg-1) then
                                             begin
                                                  vcStrOut=vcStrOut||vcTempStr2;
                                                  iCounter3=iCounter3+1;
                                                  if (iCounter3=iNumChar) then
                                                       begin
                                                            suspend;
                                                            exit;
                                                       end
                                             end
                                        else
                                             vcTempStr1=vcTempStr1||'_';
                                   end
                              else
                                   iCounter1=iCounter1+1;
                                   if (iCounter1=10) then
                                        vcTempStr1=vcTempStr1||'_';
                         end /*assign value to test with*/
                    iCounter2=iCounter2+1;
                    iCounter1=0;
               end /*infinite loop*/
     end /*main body*/;
When I try:
Code:
execute procedure prc_TestingSubStr('a2345678',3,1)
, I get an answer of 2.
When I try:
Code:
execute procedure prc_TestingSubStr('a2345678',2,1)
, I get an answer of 2.
It's supposed to return a substring. In the first case, the answer should be 3, not 2. In the second case, the result is correct.
I tried some more:
When I try:
Code:
execute procedure prc_TestingSubStr('a2345678',4,1)
, I get an answer of 4 (correct).
When I try:
Code:
execute procedure prc_TestingSubStr('a2345678',5,1)
, I get an answer of 6 (incorrect).
Whew! I know I'm askin a lot here. Any help you can provide is very much appreciated.
Thank you.
-Mike Kemp
 
OK, I got it.
I must be the only person that answers their own questions. You know what they say, &quot;It's OK to talk to yourself, as long as you don't start answering yourself...&quot;. Well...
I've highlighted the correction in red.
Code:
create procedure prc_TestingSubStr(vcStrIn varchar(10),iBeg integer,iNumChar integer) returns (vcStrOut varchar(256)) as
     declare variable vcTempStr1 varchar(10); /*Wildcard place holder*/
     declare variable vcTempStr2 varchar(10); /*Singular read value for testing*/
     declare variable iCounter1 integer;      /*Used as a value to step through all potential characters.*/
     declare variable iCounter2 integer;      /*Counts the number of characters up to first character in return value*/
     declare variable iCounter3 integer;      /*Counts the number of characters in the return value*/
     declare variable iCounter4 integer;      /*Emergency exit counter*/
     begin /*main body*/
          vcStrOut=''; /*return value*/
          vcTempStr1='';
          vcTempStr2='';
          iCounter1=0;
          iCounter2=1;
          iCounter3=0;
          if (iBeg is null) then exit;
          if (iNumChar is null) then exit;
          while (1=1) do
               begin /*infinite loop*/
                    while (iCounter1 < 10) do
                         begin /*assign value to test with*/
                              vcTempStr2=cast(iCounter1 as char(1));
                              if (vcStrIn like vcTempStr1||vcTempStr2||'%') then
                                   begin
                                        iCounter1=10;
                                        if (iCounter2 >= iBeg-1) then
                                             begin
                                                  vcStrOut=vcStrOut||vcTempStr2;
                                                  iCounter3=iCounter3+1;
                                                  if (iCounter3=iNumChar) then
                                                       begin
                                                            suspend;
                                                            exit;
                                                       end
                                             end
                                        else
                                             vcTempStr1=vcTempStr1||'_';
                                   end
                              else
Code:
begin
Code:
                                        iCounter1=iCounter1+1;
                                        if (iCounter1=10)  then
Code:
                                             vcTempStr1=vcTempStr1||'_';
Code:
                                   end
Code:
                         end /*assign value to test with*/
                    iCounter2=iCounter2+1;
                    iCounter1=0;
               end /*infinite loop*/
     end /*main body*/;
 
what is the use of the &quot;prc_TestingSubStr&quot; since there is allready a &quot;sbstring(s from i for k)&quot; function in sql standard!
 
Thanks, GoreXP.
When I try
Code:
&quot;select sbstring('mike' from 2 for 2) from tbl_CampaignStatsTableNames&quot;
,
I get
Code:
Dynamic SQL Error, SQL error code=-104, Token unknown - line 1, char23, from (#-104)
.
Also, all the documentation I have on Interbase SQL says there is no &quot;SBSTRING&quot; (or substring) function.
Am I missing something?
Thanks again.
-Mike
 
InterBase doesn't have a native &quot;SUBSTRING&quot; function.

However, there are some available, if I'm correct, in free UDF libraries. Try searching for &quot;FreeUDFLib&quot;.


Martijn Tonies
Database Workbench - the developer tool for InterBase, Firebird, MySQL & MS SQL Server
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top