tags:

views:

38

answers:

1

I have one select:

select * from table1 where col1=10;

and another select:

select * from table1 where col1=11;

Sql parser parses them as different sqls.. I want to make SQL PARSER parse the statement once and just change the parameter in the where clause..Can I do that? Do have any idea ? please share it with me.. thanks a lot.

P.S: I am selecting:

select * from v$sql
where parsing_user_id=55 (my user id)

and see that new row is inserted when i run first query and another row is inserted when i run second query.If i run first or second query one more time now new row is inserted(means that sql parser doesn't parse it once more)

+7  A: 

You need to use bind variables in some form. If you're doing this in SQL*Plus, for example, you could do:

var myval number;

exec :myval := 10;
select * from table1 where col1 = :myval;

exec :myval := 11;
select * from table1 where col1 = :myval;

If you're calling from somewhere else there are slightly different mechanisms so you might need to be a bit more specific.

Alex Poole
Thank you very good answer,but I am using PL/SQL Developer..how can it be there?
kupa
Hmm, don't use that but I thought it was the same; have you tried it?
Alex Poole
yaa I've tried it works :) inserting just one row for this queries..but it should be done from PL/SQL developer
kupa
I meant, have you tried it in PL/SQL Developer? The bind syntax ought to be the same but not sure whether it will recognise `var` (or `variable`). If not it might prompt for the value each time it's run.
Alex Poole
no it will not work because it will be more complicated syntax.You must create function and give it parameter an so on (one more thing you cannot write select in function like that you should assign result of the querie to another variable and return it) You know may be i've got the answer..all i need is to shake my head and write this function based on bind variables.Thank you very much for you answer.
kupa
If you are putting the select into a function, simply use a function parameter for the lookup value. PL/SQL automatically uses bind variables as placeholders for variables used in SQL statements.
Dave Costa