Solved

How to check if a table has at least one record in it ?

Posted on 2015-02-17
6
99 Views
Last Modified: 2015-02-21
I have a table, and I want to know if it has at least one record in it. If it the table does not have a record, then I want sql code execution to exit the stored procedure.
I came up with the following code, however I don't know if it is correct or not? I need help from an expert.

-- Code below is from stored procedure.
if not exists (select top 1 uid
               from user_detail
               where uid IS NOT NULL)
  retur
0
Comment
Question by:brgdotnet
6 Comments
 
LVL 37

Accepted Solution

by:
bbao earned 100 total points
ID: 40615860
it looks fine for me.

have you tried it using MS SQL?
0
 
LVL 13

Assisted Solution

by:Mark Bullock
Mark Bullock earned 200 total points
ID: 40615867
EXISTS test for existence of rows.
The "top 1 uid" is not necessary because the sub-query can return any number of rows and result in a true value. You could use "uid" or "1" instead.
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40615871
When I run the query in Query Analyzer, it just comes back with an empty column. Anyway, I don't think my solution is the best approach. Perhaps someone can offer another solution. Plus how do I exit the stored procedure if the row does not exists? I put a return statement below, but I don't know if that is the correct syntax?

if not exists (select top 1 EmptyColumn
                from Table_3)
   return
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Assisted Solution

by:Mark Bullock
Mark Bullock earned 200 total points
ID: 40615883
Run the subquery, select top 1 EmptyColumn from Table_3. If you get records, the not exists statement will evaluate to false.

The return statement should work.
0
 
LVL 48

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 100 total points
ID: 40616075
Your code it's OK. There is no better way to do it.
if not exists (select top 1 EmptyColumn 
                 from Table_3) 
    return 

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 40616843
I use a static 1 as the column, to avoid errors just in case the table definition changes, and the column you checked went away, although you directly reference a column anyway, so you could use "uid" instead of 1:

if not exists (select top (1) 1
        from dbo.user_detail
        where uid IS NOT NULL)
   return

Btw, you should add the parentheses around the "top" row count value, as that will be required in a future version of SQL.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question