Lavandil
Lavandil

Reputation: 153

declare bind variables in script

I have script that uses this variables

with
TIME_DATA  as ( select $$D:=:DA$$ td from dual),

GROUP_INFO as (select $$N:=:GR_ID$$ gr_id_number from dual),

and uses them like this

 A_PLUS_TEK as(select point_id, ml_id, ml_name, val a_plus_month, DA
from VALUE_DATA, TIME_DATA
where ml_id = 381 and DA = trunc(td, 'MM'))

I want to know how to initialize this variables, now when I run script I have this ouput:

Bind Variable "DA$$" is NOT DECLARED

EDIT: Usually users enter this values on website, I have access only to database, where I got script from reports table. Also I want to know how to initialize this variables from c#.

EIDT2: I took this for example (How do I use variables in Oracle SQL Developer?). It works.

variable v_count number;
variable v_emp_id number;
exec :v_emp_id := 1234;
exec select count(1) into :v_count from emp;
select *
  from emp
 where empno = :v_emp_id
exec print :v_count;

my code:

    declare
    variable DA$$ VARCHAR2(80);
    variable gr_id$$ number;
    begin 
    exec :DA$$ := to_date('2011-09-13 09:00:00', 'YYYY-MM-DD HH24:MI:SS');
    exec :gr_id$$ := '1341';
BEGIN

and then with statement :

   WITH
    TIME_DATA  as ( select $$D:=:DA$$ td from dual),

    GROUP_INFO as (select $$N:=:GR_ID$$ gr_id_number from dual),

 ....
END;

But still have this

 Bind Variable "DA$$" is NOT DECLARED
anonymous block completed

This also change nothing

exec :DA$$ := '2011-09-13 09:00:00';

I think that DA$$ is date, because it used in trunc() DA = trunc(td, 'MM')) When I select Run Statement(trl+Enter), Sql Developer offers to enter bind variables, but I don't know how to enter date.

EDIT3: Finally I do this without Declare, Begin, End and it works :

    variable DA$$ VARCHAR2(80);
    variable gr_id$$ number; 
    exec :DA$$ := to_date('2011-09-13 09:00:00', 'YYYY-MM-DD HH24:MI:SS');
    exec  DBMS_OUTPUT.PUT_LINE(:DA$$);    
    exec :gr_id$$ := '1341';
    with
    TIME_DATA  as ( select $$D:=DA$$; td from dual),
    GROUP_INFO as (select $$N:=:GR_ID$$ gr_id_number from dual),
...

Upvotes: 0

Views: 2378

Answers (1)

playful
playful

Reputation: 1774

From C# you can do this:

// using Oracle.DataAccess.Client
using(OracleCommand command = new OracleCommand(commandText, dbConnection))
{
    command.CommandType = CommandType.Text;
    command.Parameters.Add("DA$$", OracleDbType.Int32, valueDA, ParameterDirection.Input);
    command.Parameters.Add("GR_ID$$", OracleDbType.Int32, valueGR_ID, ParameterDirection.Input);
}

But review your comand text. Because you will probably face syntax errors due to "$$D:" and "$$N:"

Upvotes: 1

Related Questions