Solved

SQL Error 207

Posted on 2007-11-14
2
1,545 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 the fundamental information of how to create a table.

910 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

22 Experts available now in Live!

Get 1:1 Help Now