May 7, 2014 at 10:28 am
The user can put in the following parameters and I'm trying to create an sql statement that would work for the parameters specified.
The :comp is company number; user can enter one company or 'all' which is 0
:year, :fprd, tprd should be self explanatory and
:ccty is for country; the supplier country and receiver company must be the same no matter what so the user can select a certain country or the can select 'all' which is 0
When I run the statement for 'all' on country then I get the records but if I select a country, the query gets an error. What am I doing wrong with this sql statement????
I am having problems with the following sql statement:
select ledger_account, supplier_country, receiver_country, supplier_number, supplier_name, supplier_invoice_number, purchase_order_number, tran_type_po, document_number_po, invoice_amount,
currency, invoice_amount_hc, vat_account_po, vat_amount_po, tran_type_app, document_number_app, vat_account_app, vat_amount_app
from ADTRAN.ad_vat_recon
where (finance_company = :comp or :comp=0)
and fiscal_year = :year
and fiscal_period between :fprd and :tprd
and (supplier_country = receiver_country and :ccty = 0 or
trim(supplier_country) = :ccty and trim(receiver_country) = :ccty)
May 7, 2014 at 12:26 pm
What SQL dialect is it?
In MS SQL Server T-SQL you should use parameters in form @year, @ccty, etc.
May 7, 2014 at 12:30 pm
this would be oracle sql
May 7, 2014 at 12:34 pm
michelle.malone (5/7/2014)
this would be oracle sql
This site is dedicated to SQL Server. There is an Oracle section. You might find some luck there. You may find that you have better luck on a dedicated Oracle site. Not that we are not willing to help it is just that most of us are not that strong with Oracle. I can barely spell it.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 8, 2014 at 9:15 am
I think you probably need the country part of the WHERE clause as below;
select ledger_account, supplier_country, receiver_country, supplier_number, supplier_name, supplier_invoice_number, purchase_order_number, tran_type_po, document_number_po, invoice_amount,
currency, invoice_amount_hc, vat_account_po, vat_amount_po, tran_type_app, document_number_app, vat_account_app, vat_amount_app
from ADTRAN.ad_vat_recon
where (finance_company = :comp or :comp=0)
and fiscal_year = :year
and fiscal_period between :fprd and :tprd
and (supplier_country = receiver_country and (:ccty = 0 or trim(supplier_country) = :ccty ))
Once you've established that the supplier and receiver country are the same, you only need to check one of them is equal to the input. Your main problem was that the AND takes precedence over the OR.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply