Solved

SQL Server 2005: Case Logic in Dynamic SQL

Posted on 2010-09-15
2
346 Views
Last Modified: 2012-05-10
Hi Techies--

I am getting the error:

Incorrect syntax near the keyword 'as'.

...because I am trying to dub a column name within case/end logic.  Does anyone have a suggestion on how to solve this issue?

[begin snippet]



SET @Sql = N'

SELECT

 pat.AssemblyNum as ''assemblyNumber'',

 pat.PatternNum as ''patternNumber'',

 pat.Corner as ''cornerNumber'',

 class.PatClassTypeName as ''className'',

 case

  when pat.MatchTypeID = ''101'' then pat.Width/2 as ''repeatWidth''

  else pat.Width as ''repeatWidth''

 end ,

 pat.Length as ''repeatLength'',

 match.MatchTypeName as ''matchType'',

 pat.CollectionID as ''collectionID'',

 coll.CollectionName as ''collectionName''

FROM PatternClassType class, Collections coll, Patterns pat

    LEFT OUTER JOIN MatchType match

       ON pat.matchtypeid = match.matchtypeid



[end snippet]

Open in new window

0
Comment
Question by:ditallop
2 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
Comment Utility
Your "AS" has to be on the entire case statemented column, not on each possible value.  So the correct syntax would be...


SET @Sql = N'
SELECT
 pat.AssemblyNum as ''assemblyNumber'',
 pat.PatternNum as ''patternNumber'',
 pat.Corner as ''cornerNumber'',
 class.PatClassTypeName as ''className'',
 case
  when pat.MatchTypeID = ''101'' then pat.Width/2
  else pat.Width
 end as ''repeatWidth'',
 pat.Length as ''repeatLength'',
 match.MatchTypeName as ''matchType'',
 pat.CollectionID as ''collectionID'',
 coll.CollectionName as ''collectionName''
FROM PatternClassType class, Collections coll, Patterns pat
    LEFT OUTER JOIN MatchType match
       ON pat.matchtypeid = match.matchtypeid

0
 

Author Comment

by:ditallop
Comment Utility
brilliantly done!:-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now