SQL Server 2012 - Pagination without Order Clause - sql

I found best approach for pagination in SQL Server 2012 is OFFSET FETCH Clause
SELECT First Name + ' ' + Last Name
FROM Employees
ORDER BY First Name OFFSET 10 ROWS;
It works fine.
Now in my case, my software generate SQL Server table dynamically and I just know table name.
I don't know columns name in dynamically generated table.
Now in this case how this pagination works ? because I don't know which column should be used with ORDER BY clause with OFFSET Fetch clause..
any alternative solution?
Thanks

You can order by column index if that helps:
SELECT First Name + ' ' + Last Name
FROM Employees
ORDER BY 1 OFFSET 10 ROWS;
This will order by the first column. If the ordering column is stored on the UI (controlled by users), then you need to store the column index on the UI and pass that to SQL Server to use in the ordering.
If you simply want to order by the order the records are added to the table, you will need a primary key, identity column. Make sure that is the first column on the table and use ORDER BY 1.

As #Tanner mentioned in his answer, in order to use his answer you have to put identity column as the first column of your query, if you can do that, use his answer, but if you can not, use (SELECT NULL) instead of 1, e.g:
SELECT First Name + ' ' + Last Name
FROM Employees
ORDER BY (SELECT NULL) OFFSET 10 ROWS;

Related

Select the first 20 columns

Is it possible to select the first 20 columns from my table, without naming out each column in the select?
The columns are always ordered in the same way when I do a select, so there must be some underlying order.
The below query forms the SQL for you. It uses the dictionary table all_tab_columns to fetch the column names for the table.
SELECT ' SELECT '
|| REPLACE(LISTAGG(column_name,',') WITHIN GROUP( ORDER BY column_id),',',','
||CHR(10))
|| ' FROM YOUR_TABLE'
FROM all_tab_columns
WHERE owner ='YOUR_SCHEMA_NAME'
AND table_name='YOUR_TABLE_NAME'
AND column_id <= 20;
you can use column index instead of column name like select 0,1,2,.....
There is a table in a SQL Server database called sysColumns that records all the columns in every table. I think it is a SQL standard and should be in Oracle too.
EDIT: thanks to comment from #davegreen100, this table is in Oracle but is named DBA_TAB_COLUMNS.
try running Select * from DBA_TAB_COLUMNS and see what the results are, and work from there.
If it's there (in Oracle), you will eventually end up with something like
Select name from DBA_TAB_COLUMNS
Where id = #tableId -- <--- the id of the table
and colOrder <= 20
Your final SQL will be probably have to be generated dynamically using the output from the above

Oracle fetching columns names does not retrieve the result in the same order everytime

So, I use the following SQL command to fetch the name of all columns of a table, but I also wanted them to be returned in the order they were created, for example:
create table X{
name varchar(255),
number varchar(255),
something varchar(255))
I'd like that the return list was: name - number - something; but I keep getting different results. sometimes in that order, sometimes in the opposite. I'd like a command that retrieves in the order of creation, or in the opposite, I don't want "sorts" by alphabetical order or something. This is the command I use:
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'X'
Idf you want to be sure of the order You could use
COLUMN_ID Sequence number of the column as created
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'X'
ORDER BY column_id
Your question says that you don't want "sorts". Tables are defined in the relational model as an unordered set of records. If you want them in a specific order, you must specify an ORDER BY clause. Without an ORDER BY the database returns records in whatever order it finds convenient.
If you wish to know the order of creation, you will need to store a datetime value for that time of creation in a column in the table. Then, when you retrieve data from the table you must ORDER BY that value.
You could also try using a SEQUENCE value instead of a datetime, but those may or may not work as well with record churning. (I have limited experience with Oracle SEQUENCEs.) Even then, however, you will still need an ORDER BY.
SELECT column_name
FROM USER_TAB_COLUMNS
WHERE table_name = 'X'
ORDER BY COLUMN_ID

SQL statement to get largest ID from database column

