Solved

SQL Error 207

Posted on 2007-11-14
2
1,546 Views
Last Modified: 2012-08-13
On this piece of sql code I'm getting an error. Here is the code:

(OID.[Pieces]-OID.[delivered]) AS NotShipped,  CONVERT(INT,NotShipped/CIL.[Unit]) AS Cases, NotShipped % CIL.[Unit] AS Piece,

and here is the Error:
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name 'NotShipped'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'NotShipped'.

The error is with this piece of code:
CONVERT(INT,NotShipped/CIL.[Unit]) AS Cases, NotShipped % CIL.[Unit] AS Piece,

What am I doing wrong? am I not able to refer to a previously created column?

0
Comment
Question by:Tammak1
2 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 20284682
what columns are in the table
0
 
LVL 25

Accepted Solution

by:
imitchie earned 50 total points
ID: 20284782
you can't use a named column in the select statement. put it in a a subquery. i.e.

select *, CONVERT(INT,NotShipped/[Unit]) AS Cases, NotShipped % [Unit] AS Piece
from
(
select
field1, field2, field3, (OID.[Pieces]-OID.[delivered]) AS NotShipped, field5
from table
) innertable

either that or

(OID.[Pieces]-OID.[delivered]) AS NotShipped,  CONVERT(INT,(OID.[Pieces]-OID.[delivered])/CIL.[Unit]) AS Cases, (OID.[Pieces]-OID.[delivered]) % CIL.[Unit] AS Piece,
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

776 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