limiting results in sybase ASE between a particular range - sybase

I have a search screen which allows the user to search information and it populates a grid. Some of the search results are returning a huge amount of data. I am trying to create a paging grid so that I only bring from the store procedure 10 or 20 results at a time. (I already have a paging grid in the UI)
I am trying to do something like this:
select * from wl_eval limit 1, 20
The query above will return only the first 20 records. How would I be able to accomplish that in Sybase ASE? Unfortunately, for my client project we are using Sybase. I know that in other database engines we could have used the query I mention above. I also know we can use SET ROW COUNT 20 but this wont work if I want a particular range, say from 30 to 50.
Any thoughts?

Add TOP numberofrecords you want to return
DECLARE #intStartRow int;
DECLARE #intEndRow int;
SET #intStartRow = (#intPage -1) * #intPageSize + 1;
SET #intEndRow = #intPage * #intPageSize;
WITH wl_eval AS
(SELECT field,
ROW_NUMBER() OVER(ORDER BY intID DESC) as intRow,
COUNT(intID) OVER() AS intTotalHits
FROM tblBlog)
SELECT field, intTotalHits FROM wl
WHERE intRow BETWEEN #intStartRow AND #intEndRow

SELECT TOP 20 <column list>
FROM YourTable
ORDER BY <list>

Related

How to generate an excel file (.xlsx) from SQL Server

I have this query:
WITH InfoNeg AS
(
SELECT DISTINCT
n.idcliente,
CASE
WHEN DATEDIFF(MONTH, MAX(n.fechanegociacion), GETDATE()) <= 2
THEN 'Negociado 6 meses'
ELSE NULL
END AS TipoNeg
FROM
SAB2NewExports.dbo.negociaciones AS n
WHERE
Aprobacion = 'Si'
AND cerrado = 'Si'
GROUP BY
n.idcliente
), Multi AS
(
SELECT DISTINCT
idcliente, COUNT(distinct idportafolio) AS NumPorts
FROM
orangerefi.wfm.wf_master_HIST
WHERE
YEAR(Fecha_BKP) = 2021
AND MONTH(Fecha_BKP) = 08
GROUP BY
idcliente
)
SELECT DISTINCT
m.IdCliente, c.Nombre1
FROM
orangerefi.wfm.wf_master_HIST as m
LEFT JOIN
InfoNeg ON m.idcliente = InfoNeg.idcliente
LEFT JOIN
Multi ON m.IdCliente = Multi.idcliente
LEFT JOIN
SAB2NewExports.dbo.Clientes AS c ON m.IdCliente = c.IdCliente
WHERE
CanalTrabajo = 'Callcenter - Outbound' -- Cambiar aca
AND YEAR (Fecha_BKP) = 2021
AND MONTH(Fecha_BKP) = 08
AND GrupoTrabajo IN ('Alto') -- Cambiar aca
AND Bucket IN (1, 2) -- Cambiar aca
AND Multi.NumPorts > 1
AND Infoneg.TipoNeg IS NULL
When I run it, I get 30 thousand rows and the columns that I get when performing the query are: ClientID, name. I would like it to be saved in an Excel file when I run it, I don't know if it's possible.
Another question, is it possible to create a variable that stores text?
I used CONCAT(), but the text being so long is a bit cumbersome, I don't know if there is any alternative.
If you can help me, I appreciate it.
To declare a variable
DECLARE #string VARCHAR(MAX)
SET #string = concat()
then insert whatever you are concatenating
Here is an answer given by carusyte
Export SQL query data to Excel
I don't know if this is what you're looking for, but you can export the results to Excel like this:
In the results pane, click the top-left cell to highlight all the records, and then right-click the top-left cell and click "Save Results As". One of the export options is CSV.
You might give this a shot too:
INSERT INTO OPENROWSET
('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=c:\Test.xls;','SELECT productid, price FROM dbo.product')
Lastly, you can look into using SSIS (replaced DTS) for data exports. Here is a link to a tutorial:
http://www.accelebrate.com/sql_training/ssis_2008_tutorial.htm
== Update #1 ==
To save the result as CSV file with column headers, one can follow the steps shown below:
Go to Tools->Options
Query Results->SQL Server->Results to Grid
Check “Include column headers when copying or saving results”
Click OK.
Note that the new settings won’t affect any existing Query tabs — you’ll need to open new ones and/or restart SSMS.

