Register  Login  
  July 29, 2010  
Search_Blog Minimize
Print  

Blog_Archive Minimize
Print  

Blog    
View_Blog Minimize
Jan 9

Written by: host
Monday, January 09, 2006 7:13:00 PM 

Today I started to create a rather simple stored procedure that I thought would take a few minutes... Well it fell into the "20 minute" rule... if you estimate about "20 minutes" it won't be...

Anyway, this procedure needed to build SQL Select based on parameters passed in.  So, I needed to user SQL EXECUTE statement.  I create @SQL as the varchar that I contained the select statement and passed the command to the EXECUTE.. When procedure ran, I got the MSG 203  error message, but it was truncated due to the length of the SQL statement..  No problem, off to Books Online to determine what the MSG 203 message -- not there... Google?

OK .. 20 minutes later I finally discovered that any statement I passed into the EXECUTE statement did not work.  Hmmmm.  So back to the EXECUTE statement documentation.  Seems that I had missed a little detail.  If you are using a variable -- eg. @SQL in my case -- you must surround the EXECUTE parameter statement in brackets.  

So...(using the NorthWind database for example)

DECLARE @SQL AS VARCHAR(50)
SET @SQL = 'select * from orders'
EXEC @SQL

doesn't work, but

DECLARE @SQL AS VARCHAR(50)
SET @SQL = 'select * from orders'
EXEC (@SQL)   <-- notice the brackets

does...  lesson learned.   50 minutes later...  The "20 minute" rules lives!

Tags:

1 comment(s) so far...

Re: The 20 Minute Rule

I really liked your method. thanks for sharing this:) hope many people will find it useful as I did. have read lots of articles on the topic downloaded by rapid4me.com SE , but have never thought that could be so easy:)

By Rapid Share on   Tuesday, February 02, 2010 1:15:13 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 

Blog_List Maximize
Print  

  Home | Blog | Forum | Subscriptions | Free Modules   Page generated in 0.0801152 seconds.
  Copyright 2010 by TressleWorks   Terms Of Use | Privacy Statement