Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:34:22+00:00 2026-06-14T17:34:22+00:00

I am trying to execute following PL/SQL script in SQL Developer. The loop should

  • 0

I am trying to execute following PL/SQL script in SQL Developer. The loop should return count of nulls but somehow everytime it is returning 0.
set serveroutput on

DECLARE

--v_count number;
v_count_null number;

BEGIN
execute immediate 'select count(*) from SP_MOSAIX' into v_count;

FOR  i in (select column_name from all_tab_COLUMNS where table_name = 'SP_MOSAIX')
LOOP
select count(*) into v_count_null from SP_MOSAIX  where i.column_name IS NULL ;
dbms_output.put_line(v_count_null);

END LOOP;

END;

So when I run this, following output is what i get:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

But if I manually execute the query subsituting column_name I get the result.

select count(*) into v_count_null from SP_MOSAIX  where i.column_name IS NULL; 

Can anybody help on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:34:23+00:00Added an answer on June 14, 2026 at 5:34 pm

    There are a couple of things going on here that you need to be aware of. Firstly, as you allude to, your COUNT query is executing using the value of i.column_name, which is never NULL.

    Secondly, COUNT(*) returns the number of rows that match your WHERE clause condition, regardless of NULL values. If you want to count how many NOT NULL values there are in a particular column, you must COUNT values in that column explicitly.

    Please see the following example (SQL Fiddle):

    Oracle 11g R2 Schema Setup:

    CREATE TABLE null_col_vals (
      col_without_nulls INTEGER NOT NULL
    , col_with_nulls INTEGER
    , col_with_mix INTEGER  
    )
    /
    
    INSERT INTO null_col_vals (col_without_nulls, col_with_nulls, col_with_mix)
    VALUES (1, NULL, NULL)
    /
    
    INSERT INTO null_col_vals (col_without_nulls, col_with_nulls, col_with_mix)
    VALUES (1, NULL, 1)
    /
    
    INSERT INTO null_col_vals (col_without_nulls, col_with_nulls, col_with_mix)
    VALUES (1, NULL, NULL)
    /
    

    Query 1:

    SELECT 
      COUNT(col_without_nulls) col_without_nulls
    , COUNT(col_with_nulls) col_with_nulls
    , COUNT(col_with_mix) col_with_mix
    , COUNT(*) all_rows
    FROM null_col_vals
    

    Results:

    | COL_WITHOUT_NULLS | COL_WITH_NULLS | COL_WITH_MIX | ALL_ROWS |
    ----------------------------------------------------------------
    |                 3 |              0 |            1 |        3 |
    

    As you can see, COUNT(*) always returns the number of rows present, but the others vary in results depending on whether NULL values are present in the specified column or not.

    You’ll need to use EXECUTE IMMEDIATE to convert your column name into part of the query. Something like this might do the job:

    Query 2:

    DECLARE
    
      l_count INTEGER;
    
    BEGIN
    
      FOR r_col IN (
        SELECT *
        FROM all_tab_columns atc
        WHERE atc.table_name = 'NULL_COL_VALS'
      )
      LOOP
    
        dbms_output.put_line(r_col.column_name);
    
        EXECUTE IMMEDIATE 'SELECT COUNT(' || r_col.column_name || ') FROM null_col_vals' 
        INTO l_count;
    
        dbms_output.put_line(l_count);
    
      END LOOP;
    
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute the following SQL query but it is not working:
I'm trying to execute the following SQL: SELECT * FROM Table2 INNER JOIN (SELECT
I'm trying to execute the following line: exit | sqlplus username/password@sid @test.sql Works great
I am trying to execute the following query in oracle but not able to
I'm trying to execute the following code, but I'm getting SyntaxError: invalid syntax at
Trying to take remote backup of sql server database using Perl. But somehow getting
I'm trying to execute the following SQL statement (built and tested in SQL Server
I am trying to execute the following SQL query using MS SQL Server Management
I am trying to run the following script using doctrine but am getting a
I'm trying to execute the following sql instruction in PostgreSQL and when I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.