English 中文(简体)
声明和背景 选择声明中的变数
原标题:Declaring & Setting Variables in a Select Statement

我试图写一个简单的问题,我宣布了一些变数,然后用这些变量在Oracle的选定声明中使用。 此前,我已能够在服务器中这样做:

DECLARE @date1   DATETIME
SET @date1 =  03-AUG-2010 

SELECT U.VisualID
FROM Usage u WITH(NOLOCK)
WHERE U.UseTime > @Date1

从查询一开始,你似乎无法在选择性发言中宣布和确定这种变量。 这项权利还是我ms弄什么?

最佳回答

从查询一开始,看看看看看看你不能在选择发言中宣布和确定类似变量。 这项权利还是我失踪了吗?

在Oracle PL/SQL和LL中,有两种单独的发动机。 你们可以在PL/SQL中安置SQL,这将产生你们的变数。 例如以下匿名的PL/SQL组。 注:/归根结底不是PL/SQL的一部分,而是计票L.Q* Plus发送前一个组。

declare 
    v_Date1 date := to_date( 03-AUG-2010 ,  DD-Mon-YYYY );
    v_Count number;
begin
    select count(*) into v_Count
    from Usage
    where UseTime > v_Date1;
    dbms_output.put_line(v_Count);
end;
/

问题在于,与你的T-SQL法典相当的集团不会发挥作用:

SQL> declare 
  2      v_Date1 date := to_date( 03-AUG-2010 ,  DD-Mon-YYYY );
  3  begin
  4      select VisualId
  5      from Usage
  6      where UseTime > v_Date1;
  7  end;
  8  /
    select VisualId
    *
ERROR at line 4:
ORA-06550: line 4, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement

为了通过一个匿名栏目、储存程序或储存功能的PL/SQL的查询结果,必须宣布、开张,然后回到电话程序。 (除了回答这一问题的范围外。) <EDIT: see 从储存的矿石中提取结果

与数据库连接的客户工具可能具有自己的变数。 * 附加说明:

SQL> -- SQL*Plus does not all date type in this context
SQL> -- So using varchar2 to hold text
SQL> variable v_Date1 varchar2(20)
SQL>
SQL> -- use PL/SQL to set the value of the bind variable
SQL> exec :v_Date1 :=  02-Aug-2010 ;

PL/SQL procedure successfully completed.

SQL> -- Converting to a date, since the variable is not yet a date.
SQL> -- Note the use of colon, this tells SQL*Plus that v_Date1
SQL> -- is a bind variable.
SQL> select VisualId
  2  from Usage
  3  where UseTime > to_char(:v_Date1,  DD-Mon-YYYY );

no rows selected

注 此外,在Toad PL/SQL开发商等工作可能无法(可能赢得t)。 形形形和外壳的线为<>。 附加指挥。 他们不是PL/SQL的指挥。 由于表空,没有选定任何行。

问题回答

我已尝试过这项工作。

define PROPp_START_DT = TO_DATE( 01-SEP-1999 )

select * from proposal where prop_start_dt = &PROPp_START_DT

 

<代码>SET的指挥是TSQL的特例——此处的指令相当于你规定的:

v_date1 DATE := TO_DATE( 03-AUG-2010 ,  DD-MON-YYYY );

SELECT u.visualid
  FROM USAGE u 
 WHERE u.usetime > v_date1;

没有必要用“@”预先确定变数;我倾向于先确定“v_”的变数,以区分变数和计划;一栏/etc。

。 参看NLOCK等同文。

Try the to_date function.

从服务器也看,这实际上使我gged。 对于使用Toad Data PointToad for Blake/em>的人来说,这极为简单。 在你们的国名面前安放殖民地,将迅速 准备开方言,让你在座标上。

SELECT * FROM some_table WHERE some_column = :var_name;




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...