• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

Query generated in MS access work on linked sybase tables but not on its own - please translate!

I basically have an access database with linked tables to a Sybase DB.  I need to use this query in an ASP page, but it won't work.  Have also tried in Sybase interactive but still no joy - can anyone help?  I know its a problem with the left and right joins, and I'm guessing just a syntax issue, but not sure what.

Thanks!

SELECT FFUsers.Companies.Name, FFUsers.Assignments.title, FFUsers.Assignments.status, FFUsers.Assignments.assign_no, FFUsers.Assignments.consult_2, FFUsers.Invoices.inv_date, FFUsers.Invoices.inv_type, FFUsers.Consultant_Fees.fee_comment, FFUsers.UsersPreferences.User_Long, FFUsers.Consultant_Fees.rate1

FROM (FFUsers.Consultant_Fees LEFT OUTER JOIN (FFUsers.Invoices INNER JOIN (FFUsers.Assignments INNER JOIN FFUsers.Companies ON FFUsers.Assignments.client_company_guid = FFUsers.Companies.Companies_GUID) ON FFUsers.Invoices.assignments_guid = FFUsers.Assignments.Assignments_guid) ON FFUsers.Consultant_Fees.related_invoice_guid = FFUsers.Invoices.invoice_guid) LEFT OUTER JOIN FFUsers.UsersPreferences ON FFUsers.Consultant_Fees.consultant = FFUsers.UsersPreferences.User_Short

WHERE FFUsers.Assignments.status Like 'Open*' AND Year(FFUsers.Invoices.inv_date)=2004 AND FFUsers.Invoices.inv_type Like 'Stage*'

ORDER BY FFUsers.UsersPreferences.User_Long DESC;
0
fraggle5
Asked:
fraggle5
  • 3
  • 3
1 Solution
 
fraggle5Author Commented:
Meant to add - where I've now got LEFT OUTER JOIN now, was just LEFT JOIN in Access - without the OUTER this gave me an error in both ASP/ADO and Interactive SQL.  The query above doesn't give me any errors, just no results - when run in Access I get about 500.
0
 
Frostbyte_ZeroCommented:
I reformatted this into the a format that makes sense in Sybase.

SELECT
      C.Name,
      A.title,
      A.status,
      A.assign_no,
      A.consult_2,
      I.inv_date,
      I.inv_type,
      CF.fee_comment,
      UP.User_Long,
      CF.rate1
FROM
      FFUsers.Consultant_Fees CF,
      FFUsers.Invoices I,
      FFUsers.Assignments A,
      FFUsers.Companies C,
      FFUsers.UsersPreferences UP
WHERE
      /* THESE ARE JOINING CLAUSES */
      A.client_company_guid = C.Companies_GUID
      AND
      A.client_company_guid = C.Companies_GUID
      AND
      I.assignments_guid = A.Assignments_guid
      AND
      CF.related_invoice_guid = I.invoice_guid
      AND
      CF.consultant = UP.User_Short
      AND
      /* THESE ARE FILTER CLAUSES */
      A.status Like 'Open%'
      AND
      Year(I.inv_date)=2004
      AND
      I.inv_type Like 'Stage%'

Note that in the WHERE clause there are JOINING CLAUSES. These are equivalent to the ON in ANSI-92 Syntax.

1)
A couple of things. I used all INNER JOINS to reformat your SQL. To make an OUTER JOIN use a * in the side of the = that you want all records from. Like this:

A.client_company_guid =* C.Companies_GUID

-- This would yield all rows from C with matching data from A.

A.client_company_guid *= C.Companies_GUID

-- This would yield all rows from A with matching data from C.

Try to get the joins the way you want and run it in Sybase. Then when you get the desired results paste it into Access in SQL View and Access should reformat to what it thinks should work.

If Access doesn't like the reformatted SQL, make it a pass through query and it should go write through the OLE DB Provider and make the call natively on Sybase and return results to Access.

2)

Also, I have never seen * used as a wildcard with a LIKE operator. Usually the format is LIKE '%string%'. Try replacing the * with a %. Access may be a little too forgiving.
0
 
Frostbyte_ZeroCommented:
Oh yeah, I used Table Aliases. Like FFUsers.UsersPreferences becomes UP. This just makes your code easier to read. I suggest you do that yourself.

In Access, right Click on the table and type the Alias in.

:)
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
fraggle5Author Commented:
Thanks Frostbyte Zero - have made a few modifications and it's (almost) working OK now - using the above with slight modifications it works in Sybase, but not in ASP.  I suspect it's something to do with the *= join syntax - I don't think ASP likes it - I don't get an error, just no results...

PS - ASP doesn't like % as a wildcard, but * works fine hence the change.

PPS - Haven't used aliases as I'm not sure if ASP will like it, and I'd rather minimise the scope for errors.

PPPS - In "FFUsers.Consultant_Fees.related_invoice_guid = FFUsers.Invoices.invoice_guid" I tried to do =* but I get an error - 'Invalid expression in where clause of transact-sql outer join'

SELECT FFUsers.Companies.Name, FFUsers.Assignments.title, FFUsers.Assignments.consult_2, FFUsers.Invoices.inv_date, FFUsers.Invoices.inv_type, FFUsers.Consultant_Fees.fee_comment, FFUsers.UsersPreferences.User_Long, FFUsers.Consultant_Fees.rate1, FFUsers.Consultant_Fees.consultant

FROM FFUsers.Consultant_Fees, FFUsers.Invoices, FFUsers.Assignments, FFUsers.Companies, FFUsers.UsersPreferences

WHERE
FFUsers.Assignments.client_company_guid = FFUsers.Companies.Companies_GUID
AND
FFUsers.Invoices.assignments_guid = FFUsers.Assignments.Assignments_guid
AND
FFUsers.Consultant_Fees.related_invoice_guid = FFUsers.Invoices.invoice_guid
AND
FFUsers.Consultant_Fees.consultant *= FFUsers.UsersPreferences.User_short
AND
FFUsers.Assignments.status Like 'Open%'
AND
Year(FFusers.Invoices.inv_date)=2004
AND
FFUsers.Invoices.inv_type Like 'Stage%'

ORDER BY User_Long DESC;
0
 
Frostbyte_ZeroCommented:
Are you using ASP or ASP.NET? What version of Sybase?

PS - ASP doesn't like % as a wildcard, but * works fine hence the change.
A: ASP Doesn't care. ACCESS uses a "*". All other SQL RDBMS use a "%"

PPS - Haven't used aliases as I'm not sure if ASP will like it, and I'd rather minimise the scope for errors.
A: ASP Doesn't care. Aliases are a standard practice. ACCESS prefers if you specify it as:

SELECT * FROM yourtable AS youralias

However Sybase will accept:

SELECT * FROM yourtable youralias


PPPS - In "FFUsers.Consultant_Fees.related_invoice_guid = FFUsers.Invoices.invoice_guid" I tried to do =* but I get an error - 'Invalid expression in where clause of transact-sql outer join'
A: ACCESS is still trying to validate the syntax using its own rules. If the SQL works in Sybase, do not change it. Create a Select Query in Access without the Wizard. When the window prompts for you to select a table, close it. From the Main Menu Select Query/SQLSpecific/PassThrough. Then Paste the Query into SQL Pass-Through Query. Then save it.

This will make ACCESS ignore the SQL Syntax and pass it directly into Sybase. The results are returned to ACCESS and are then available to you.

This is a difficult way of doing it. You may be better off avoiding ACCESS altogether and consider connecting directly to SYBASE from ASP. You just have to create a DSN Connection using Sybase ODBC on the IIS Server. Or if you have an OLEDB Compliant Driver, you could set the ConnectionString property directly in your ASP. Having ACCESS in the middle just adds another layer of complexity. Expecially if the data actually resides on SYBASE.

The best way to troubleshoot further errors is to have ASP do a Response.Write SQL (assuming SQL is your SQL String Variable). Copy the output from ASP to your clipboard and paste it into WISQL32 (or whatever you use for testing on Sybase). Then modify your ASP to format the string correctly.

0
 
fraggle5Author Commented:
I'm using standard ASP on Windows server 2003.  Sybase is ASA 7.0

Re wildcards, I've tried with a % and I get: ADODB.Recordset (0x800A0CC1) Item cannot be found in the collection corresponding to the requested name or ordinal.  Error disappears if I substitute it for a *.  Not a major concern at the moment I think - I've used * on other ASP queries on Sybase and no problems.

Re aliases, thanks - will still leave as is for the time; I don't think it's causing the problem, but I'll use when I get this sorted.

Don't think I made myself clear enough first time around - I used access to do the initial query as that's where I had it originally (and it's easier for me as I can do it using the design wizard without writing the SQL to being with - not my strong point).  Once I had the SQL from access, I then changed it slightly and ran the query directly in Sybase.  The ASP page does not go through access, it goes straight into Sybase.

The query used in the ASP page is exactly as above, except for substituting * for % as wilcards - but in Sybase I get results, and the ASP page shows 0 results - no error message though.

0
 
namasi_navaretnamCommented:
Few hints:
1) Year not supported in sybase. Use datepart function.
Year(FFusers.Invoices.inv_date)=2004
Use this:
datepart(yy, FFusers.Invoices.inv_date)= 2004

2) Ansi syntax is supported in versions above 12.

3) If your sql works from isql. It should also work thru asp code. First verify that you get results running the sql thru isql. Check the error message from asp.

hope that helps.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now