![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, is there a way I can check if a table is empty and contains no rows? I have a Table1 which being dynamic can sometimes end up with now columns what so ever. I'm using Table1 in one of my views along with 2 other tables and I would like put a condition at the...something like AND Table1 IS NOTEMPTY |
#3
| |||
| |||
|
|
On 18 Aug, 15:52, Yas <yas... (AT) gmail (DOT) com> wrote: Hi, is there a way I can check if a table is empty and contains no rows? I have a Table1 which being dynamic can sometimes end up with now columns what so ever. I'm using Table1 in one of my views along with 2 other tables and I would like put a condition at the...something like AND Table1 IS NOTEMPTY Actually could I possibly do something like the following? OR is there a better way? DECLARE @myCount int SELECT @myCount = (select count(*) FROM Table1) SELECT xyz FROM Table1, Table2 etc joins etc etc WHERE .... AND @mycount > 10 |
#4
| |||
| |||
|
|
Hi, is there a way I can check if a table is empty and contains no rows? I have a Table1 which being dynamic can sometimes end up with now columns what so ever. I'm using Table1 in one of my views along with 2 other tables and I would like put a condition at the...something like AND Table1 IS NOTEMPTY |
#5
| |||
| |||
|
|
Yas (yas... (AT) gmail (DOT) com) writes: Hi, is there a way I can check if a table is empty and contains no rows? I have a Table1 which being dynamic can sometimes end up with now columns what so ever. I'm using Table1 in one of my views along with 2 other tables and I would like put a condition at the...something like AND Table1 IS NOTEMPTY AND EXISTS (SELECT * FROM Table1) The EXISTS / NOT EXISTS are part of the essential SQL anyone who is working with SQL should learn. The above example is simple, but say you want all customers who have placed an order / who have never placed an order: SELECT C.CustomerID, C.CompanyName FROM Customers C WHERE /* NOT */ EXISTS (SELECT * FROM Orders O WHERE O.CustomerID = C.CustomerID) You may be used to using IN / NOT IN for this, but (NOT) IN is limited, as you get problem if you have a multi-column key. Plus that NOT IN can trap you if NULL values are involved. The query above runs in the Northwind database, if you want to try it. |
#6
| |||
| |||
|
|
Oh that's good, thanks! :-) ...and if I wanted to say make sure in a table where normaly there should be 2000 rows, there are at least 1500 present could I just simply do the following...or is there a better way? AND ((SELECT COUNT(*) FROM TABLE1) > 1500) what do you think of that? |
#7
| |||
| |||
|
|
Erland Sommarskog <esquel (AT) sommarskog (DOT) se> wrote in news:Xns9990ED010F0E2Yazorman (AT) 127 (DOT) 0.0.1: You may be used to using IN / NOT IN for this, but (NOT) IN is limited, as you get problem if you have a multi-column key. Plus that NOT IN can trap you if NULL values are involved. Erland, Could you elaborate on this or point me to an appropriate web page, please. |
#8
| |||
| |||
|
|
Consider the script below. |
![]() |
| Thread Tools | |
| Display Modes | |
| |