문제

How to Check whether a table contains rows or not sql server 2005?

도움이 되었습니까?

해결책

For what purpose?

  • Quickest for an IF would be IF EXISTS (SELECT * FROM Table)...
  • For a result set, SELECT TOP 1 1 FROM Table returns either zero or one rows
  • For exactly one row with a count (0 or non-zero), SELECT 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

Like Other said you can use something like that:

IF NOT EXISTS (SELECT 1 FROM Table)
  BEGIN 
    --Do Something
  END 
ELSE
  BEGIN
    --Do Another Thing
  END

FOR the best performance, use specific column name instead of * - for example:

SELECT TOP 1 <columnName> 
FROM <tableName> 

This is optimal because, instead of returning the whole list of columns, it is returning just one. That can save some time.

Also, returning just first row if there are any values, makes it even faster. Actually you got just one value as the result - if there are any rows, or no value if there is no rows.

If you use the table in distributed manner, which is most probably the case, than transporting just one value from the server to the client is much faster.

You also should choose wisely among all the columns to get data from a column which can take as less resource as possible.

Can't you just count the rows using select count(*) from table (or an indexed column instead of * if speed is important)?

If not then maybe this article can point you in the right direction.

제목 이외에 별도의 URL이 아니라 스코어 카드를 호스팅하는 웹 파트에 대한 제목 URL을 사용했습니다.위의 위의 그림에서 카트만두 밸리 프로젝트 을 클릭하면 원하는 SharePoint 페이지로 이동합니다.

아래의 웹 부분의 제목 URL을 편집하는 데 필요한 단계입니다.

  1. 웹 파트 편집을 클릭합니다.
  2. 고급 섹션을 확장합니다.
  3. 제목 URL 값을 채 웁니다.

    스코어 카드에서 요약 페이지로 리디렉션하는 목적을 달성하는 대안적인 방법을 알려 줬습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top