English 中文(简体)
如果变量在插板中无效,则核对
原标题:Check if a variable is null in plsql

我想检查一个变量是否无效。 如果无效,我就想对这一变量设定一个价值:

//data type of var is  number 
if Var = null then
  var :=5;
endif

但我却在错了。 我如何检查变数是否无效?

我正在使用矿石数据类型

最佳回答
if var is NULL then
  var :=5;
end if;
问题回答

使用:

IF Var IS NULL THEN
  var := 5;
END IF;

页: 1

var := COALESCE(Var, 5)

其他替代物:

var := NVL(var, 5)

参考:

在PL/SQL中,由于与<代码>NUL的所有比较,你可以提用操作员,如 = 或lt;> 测试。 比较<代码>NUL上的部分,您需要使用特殊操作者IS NUL,这些特别操作者正是为此目的使用的。 因此,而不是书面

IF var = NULL THEN...

页: 1

IF VAR IS NULL THEN...

如果是的话,请你选择使用<代码>NVL 有两个论点,第一个是变数,第二个是价值(不变或计算)。 审视其第一个论点,如果认为第一种论点是NUL,则将第二个论点退回。 如果第1条关于的论点不是NUL,第1条理由即告回。 因此,你可以改写

IF var IS NULL THEN
  var := 5;
END IF;

页: 1

var := NVL(var, 5);

我希望这一帮助。

EDIT

既然我写了这一答案已有将近10年,那么,仅扩大这个答案就值得庆贺。

<编码>COALESCE功能是Oracle s 代码。 该编码与在海事组织两种良好方式中有所不同:

  1. It takes any number of arguments, and returns the first one which is not NULL. If all the arguments p页: 1sed to COALESCE are NULL, it returns NULL.

  2. In contr页: 1t to NVL, COALESCE only evaluates arguments if it must, while NVL evaluates both of its arguments and then determines if the first one is NULL, etc. So COALESCE can be more efficient, because it doesn t spend time evaluating things which won t be used (and which can potentially cause unwanted side effects), but it also means that COALESCE is not a 100% straightforward drop-in replacement for NVL.

如果使用无效

永远不会比任何东西都大、小、平等或不平等。 避免这种情况的最佳办法是使用Nvl。

例如

declare
  i integer;
begin
  if i <> 1 then
    i:=1;
    foobar();
  end if;
end;
/

该条款从未在其中。

这将发挥作用。

if 1<>nvl(i,1) then
if i<> 1 or i is null then

另一种方式:

var := coalesce (var, 5);

COALESCE是Oracle s NVL(多或少)功能的ANSI当量。





相关问题
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 ...

热门标签