Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Cumulative Update 1 to the RML Utilities for Microsoft SQL Server ReleasedUser to Schema to Roles for controlling security »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Take a look at this code which I found a while back in a stored proc

  1. DECLARE @id INT,@xtype CHAR(1),@uid INT,@info INT,@status INT
  2.  
  3. SET  @id =(SELECT id FROM sysobjects WHERE name = 'sysobjects')
  4. SET @xtype  =(SELECT xtype FROM sysobjects WHERE name = 'sysobjects')
  5. SET @uid   =(SELECT uid FROM sysobjects WHERE name = 'sysobjects')
  6. SET @info  =(SELECT info FROM sysobjects WHERE name = 'sysobjects')
  7. SET @status =(SELECT status FROM sysobjects WHERE name = 'sysobjects')
  8.  
  9. SELECT @id ,@xtype ,@uid ,@info ,@status
  10. go

Do you see what is wrong? It uses five select statements to accomplish something which can be done in one. I would do something like this instead.

  1. DECLARE @id INT,@xtype CHAR(1),@uid INT,@info INT,@status INT
  2.  
  3. SELECT @id =id
  4. ,@xtype =xtype
  5. ,@uid =uid
  6. ,@info =info
  7. ,@status =status
  8. FROM sysobjects WHERE name = 'sysobjects'
  9.  
  10. SELECT @id ,@xtype ,@uid ,@info ,@status

Let's take a look at another example.

What we want to do is display a row of counts for 4 xtypes from the sysobjects table, here is an example

s	u	p	c
19	143	74	6

Have you ever seen code like this that does that? I have!

  1. SELECT COUNT(*) AS [s],
  2. (SELECT COUNT(*) FROM  sysobjects WHERE xtype = 'u') AS [u],
  3. (SELECT COUNT(*) FROM  sysobjects WHERE xtype = 'p') AS [p],
  4. (SELECT COUNT(*) FROM  sysobjects WHERE xtype = 'c') AS [c]
  5. FROM  sysobjects
  6. WHERE xtype = 's'

That code will do a select 4 times against the table
A better way would be to do this

  1. SELECT  SUM(CASE xtype WHEN 's' THEN 1 ELSE 0 END) AS [s],
  2. SUM(CASE xtype WHEN 'u' THEN 1 ELSE 0 END) AS [u],
  3. SUM(CASE xtype WHEN 'p' THEN 1 ELSE 0 END) AS [p],
  4. SUM(CASE xtype WHEN 'c' THEN 1 ELSE 0 END) AS [c]
  5. FROM sysobjects
  6. WHERE xtype in('s','u','p','c')

In SQL server 2005/2008 you can use the PIVOT operator, here is what the query would look like

  1. SELECT s, u, p, c
  2. FROM
  3. (SELECT xtype
  4. FROM sysobjects
  5. WHERE xtype IN('s','u','p','c')) AS pivTemp
  6. PIVOT
  7. (   COUNT(xtype)
  8.     FOR xtype IN(s, u, p, c)
  9. ) AS pivTable

If you can think of any other examples feel free to leave a comment

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
928 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)