Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > long SQL strings..

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-04, 09:36
Td04 Td04 is offline
Registered User
 
Join Date: Mar 2004
Posts: 52
long SQL strings..

I wonder how to make this code shorter..(smarter)
and (or) how to do a linebreak in the SQL string, it gets to long in the end.
Thanks//Martin
fnr_flera = request.queryString("Fnr_flera")
myArray=split(fnr_flera,",")
'As the selections are different every time (1-75 records)
if UBound(myArray)+1 = 1 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'order by NAMN"
elseif UBound(myArray)+1 = 2 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"'order by NAMN"
elseif UBound(myArray)+1 = 3 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'order by NAMN"
elseif UBound(myArray)+1 = 4 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'OR Fnr='"&myArray(3)&"'order by NAMN"
elseif UBound(myArray)+1 = 5 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'OR Fnr='"&myArray(3)&"'OR Fnr='"&myArray(4)&"'order by NAMN"
elseif UBound(myArray)+1 = 6 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'OR Fnr='"&myArray(3)&"'OR Fnr='"&myArray(4)&"'OR Fnr='"&myArray(5)&"'order by NAMN"
elseif UBound(myArray)+1 = 7 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'OR Fnr='"&myArray(3)&"'OR Fnr='"&myArray(4)&"'OR Fnr='"&myArray(5)&"'OR Fnr='"&myArray(6)&"'order by NAMN"
elseif UBound(myArray)+1 = 8 then
SQL = "SELECT * from owner_alla WHERE Fnr = '"&myArray(0)&"'OR Fnr='"&myArray(1)&"' OR Fnr='"&myArray(2)&"'OR Fnr='"&myArray(3)&"'OR Fnr='"&myArray(4)&"'OR Fnr='"&myArray(5)&"'OR Fnr='"&myArray(6)&"' OR Fnr='"&myArray(7)&"'order by NAMN"
elseif UBound(myArray)+1 = 9 then ....
it goes on until 75... :O
__________________
"Never underestimate a large number of morons"

Last edited by Td04 : 10-01-04 at 09:38.
Reply With Quote
  #2 (permalink)  
Old 10-01-04, 10:18
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

1. Using IN will shorten your SQL a little :

Code:
SQL = "SELECT * from owner_alla WHERE Fnr IN ('"&myArray(0)&"','"&myArray(1)&"','"&myArray(2)&"','"&myArray(3)&"','"&myArray(4)&"','"&myArray(5)&"','"&myArray(6)&"' ,'"&myArray(7)&"') order by NAMN"


2. You can try something like this (not sure about the correct syntax, but here is the idea) :

Code:
SQL = "SELECT * from owner_alla WHERE Fnr IN(" FOR i=0 TO UBound(myArray)-1 IF (i < UBound(myArray)-1) THEN SQL = SQL & "'"&myArray(i)&"'," ELSE SQL = SQL & "'"&myArray(i)&"'" END IF NEXT i SQL = SQL & ") order by NAMN"


Hope that helps,

Regards,

RBARAER
Reply With Quote
  #3 (permalink)  
Old 10-01-04, 12:06
Td04 Td04 is offline
Registered User
 
Join Date: Mar 2004
Posts: 52
That loop was brilliant! It works excellent after just removeing the
i after next..
Thanks a million RBARAER, that saved alot of headache
//Martin :-)
__________________
"Never underestimate a large number of morons"
Reply With Quote
  #4 (permalink)  
Old 10-04-04, 07:23
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Glad it helped you !

Regards,

RBARAER
Reply With Quote
Reply


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

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