I have an ID column which it supposed to set to auto-increment but I forgot to set in when creating the database. Let's say the ID is from 1 - 20. I used the select Max() Sql statement to get the largest ID:
SELECT MAX(id) FROM Table_Name;
It supposed to return me 20. However, it returns me 9. I also realized that the id column in database is jumbled up. It starts from 1,2 then skips to 9,10 - 20 then back to 3 - 8. And 8 appears to be the last row and I think that's where the 9 comes from. My id in database is varchar() data type.
So, is there any way to amend my Sql statement to get the largest id in a list of sorted id?
Thanks in advance.
The issue is likely that the ID column is a varchar field, so 9 is greater than 10.
select max(convert(int, id)) from Table
Your column is a character type, not a numeric type, which explains everything you're seeing.
Try casting it to numeric:
select max(cast(id as signed)) from table
You haven't said which database you are using, so the syntax may vary to achieve the cast - consult online docs for your database.
Try this:
SELECT TOP 1 Id FROM Table ORDER BY Convert(INT, id) DESC

similar to last() in sql, what can i use in derby database?

Select Last(column_name) from table_name will return the last value in the column in standard SQL. What is a similar query that will work in derby to fetch the last value in the column?
Please find the sample table below: 'secondcol' is the column name in the table.
secondcol
33
45
78
Select Last(secondcol) from table_name in sql will return 78 as output. If i want to get similar output in javadb/derby database, how can i query it? I don't want to change any order in the column values.
To select last row in table is little bit different then it is in pure SQL:
SQL:
SELECT * FROM tableName ORDER BY id DESC LIMIT 1;
DERBY:
SELECT * FROM tablename ORDER BY id DESC FETCH FIRST ROW ONLY;
Have a nice day ;)
Is there some unique key on the table where you could form the question as "give me the secondcol value on the row with the max key value"? If so, there is a technique that will work in any database engine - the idea is to concatenate the key plus any desired result data, perform a min/max, then extract the result data.
See here and here.

How to select a sql table's column value by giving column index?

I have table with 3 columns. One is Id, second column is Name and the third one Description. How can I select the value in the Description field by giving the column index, 3?
Thanks in advance
You can't, from plain SQL (other than in the ORDER BY clause, which won't give you the value but will allow you to sort the result set by it).
If you are using another programming language to construct a dynamic query, you could use that to identify the column being selected by its index number.
Alternatively, you could parameterise your query to return a specific column based on a case statement - like so:
select a, b, c, d, e, ...,
case ?
when 1 then a
when 2 then b
when 3 then c
when 4 then d
when 5 then e
...
end as parameterised_column
from ...
The problem with referring to a column by an index number is that, one day, someone may add a column and break your application as the wrong value will be returned.
This principle is enforced in SQL because you can select named columns, or all columns using the * syntax.
This principle is not enforced in programming languages, where you can usually access the column by ordinal in code, but you should consider the principle before deciding to use a statement such as (psuedo code)
value = results[0].column[2].value;
It should be possible. You'd have to query the system tables (which do vary from one version of SQL to another) to get the 3rd (or Nth) column name as a string to form a following query using that column name.
In SQL 2000 the tables you'll need to start with are syscolumns with a join to sysobjects for the table name. Then the rank() function on "Colid" will give you the Nth column and "name" (shockingly) the name of the column. Once you've got that in a variable the following command can return the value, compare to it, order by it or whatever you need.
This is how you can retrieve a Column's name by passing it's index.
Here variable AcID is used as the index of the column.
Below is the code e.g
dim gFld as string
vSqlText1 = "Select * from RecMast where ID = 1000"
vSql1 = New SqlClient.SqlCommand(vSqlText1, cnnRice)
vRs1 = vSql1.ExecuteReader
if vRs1.Read then
gFld = vRs1.GetName(AcID)
msgbox gfld
end if
declare #searchIndex int
set #searchIndex = 3
select Description from tbl_name t where t.Id = #searchIndex