How to implement pagewise loading in MSAccess - sql

In my application that using MSAccess as DB, I need to implement pagewise loading with sql query.
I know how to load first 10 records, thats by
SELECT TOP 10 * FROM Product ORDER BY dateAdded DESC
But how can I pick record that is from 10 to 20.
Any Idea?

It's possible in Access SQL, but not as straightforward as in other database products.
(for example MySQL, where it would be just LIMIT 10,10)
Check out my answer here:
How to do MS Access database paging + search?
(the code to build the SQL Statement is in C#, but of course you can do it in any other language as well. If you don't know C# and need help understanding my answer, just leave a comment here)

Related

How to paginate results in Legacy SQL

We are using Legacy SQL on a specific request. We can't use standard SQL for some internal reasons.
We would like to paginate our results, because we have a lots of rows. Like that :
SELECT ... FROM ... LIMIT 10000 30000 // In standard SQL
But in Legacy SQL Offset don't exists. So how to do the same job ?
Edit :
I don't want to order. I want to paginate. For example get 1000 rows after skipping 2000 rows. A simple LIMIT clause with an offset, like in traditional SQL Database or like in BigQuery Standard SQL.
To do this, I want to use Big Query Legacy SQL.
The pagination you talking about is done via tabledata.list API
Based on your question and follow-up comments - It might be the way for you to go. Even though it does not involve querying. Just API or related method in client of your choice.
pageToken parameter allows you to page result
Btw, another benefit of this approach - it is free of charge
If you still need to do pagination via query - you option is using ROW_NUMBER()
In this case - you can prepare your data in temp table with below query
SELECT <needed fields>, ROW_NUMBER() OVER() num
FROM `project.dataset.table`
Then, you can page it using num
SELECT <needed fields>
FROM `project.dataset.temp`
WHERE num BETWEEN 10000 AND 30000

Quickly navigate through data with SQL Management Studio

I'm a developer who works a lot in an environment with lots of FK relations.
When I'm developing my application, I would like the ability to quickly navigate through data.
I'll explain it through an example :)
Table - Car
ID TypeID CarName ...
1 1 A
2 1 B
3 2 C
I would get this data if I did a "SELECT * FROM car". The problem is that I only get a limit amount of data. I don't know which Type Car A is, I only know its TypeID.
How I handle this now, is by either writing an extended SQL query (inner join it with type and also include the name) or by doing a SELECT * FROM type where ID = 1.
I'd like to be able to have this data presented to me quickly (and I'm not talking about a regular view). I would like to write an extension on SQL management studio, in which I would check if the table had a FK-relation. If it's filled in, I'd then give a tooltip if I hover over the FK-cell.
The question: is this possible? Can I write extensions on SQL Management studio? Does something like this already exist within SQL Mang studio or any third party packages?
If it doesn't exist, but I can make it, how would I go about doing it?
Thanks for the help :)
LINQPad is a c# scratchpad that speaks LINQ2SQL.
I use it a lot to query data.
You can quite easily mix LINQ2SQL and c# code to do more advanced queries and calculations than possible in plain SQL.
The free version works OK, but if you pay you get autocompletion too.

Firebird rownum *or* linq style skip and take

I'm using firebird database and it does not seem to have ROWNUM or ROW_NUMBER() like sql server does so I cannot follow the procedure explained here.
I have a query whose result dataset is bigger than what my system memory can accommodate. I'm trying load the dataset in smaller chunks. I have a series of generic queries which I cannot modify and they could be anything. I know I can do
select first 5000 * from
(-my actual query here-)
to get the first 5000 records. But how can I go about getting the next 5000 records.
Thanks
Since FireBird 2.0 ROWS syntax is supported, ie you would use
select * from tab ROWS m TO n
I suggest you download FB's language reference update where it is documented.
In firebird you use Select First ? Skip ? to specific how many, and what your offset is.

MS Access Limit

