Solved

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

Posted on 2015-02-17
6
95 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 47

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

786 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