Solved

SQL Server 2005: Case Logic in Dynamic SQL

Posted on 2010-09-15
2
350 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
[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 500 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:ditallop
ID: 33688389
brilliantly done!:-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

729 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