Solved

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

Posted on 2015-02-17
6
116 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 51

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

624 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