Solved

SQL Server 2005: Case Logic in Dynamic SQL

Posted on 2010-09-15
2
351 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

622 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