SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Determine if row is empty and not null
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sirmilt
Starting Member

USA
49 Posts

Posted - 04/25/2013 :  11:32:21  Show Profile  Reply with Quote
I need to create a simple stored procedure to set a value of 0 if there are no records in a table. This code:
SELECT (*) FROM Table

In Visual Basic because the first row of a table with no data is filled with null values it throws an error completed. What I need to do is determine if there are records or not.

Milt

Milt

MuMu88
Aged Yak Warrior

547 Posts

Posted - 04/25/2013 :  11:55:23  Show Profile  Reply with Quote
Here is one way to do it:
You can do
SELECT COUNT(*) as cnt FROM Table

If the cnt is > 0 then you can run your
SELECT (*) FROM Table



quote:
Originally posted by sirmilt

I need to create a simple stored procedure to set a value of 0 if there are no records in a table. This code:
SELECT (*) FROM Table

In Visual Basic because the first row of a table with no data is filled with null values it throws an error completed. What I need to do is determine if there are records or not.

Milt

Milt

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/25/2013 :  12:29:41  Show Profile  Reply with Quote

SELECT COUNT(*) AS cnt FROM table
UNION ALL
SELECT 0
WHERE NOT EXISTS (SELECT 1 FROM table)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sirmilt
Starting Member

USA
49 Posts

Posted - 04/25/2013 :  14:58:29  Show Profile  Reply with Quote
Thank you both for the responses.

While I wasn't quite sure exactly what the entry "WHERE NOT EXISTS (SELECT 1 FROM table)" does, the procedure worked perfectly. I'll have to do a little research to figure out why.

Thanks again
Milt

Milt
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 04/25/2013 :  15:49:27  Show Profile  Reply with Quote
I am curious too...
Visakh, your query returns two rows of zeros when the table is empty... why do you need two rows?
Go to Top of Page

sirmilt
Starting Member

USA
49 Posts

Posted - 04/25/2013 :  18:07:37  Show Profile  Reply with Quote
I don't need two rows, all I wanted was the actual nubber of records that have been entered.




Milt
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/26/2013 :  00:17:19  Show Profile  Reply with Quote
quote:
Originally posted by sirmilt

I don't need two rows, all I wanted was the actual nubber of records that have been entered.




Milt


in case of table being empty what should be your returned resultset?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sirmilt
Starting Member

USA
49 Posts

Posted - 04/26/2013 :  09:27:06  Show Profile  Reply with Quote
Thanks to all of you for the help.

If there are no records all I need is a 0. The while I don't understand the last line of the code, the code posted by visakh16 seems to work fine.

Milt
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/29/2013 :  00:30:22  Show Profile  Reply with Quote
quote:
Originally posted by sirmilt

Thanks to all of you for the help.

If there are no records all I need is a 0. The while I don't understand the last line of the code, the code posted by visakh16 seems to work fine.

Milt


If thats the case

even MuMu88's suggestion should give you result right?



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000