SQL Dinamic with inserts

Hi Experts, I need do an insert, and I am using a code, with :

DECLARE
CURSOR C1 IS
    select DISTINCT estacion
    FROM xpuntos
    order by 1;

r1 c1%ROWTYPE;
str_a VARCHAR2(1000);
str_insert VARCHAR2(2000);
r2 VARCHAR2(100);

BEGIN

 
        str_a := 'Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = ';

        str_insert := str_a ||r1.estacion||', ' ||r1.estacion||' );';        

        FOR r1 IN c1
        
            LOOP
            EXIT WHEN c1%NOTFOUND;
                          
            --DBMS_OUTPUT.put_line (str_a||''||r1.estacion||' , '||r1.estacion||');');
            DBMS_OUTPUT.put_line (str_insert);
            
            END LOOP;

END;

Open in new window


The final sentence for inserts must be:
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 1, 1 );
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 2, 2 );
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 3, 3 );

But I see:
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = ,  );
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = ,  );
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = ,  );
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = ,  );

Open in new window


I think the problem is in:
str_insert := str_a ||r1.estacion||', ' ||r1.estacion||' );';     

Open in new window

 
Because I can see the correct string with DBMS_OUTPUT.
Could you help me to see the error?
Thanks in advanced!
Regards
insert-xpuntos.sql
create-xpuntos.sql
carlino70Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
this:  str_insert := str_a ||r1.estacion||', ' ||r1.estacion||' );';  

Needs to be inside the loop where you are fetching the values.
0
slightwv (䄆 Netminder) Commented:
Your insert will also fail:
Insert into RANGOS
                       (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 1, 1 );


You are providing two values for the insert: DEVICEQUERYSTRING, ESTACION
And only selecting one.

You also have a VALUES statement.  You either use VALUES or a sub query, not both.




Any reason you are doing it in a loop?




Why not everything in a single insert and 'correct' select?
Insert into RANGOS
(DEVICEQUERYSTRING, ESTACION)
 ( select DISTINCT estacion, some_other_value
    FROM xpuntos
 );
0
carlino70Author Commented:
Why not everything in a single insert and 'correct' select?

But in this case, how get the string correct in each row?, Knowing I must concatenate the constant value of the field DEVICEQUERYSTRING:
'SELECT PointNumber from xpuntos where estacion = '

Open in new window

concatenated with r1.estacion, concatenated with r1.estacion:
For station 1:
SELECT PointNumber from xpuntos where estacion = 1, 1

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

slightwv (䄆 Netminder) Commented:
Please post the table definition for RANGOS and the expected results based on the data oyu provided for XPUNTOS.

>>But in this case, how get the string correct in each row?,

If you have a select, go into a loop to generate insert statements, you should be able to combine the two and do the insert with a correct sub-select and make it all one statement.
0
carlino70Author Commented:
Table RANGOS:
CREATE TABLE RANGOS
(
  DEVICEQUERYSTRING  VARCHAR2(2000 CHAR),
  ESTACION           INTEGER
)

Open in new window


Expected for each 'estacion' of xpuntos, example with 'estacion' = 1:
Insert into RANGOS (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 1, 1 );

Open in new window


with 'estacion' 2:
Insert into RANGOS (DEVICEQUERYSTRING, ESTACION)
                       Values
                       ( SELECT PointNumber from xpuntos where estacion = 2, 2 );

Open in new window


note: 'SELECT PointNumber from xpuntos where estacion = ' is a constant, don´t sub-query
0
slightwv (䄆 Netminder) Commented:
>>Expected for each 'estacion' of xpuntos, example with 'estacion' = 1:

I want the actual desired values in the table, RANGOS, after you execute those inserts.

As I pointed out, those insert statements are incorrect.  They are not even valid syntax.
0
carlino70Author Commented:
OK, in the RANGOS table, I expect:

example with 'estacion' = 1
DEVICEQUERYSTRING, ESTACION
SELECT PointNumber from xpuntos where estacion = 1, 1

Open in new window


example with 'estacion' = 2

DEVICEQUERYSTRING, ESTACION
SELECT PointNumber from xpuntos where estacion = 1, 1

Open in new window

0
slightwv (䄆 Netminder) Commented:
>>OK, in the RANGOS table, I expect:

Still does not provide what I asked for.

Where does devicequerystring come from?

You provided insert statements for XPUNTOS.

Based on that data, what I'm after is the raw expected results that will be in the RANGOS table.

for example:
DEVICEQUERYSTRING,  ESTACION          
'a',1
'b',2
etc...

The actual raw data you expect to see if you do:
select * from rangos;
0
carlino70Author Commented:
Where does devicequerystring come from?

devicequerystring should be a constant string::
'SELECT PointNumber from xpuntos where estacion = '

Open in new window

concatenating with:
r1.estacion

Open in new window

and column estacion should be:
r1.estacion

Open in new window

The actual raw data you expect to see if you do:
select * from rangos;

DEVICEQUERYSTRING, ESTACION
'SELECT PointNumber from xpuntos_analog where estacion = 1', 1
'SELECT PointNumber from xpuntos_analog where estacion = 2', 2
'SELECT PointNumber from xpuntos_analog where estacion = 3', 3

Open in new window

It is possible to do?
0
slightwv (䄆 Netminder) Commented:
OK, I think I have it.  Sorry I wasn't understanding.

Will this not work:

insert into RAGNOS
(select DISTINCT 'SELECT PointNumber from xpuntos_analog where estacion = ' || estacion, estacion FROM xpuntos);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carlino70Author Commented:
I apologize for my poor explanation.
The latter solution worked, without using a block PL.
Thanks, as always your solution helps me a lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.