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 Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 2:30 AM
Points: 85, Visits: 114
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


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 2:10 PM
Points: 680, Visits: 6,854
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: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 2:10 PM
Points: 680, Visits: 6,854
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: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
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