How to limit the result data in flexible seach query

I want to limit the result data in flexible search query.
Let's say query should return only 10 records each time (like LIMIT)
How can I do this?
You already answered your query, you can use LIMIT cause same as we use in MySQL.
Try this
SELECT * FROM {Product} LIMIT 10
or
SELECT TOP 10 * FROM {Product}
For Oracle
SELECT * FROM {Product} WHERE rownum <= 10
Though API
final FlexibleSearchQuery query = new FlexibleSearchQuery("SELECT * FROM {Product}");
query.setCount(10);
Find more detail in helphybris
Use the query.setCount(int) method of the API.

Codename One Database print range of rows

I am using Codename One to get data from a database and display it in a table.
This is functioning quite well, and now I am trying to add a feature that limits the number of items that are shown in the table. To get there, I want to retrieve a selected number of columns from the database .
Unfortunately, common java sql operations do not seem to work. Below is what I have so far. All approaches don't throw any errors, but also also don't display the desired range or ranges. Another way to obtain a selection would probably be an ArrayList, but I would very much like to stick with a direct approach, since this appears to be relatively easy and elegant solution.
db = display.getInstance().openOrCreate("MyDB.db");
db.execute("CREATE TABLE IF NOT EXISTS Termine (Date NOT NULL,Event NOT NULL, Date_String NOT NULL)");
// prints the entire number of items from the database
cur = db.executeQuery("select * from (select * from Termine order by Date) where Date >= 5;");
// doesn't print anything
cur = db.executeQuery("SELECT * from ( select m.*, Date r from Termine m ) where r > 4 and r < 10;");
// doesn't print anything
cur = db.executeQuery("SELECT * from ( select m.*, Date r from Termine m ) where r BETWEEN 5 AND 10;");
while (cur.next()) {
Row currentRow = cur.getRow();
String event = currentRow.getString(1);
System.out.println(event);
}
Is my approach feasible? Any help would be greatly appreciated.
You can use the LIMIT SQL keyword as explained here.
SELECT column_list FROM table LIMIT row_count OFFSET offset;
A good practice is to search for "sqlite" and the SQL feature you are looking for as mobile SQL databases on all platforms are based on sqlite which has some quirks.

SQL Query... I'm new so this is probably easy

I can write simple SELECT statements for my SSRS reports but I have run into a wall trying to figure out how to do this query and I'm stumped. I have a table, and it has an entry in it showing me that a particular process is done. The process is Operation_Seq_NO 60 and the QTY_GOOD is 1. There IS NO ENTRY for operation_seq_no with an 80 so it goes on the report. As soon as an 80 entry hits the table, it needs to go off the report. Sounds simple but totally got me stumped. I attached a pic or it in tabular format to maybe help someone understand the issue.
You can use not exists() or not in() to filter rows that have a corresponding row with operation_sec_no = 80 like so:
using not exists():
select *
from labor_ticket as t
where not exists (
select 1
from labor_ticket as as i
where t.transaction_id = i.transaction_id
and i.operation_sec_no = 80
)
or with not in():
select *
from labor_ticket as t
where transaction_id not in (
select transaction_id
from labor_ticket as i
where i.operation_sec_no = 80
)

Report builder - using different queries according to parameter

I am trying to make a report, where I can select a multiple values from a parameter - and then make a query, where i state "where in (#partner)"
However, my problems occours, when I want to make a value, that will not do the "where in (#partner)", but will simply select all from the same query, without the where in clause - as not all rows in my db table, have the specific values set, so I want to catch them too.
All the values for the parameter is selected from a query, and I have a union on it, to create a value for the one, where I need to select all - not using the where in.
It actually works, if only one value from the parameter list is chosen - but as soon as I try to select 2 or more, I get the error "query execution failed for dataset1......"
The value of the parameter value, that is used for selecting all is 0, as you see in the code - and it does work as I can execute both select statements below, but not when I select more then 1 value from the parameter list.
IF #partner = 0
BEGIN
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
END
ELSE
SELECT
thedata
FROM
thetable
WHERE
thedata = 3
and partner in (#partner)
END
ELSE
Hope there is some clever guys out there:) Thanks
Just do:
SELECT thedata
FROM thetable
WHERE thedata = 3
AND (partner IN (#partner) or #partner = 0)

Resources