Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: February 08, 2016 - 9:06 am UTC

Last updated: July 12, 2021 - 5:29 pm UTC

Version: 10.0

Viewed 10K+ times! This question is

You Asked

1)Can you use sysdate in check constraints? If no, why?Let me explain with one simple example?

2)How to count no of records in table without count?

Thanks In Advance.

and Chris said...

1. No, you can't use sysdate in check constraints.

Why?

All rows in a table for an enabled constraint must return true for its expression.

But sysdate is non-deterministic. You can get a different result every time you call it. So the outcome (true/false) can (will) change over time. So Oracle can't guarantee that the expression is always true for every row.

See:

http://docs.oracle.com/database/121/SQLRF/clauses002.htm#SQLRF52205

2. You could run a select with no where clause and see how many rows are returned. e.g.:

declare
  type t is table of hr.employees%rowtype index by pls_integer;
  emps t;
begin
  select * 
  bulk   collect
  into   emps
  from   hr.employees;
   
  dbms_output.put_line('Rows (sql rowcount) ' || sql%rowcount);
  dbms_output.put_line('Rows (collection count) ' || emps.count);
end;
/

Rows (sql rowcount) 107
Rows (collection count) 107


I'm not sure why you'd want to do this though. Why do you want a method other than count?

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

How to count no of records in table without count ?

Prabodha Kumar Sahoo, July 11, 2021 - 12:23 pm UTC

Can we get the number of records using rownum pseudocolumn.

Example :select max(rownum) from emp;
Chris Saxon
July 12, 2021 - 12:06 pm UTC

You can, but why bother? That's what count is for!

SUM

Jonathan Taylor, July 12, 2021 - 2:34 pm UTC

Or SUM(1) instead of COUNT.........but why you'd not use COUNT is beyond me.
Chris Saxon
July 12, 2021 - 5:29 pm UTC

Indeed

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here