473,624 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generate SQL with a loop...

Hello!

I need to dynamic generate a SQL statement based on how many images a user
select to upload.

Here you see an example with 2 images. It can be up to 50 images and I dont
want to write this lines 50 times since they are almost identical (Example,
first line has "varImage_1 " , the second has "varImage_2 "... and It shall go
up to varImage_50...) .

How can I "generate" these lines so I only need to write it once, by using a
loop?
<%
if varImage_1 <> "" then SQL = SQL & "insert into tblImage
(path,sort,artI D,custID,text) values
('"&varImage_1& "','1','"&dbArt ID&"','"&varCus tID&"','"&varTe xtImage1&"'); "

if varImage_2 <> "" then SQL = SQL & "insert into tblImage
(path,sort,artI D,custID,text) values
('"&varImage_2& "','2','"&dbArt ID&"','"&varCus tID&"','"&varTe xtImage2&"'); "
%>
Jul 22 '05 #1
1 1468
how are the varImage_n variables being set? If you can get them into an
array you can

For i = 1 to 50

if varImage(i) <> "" then
SQL = "insert into tblImage (path,sort,artI D,custID,text) values " &
_
"('"&varImage(i ) &
"','1','"&dbArt ID&"','"&varCus tID&"','"&varTe xtImage(i)&"')"

dbConn.Execute strSQL,, adCmdText + adExecuteNoReco rds
end if

Next

If the values are coming from form fields you can

For i = 1 to 50

varImage = Request.Form("v arImage" & CStr(i) )
if varImage <> "" then
SQL = "insert into tblImage (path,sort,artI D,custID,text) values " &
_
"('"&varIma ge &
"','1','"&dbArt ID&"','"&varCus tID&"','"&reque st.form("varTex tImage" &
Cstr(i))&"')"

dbConn.Execute strSQL,, adCmdText + adExecuteNoReco rds
end if

Next

I believe that you could also use the Eval function if you cannot change the
variables to an array. But you'll have to check the docs for that. I had my
fill off executable data a long time ago.

Also, be sure to replace any single quotes in the input data with two single
quotes in the SQL statement (or use parameterized command objects). See
recent posts about the subject ( by Bob Barrows I think).

--
--Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com

"Øyvind Isaksen" <oy****@sorenso .no> wrote in message
news:ee******** ******@TK2MSFTN GP09.phx.gbl...
Hello!

I need to dynamic generate a SQL statement based on how many images a user
select to upload.

Here you see an example with 2 images. It can be up to 50 images and I
dont want to write this lines 50 times since they are almost identical
(Example, first line has "varImage_1 " , the second has "varImage_2 "... and
It shall go up to varImage_50...) .

How can I "generate" these lines so I only need to write it once, by using
a loop?
<%
if varImage_1 <> "" then SQL = SQL & "insert into tblImage
(path,sort,artI D,custID,text) values
('"&varImage_1& "','1','"&dbArt ID&"','"&varCus tID&"','"&varTe xtImage1&"');
"

if varImage_2 <> "" then SQL = SQL & "insert into tblImage
(path,sort,artI D,custID,text) values
('"&varImage_2& "','2','"&dbArt ID&"','"&varCus tID&"','"&varTe xtImage2&"');
"
%>

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
15683
by: Tom Urbanowicz | last post by:
I have a table with 100+ columns, for which I'm trying to retrieve only 1 specific record. For this single record, I do not know which of the columns are NULL, and which are populated. I would like to create a dynamically-generated SELECT--limiting the columns to only those that are populated. If, for example, only columns COL1, COL8, and COL93 are populated for this one record in the MYTEST table, the generated SELECT statement would...
2
1717
by: grant | last post by:
Hi guys: I am try to build an online query application and would like to generate xml file like following: <survey name="Example Survey"> <question type="text" name="Title" required="yes"/> <question type="text" name="Industry"/> <question type="radio" name="Education"> <choice value="High school"></choice>
12
12290
by: jaapkramer | last post by:
I have a page with 641 images in it. Each image can be clicked with as result that the image source will be changed. For this the images need to have a unique ID. But to do this manually for 641 is just too much. Is there a way to give each image his own unique id without setting them myself? The image: <img src="images/hokje.gif" id="" onClick="javascript:veranderKeuze(this.id);" alt="">
4
8194
by: darin dimitrov | last post by:
Hello, I need help with an algoritm that given a set of "n" distinct numbers will generate all the possible permutations of fixed length "m" of these numbers WITH repetitions (a total of n^m possibilities). For example: given the set {1, 2} would output: 111,112,121,122,211,212,221,222 if we fix m=3.
5
1843
by: Chameleon | last post by:
I totally messed up with this: We have -------------------------------------- generate(v.begin(), v.end(), my_func); -------------------------------------- and my_func: ----------- int my_func();
6
6142
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an array of non-owning pointers to these variables. I am trying to write a macro to generate these variable names for me, but I am not sure if what I want to do is possible. The code below demonstrates what I want to do (except it is generating...
1
1680
by: lennyw | last post by:
Hi I'm trying to use XSLT to do an xml to xml transformation where the output xml contains summary data on the information in the input xml. I've succesfully done a Muenchian grouping of the data from the input xml file, but I can't see how to operate on the grouped result to create and output the desired summary data. If someone could give a reference to an example or provide an actual example I'd really apprecaite it.
15
2982
by: Orchid | last post by:
Hello, I am looking to generate a unique ID field on MS. Access. The ID is with 10 digits with the combination of 5 Letters from the 26 letters and 5 Numbers from 1 to 9. The letters and numbers are picked randomly. When I click a button with the VB code on a form, it will create the ID on a table call "Customer table". Can someone help me on how to write the VB code to generate the ID?? Thanks in advance for your help!!
3
2402
by: Bill Woessner | last post by:
I'm trying to replace a loop with a call to std::generate and I'm about at wits end. Here's the working code: std::vector<double>::iterator it; std::vector<doubledata(100); RandomGenerator<doubleg; for(it = data.begin(); it != data.end(); ++it) { *it = g.GetGaussian();
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.