Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server 2005: Case Logic in Dynamic SQL

Posted on 2010-09-15
2
Medium Priority
?
354 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:Paula DiTallo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 33688335
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:Paula DiTallo
ID: 33688389
brilliantly done!:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

661 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