Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

July 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

XML Feeds

Authors

« Cumulative Update 1 to the RML Utilities for Microsoft SQL Server ReleasedUser to Schema to Roles for controlling security »
The Data Management Journal

What is wrong with this code?

by SQLDenis


Permalink 12 Nov 2008 08:51 , Categories: Data Modelling & Design, Database Programming, Microsoft SQL Server Tags: coding, sql server 2005, sql server 2008

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

Leave a comment »Send a trackback » 792 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

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