How can i use variables in select statement?

hi, i have to use variables in select statement including a constant value, table name, and where clause, like so..
execute immediate 'INSERT INTO table1 USING SELECT '
                                ||lv_source
                                ||', '
                                ||'col1, col2, col3 FROM '
                                ||lv_tablename
                                ||'WHERE TEMPLATE_NAME LIKE '
                                ||CHR (39)
                                ||lv_likevalue
                                ||CHR (39);
Rao_SAsked:
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.

Rao_SAuthor Commented:
with the above statement i get a error:
Error: Error in loading:table1-933ORA-00933: SQL command not properly ended
0
sdstuberCommented:
EXECUTE IMMEDIATE
           'INSERT INTO table1 USING SELECT :source, col1, col2, col3 FROM '
        || lv_tablename
        || ' WHERE TEMPLATE_NAME LIKE :likevalue'
        USING lv_source, lv_likevalue;


the error was because you were missing a space between the tablename and the WHERE clause in the concatenation.

But, you should use bind variable (with the colon) and the USING clause for the variables.

Note, you can't bind the object names, those must be literals within the concatenated sql
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
Rao_SAuthor Commented:
thank you sdstuber, it worked!
i have to add another cursor and pass mutiple 'lv_likevalue', let me test that too...
0
Rao_SAuthor Commented:
thank you sdstuber, works correctly!!
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.