Dynamic INSERT INTO command

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.
I'm trying to create a SQL Insert Into string where one of the fields is the
value of a certain variable.

How can i do this?
What i've done is the following:

INSERT INTO P (Btsm, Bts, Data, Hora," & pa_Out & ") VALUES ('" & btsm &
"','" & bts & "','" & dia & "','" & hora & "','" & pa_Out_value & "');"

Pa_Out is the name of the field.
 
There are people here who do this imbedded SQL stuff way better than I do,
but let's take a shot at it: are you trying to make the FIELD you're
inserting into dynamic, or the DATA you're inserting into it dynamic?

If the field you're inserting into is always the same then this should work;
the incoming data (pa_Out_value) would vary dynamically, but it would always
go into the pa_Out field:

"INSERT INTO P (Btsm, Bts, Data, Hora, pa_Out) VALUES ('" & btsm &
"','" & bts & "','" & dia & "','" & hora & "','" & pa_Out_value & "');"

(assuming all your values are text; I see you have them all set off with
single quotes)

((((By the way, is the field really Data, since what you're trying to put
into it is dia? Maybe a typo there?))))

On the other hand, if you're trying to put the pa_out_Value bit of
information into a different field depending on ???whatever???, the
following would seem to be what you need. pa_Out would be the dynamic field
name you want to put pa_Out_value into; the incoming data would always be the
same, but the field it goes into would vary depending on pa_Out.

"INSERT INTO P (Btsm, Bts, Data, Hora," & pa_Out & ") VALUES ('" & btsm &
 
On the surface, that SQL statement looks correct.

What happens when you try to run it? What should happen instead? How are you
running it?
 
I'm assuming that you have an opening quote at the very beginning and it is
just missing in your example. Everything else looks like it should work,
although you may need to check for situations where a field might have a null
or missing value, especially the pa_out variable. Also, are all of the fields
text fields? If so you're OK. You don't want the single quotes around any
numeric values.
 
Hello.
Thanks for your quick answers. After all the SQL command was OK. The problem
is that there was a field name with the character "(" and that was causing
the error. When i removed that character the command worked perfectly.

Thank you all.

Luis
 
Back
Top