?
Solved

Help with using Dynamic SQL and ISNULL

Posted on 2007-07-23
1
Medium Priority
?
1,025 Views
Last Modified: 2010-05-18
The following SQL does not work.  It is due to the addition of ISNULL.  this must be dynamic SQL
 I do not know how to break the quotes so the nulls are replaced by nothing.

CREATE procedure GetLabels
@Locations varchar(100)

as

Declare @SQL as Varchar(1000)

Select @SQL ='Select isnull(V.firstname,'') as FirstName, isnull(V.lastname,'') as LastName,
      isnull(V.company,'') as Company, isnull(V.address1,'') as Address1,
      isnull(V.address2,'') as Address2, isnull(V.city,'') as City,
      isnull(S.state, '') as State, isnull(V.Zip, '') as Zip
FROM Vendordb.Vendor V
      Join Vendordb.states S on S.Stateid = V.Stateid
Where V.VendorID in (Select Distinct B.VendorID
                  From Vendordb.Booth B
                  Join Vendordb.BoothSpace BS on BS.BoothID = B.BoothID
                  Join Vendordb.Spaces S on S.SpaceID = BS.SpaceID
                  Join Vendordb.Location L on L.LocationID = S.LocationID
                  Where L.LocationID in ('+@Locations+'))'

Exec(@SQL)


GO
0
Comment
Question by:flg8tor96
1 Comment
 
LVL 15

Accepted Solution

by:
derekkromm earned 2000 total points
ID: 19551121
Double up the ''s.

Select @SQL ='Select isnull(V.firstname,'''') as FirstName, isnull(V.lastname,'''') as LastName,
      isnull(V.company,'''') as Company, isnull(V.address1,'''') as Address1,
      isnull(V.address2,'''') as Address2, isnull(V.city,'''') as City,
      isnull(S.state, '''') as State, isnull(V.Zip, '''') as Zip
FROM Vendordb.Vendor V
      Join Vendordb.states S on S.Stateid = V.Stateid
Where V.VendorID in (Select Distinct B.VendorID
                  From Vendordb.Booth B
                  Join Vendordb.BoothSpace BS on BS.BoothID = B.BoothID
                  Join Vendordb.Spaces S on S.SpaceID = BS.SpaceID
                  Join Vendordb.Location L on L.LocationID = S.LocationID
                  Where L.LocationID in ('+@Locations+'))'
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question