What's the equivalent of mysql Limit in ms access. TOP is not sufficient since I'm going to use it for pagination.
Thanks
There isn't one. Your best bet is to add an ID column as a primary key (if you don't already have one) and chunk output by looping through:
SELECT * FROM table
WHERE id >= offset AND id <= offset + chunk_size - 1
until you get all the rows.
Curiously, there are a few references in Microsoft documentation to a LIMIT TO nn ROWS syntax for the Access Database Engine:
ACC2002: Setting ANSI 92 Compatibility in a Database Does Not Allow DISTINCT Keyword in Aggregate Functions
About ANSI SQL query mode (MDB)
However, actual testing seems to confirm that this syntax has never existed in a release version of the Access Database Engine. Perhaps this is one of those features that the SQL Server team wanted to put into Jet 4.0 but were ordered to rollback by the Windows team? Whatever, it seem we must simply put it down to a bad documentation error that Microsoft won't take the time to correct :(
If you need to do pagination on the server** side then I suggest you consider a more capable, modern SQL product with better documentation ;)
** conceptually, that is: the Access Database Engine is not a server DBMS.
Since it doesn't appear that you have any type of sequencial unique key number for these rows, you'll need to create a ranking column: How to Rank Records Within a Query
You need to determine how many rows at a time you will return N = (10, 25,100).
You need to keep track of what "page" the user is on and the values of the first and last rank.
Then when you make the call for the next page it is either the next N rows that are > or < the first and last ranks (depending if the users is going to the previous or next page.).
I'm sure there is a way to calculate the last page, first page, etc.
Only way to achive paging SQL similar to Limit statement by using TOP keywords is as follows:
First Step:
sql = "select top "&LESS_COUNT&" * from (SELECT top "&(PAGE_COUNT*getPage)&" * FROM (SELECT "&COLUMNS&" FROM "&TABLENAME&") AS TBL "&getWhere&getOrderby("asc")&") as TBL "&getOrderby("desc")
Second step:
sql = "SELECT TOP "&PAGE_COUNT&" * FROM (" & sql & ") as TBL "&getOrderby("asc")
To summarize; you should re-order and make the results upside down for 3 times.
port your project to PHP & MySQL. Better support for these type of actions and queries and much much better online documentation. As a 16 year veteran DB developer, I have grown to dispise MS Access and MS SQL with a passion unmatched by anything else. This is due exclusively to their lack of support and documentation.

Is there a way to parser a SQL query to pull out the column names and table names?

I have 150+ SQL queries in separate text files that I need to analyze (just the actual SQL code, not the data results) in order to identify all column names and table names used. Preferably with the number of times each column and table makes an appearance. Writing a brand new SQL parsing program is trickier than is seems, with nested SELECT statements and the like.
There has to be a program, or code out there that does this (or something close to this), but I have not found it.
I actually ended up using a tool called
SQL Pretty Printer. You can purchase a desktop version, but I just used the free online application. Just copy the query into the text box, set the Output to "List DB Object" and click the Format SQL button.
It work great using around 150 different (and complex) SQL queries.
How about using the Execution Plan report in MS SQLServer? You can save this to an xml file which can then be parsed.
You may want to looking to something like this:
JSqlParser
which uses JavaCC to parse and return the query string as an object graph. I've never used it, so I can't vouch for its quality.
If you're application needs to do it, and has access to a database that has the tables etc, you could run something like:
SELECT TOP 0 * FROM MY_TABLE
Using ADO.NET. This would give you a DataTable instance for which you could query the columns and their attributes.
Please go with antlr... Write a grammar n follow the steps..which is given in antlr site..eventually you will get AST(abstract syntax tree). For the given query... we can traverse through this and bring all table ,column which is present in the query..
In DB2 you can append your query with something such as the following, but 1 is the minimum you can specify; it will throw an error if you try to specify 0:
FETCH FIRST 1 ROW ONLY