Solved

sql 2012 syntax

Posted on 2016-07-20
4
45 Views
Last Modified: 2016-07-26
Hi Folks
I have to maintain some old embedded SQL code (see section-3 below). It has redundant join syntax, so I replaced it with section-2 below.
When I do the (SQL Server 2012) syntax check all is good.
When I execute the query I get an error (1 below). 'APOpen' is a valid table.
thanks in advance

1.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'APOpen'

----------------------------------------------------
2.
select APPayments.ApplyToID,isnull(max(APOpen.InvoiceID),'Other') InvoiceID,isnull(max(APOpen.POID),'Amount') POID,
sum(case when APOpen.trxtype = 1 or APOpen.trxtype = 4 or APOpen.trxtype = 6 then (APOpen.docAmount + APOpen.docOther + APPayments.MiscAmount) else 0 end) as DocAmount,
sum(case when APOpen.trxtype = 1 or APOpen.trxtype = 4 or APOpen.trxtype = 6 then (APOpen.docAmount) else 0 end) as USInvoiceAmount,
sum(case when APOpen.trxtype = 1 or APOpen.trxtype = 4 or APOpen.trxtype = 6 then (APOpen.docOther) else 0 end) as USExchange,
sum(case when APOpen.trxtype = 2 or APOpen.trxtype = 3 then (APOpen.docAmount + APOpen.docOther) else 0 end) as LessDebits,
(APPayments.AppliedAmount + APPayments.MiscAmount) as AppliedAmount,APPayments.ActualApplied,APPayments.Discount,APPayments.MiscAmount,APPayments.CheckAmount
from APOpen INNER JOIN APPayments ON APOpen.ApplytoId = APPayments.ApplytoId  
Where APOpen.vendid = 9500 and printedflag = '0' group by APPayments.ApplyToID, APPayments.AppliedAmount, APPayments.ActualApplied, APPayments.Discount, APPayments.MiscAmount, APPayments.CheckAmount
order by APOpen.DocAmount

----------------------------------------------
3.
sqlCode = "select P.ApplyToID,isnull(max(O.InvoiceID),'Other') InvoiceID,isnull(max(O.POID),'Amount') POID," & _
'                      "sum(case when O.trxtype = 1 or O.trxtype = 4 or O.trxtype = 6 then (O.docAmount + O.docOther + P.MiscAmount) else 0 end) as DocAmount," & _
'                      "sum(case when O.trxtype = 1 or O.trxtype = 4 or O.trxtype = 6 then (O.docAmount) else 0 end) as USInvoiceAmount," & _
'                      "sum(case when O.trxtype = 1 or O.trxtype = 4 or O.trxtype = 6 then (O.docOther) else 0 end) as USExchange," & _
'                      "sum(case when O.trxtype = 2 or O.trxtype = 3 then (O.docAmount + O.docOther) else 0 end) as LessDebits," & _
'                      "(P.AppliedAmount + P.MiscAmount) as AppliedAmount,P.ActualApplied,P.Discount,P.MiscAmount,P.CheckAmount from APOPEN O,APPayments P" & _
'                      " Where O.vendid = " & txtVendID.Text & _
'                      " And O.ApplytoId =* P.ApplytoId and printedflag = '0'" & _
'                      " group by P.ApplyToID, P.AppliedAmount,P.ActualApplied,P.Discount,P.MiscAmount,P.CheckAmount order by O.DocAmount"
0
Comment
Question by:COACHMAN99
[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
4 Comments
 
LVL 8

Assisted Solution

by:LajuanTaylor
LajuanTaylor earned 500 total points
ID: 41722031
@sql 2012 syntax - Did you make  sure that you are selecting from the correct database? Try adding  "USE YourDatabaseNameHere" before your select statement.
1
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 0 total points
ID: 41722038
Thanks for your input Lajuan.
when I re-opened and re-entered the query the error moved/changed and I had to add APOpen.DocAmount to the group by clause. now it works.
P.S. use database only really applies when you aren't working in the current database
cheers
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41722943
>> I had to add APOpen.DocAmount to the group by clause

Surely because your ORDER BY has "order by O.DocAmount" which should surely read as  "order by DocAmount" which is the alias of your summed field. Then you should be able to remove that field as well from the GROUP By clause.
0
 
LVL 7

Author Closing Comment

by:COACHMAN99
ID: 41729215
the syntax checker gave an invalid error until the utility was reloaded and indicated a missing 'order by'.
Lajuan's comment is valid under different circumstances, but not when the query is run via VB/ADO or in the current database in Management Studio.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

695 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