Get count or find out has row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorandoran
    New Member
    • Feb 2007
    • 145

    Get count or find out has row

    I need to find out whether the following statement has any rows or not. Either by count>=1 or hasrow. Are there any other option beside SqlDataReader & SqlDataAdapter since they might take little longer. Or basically I am looking for faster way to do this.

    Code:
            string strSQL = @"select d.department_name from departments d
                            where not exists 
                            (select dm.department_name, dm.emp_id from department_membership dm
                            where (d.department_name=dm.department_name) and (dm.emp_id=@emp_id))
                            order by d.department_name";
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
            conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL,conn);
    OR

    what if I use this query and do the count on the statement then How would I tell the program look for count >=1

    Code:
    SELECT count(holiday_description) 
    from HOLIDAY_SCHEDULE HS
    where NOT EXISTS 
    (select holiday_description  from HOLIDAY_DESCRIPTION hd 
    WHERE HS.holiday_description=hd.holiday_description)
  • kunal pawar
    Contributor
    • Oct 2007
    • 297

    #2
    Query required more time to execute than Store procedure so prefare SP. Another thing you can right query using self join which make it more faster.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      If you don't care about the data and just need to know if a record exists I'd implement a Stored Procedure (as kunal pawar has suggested) that does a "SELECT count(x) Where Blah". Stored procedures are precompiled by the DBMS (database management system) which means your sql query doesn't have to be compiled when you execute it.

      Comment

      • dorandoran
        New Member
        • Feb 2007
        • 145

        #4
        Great. I have finally create Store proc to take care of this issue. Thanks guys for your input.

        Comment

        Working...