How to Check whether a table contains rows or not sql server 2005?
Designed to provide a small overhead check of the physical consistency of the table, this check can also detect torn pages, and common hardware failures that can compromise data. A full run of DBCC CHECKTABLE may take considerably longer than in earlier versions.
Microsoft SQL Server Database Console Commands (DBCC) are used for checking database integrity; performing maintenance operations on databases, tables, indexes, and filegroups; and collecting and displaying information during troubleshooting issues.
Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).
For what purpose?
IF EXISTS (SELECT * FROM Table)...
SELECT TOP 1 1 FROM Table
returns either zero or one rowsSELECT COUNT(*) FROM Table
Also, you can use exists
select case when exists (select 1 from table) then 'contains rows' else 'doesnt contain rows' end
or to check if there are child rows for a particular record :
select * from Table t1 where exists( select 1 from ChildTable t2 where t1.id = t2.parentid)
or in a procedure
if exists(select 1 from table) begin -- do stuff end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With