Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Check if a table is not empty

Author  Topic 

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 2006-11-15 : 09:41:20
Hi,

i wanna know how can i check a table has datas inside, because i wanna use delete from table and if table is empty i dont wanna run this statement.

thanks in advance

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-15 : 09:45:07
If exists(select * from table)
--Data exists
else
--doesnt exist

Also why do you want to check this?
You can apply delete on empty table also

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-15 : 09:59:50
TRUNCATE TABLE tablename

What do you care to do a check in the first place

Souunds like you'd be better off with a temp table or table variiable in thefirst place



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 2006-11-15 : 10:01:21
on ibm db2, when i use delete from table..., it gives error saying delete on an empty table..
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-11-15 : 11:14:05
quote:
Originally posted by raysefo

on ibm db2, when i use delete from table..., it gives error saying delete on an empty table..



Are you working with DB2?



CODO ERGO SUM
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-11-15 : 12:16:43
quote:
Originally posted by raysefo

on ibm db2, when i use delete from table..., it gives error saying delete on an empty table..



It gives you a -100, which is a sql cond code, the equivilent of @@ROWCOUNT=0.

What language are you coding in...all you need to do it check for the sql cond codes and handle them.

Oh, and try here

http://www.dbforums.com/forumdisplay.php?f=8

That's a DB2 site, this is a SQL Server site

But I would be glad to help you with DB2



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -