Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Syntax in comparison with MS Access Expand / Collapse
Author
Message
Posted Wednesday, February 12, 2014 11:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, April 02, 2014 2:21 AM
Points: 34, Visits: 43
Hi, Could anyone please assist me with the following:

SELECT InvNo,InDate,CaseNo,ClientRef,Curr,InvFullAmount,InvOutstAmount From DebtTran
FROM DebtTran
WHERE NameNo = (SELECT TOP 1 (NameNo) FROM DebtTran).

How would this be written in MS Access.

Thanks in advance.

Post #1541034
Posted Friday, February 14, 2014 7:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:05 PM
Points: 1,148, Visits: 6,249
Assuming all your tables are the same SQL to Access, you should be able paste into a new query in SQL view and run it.
TOP 1 with no Order By is questionable.
Post #1541600
Posted Friday, February 14, 2014 7:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
crazy_new (2/12/2014)
Hi, Could anyone please assist me with the following:

SELECT InvNo,InDate,CaseNo,ClientRef,Curr,InvFullAmount,InvOutstAmount From DebtTran
FROM DebtTran
WHERE NameNo = (SELECT TOP 1 (NameNo) FROM DebtTran).

How would this be written in MS Access.

Thanks in advance.



This query won't work in any DBMS as written. There are two FROM clauses.

Why are you using a subquery here? Your subquery is the same table as the base query. The way you have this written it is possible to return more than 1 row. If you have two rows in your table with the NameNo that is returned by your subquery you will get two rows.

Also, as Greg eluded to, you should not use a TOP without and ORDER BY. When you don't include an order by you have no way of knowing which row you will get back.

You could greatly simplify this like this:

SELECT TOP 1 InvNo, InDate, CaseNo, ClientRef, Curr, InvFullAmount, InvOutstAmount 
From DebtTran
order by NameNo --or whatever column makes sense



_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1541604
Posted Friday, February 14, 2014 7:55 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:05 PM
Points: 1,148, Visits: 6,249
Oooooppsss, I see I missed the extra from in the upper query.
Post #1541609
Posted Friday, February 14, 2014 8:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
Greg Edwards-268690 (2/14/2014)
Oooooppsss, I see I missed the extra from in the upper query.


Easy to miss with poor formatting.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1541613
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse