Stored procedure with limited number of rows - sql

I am trying to build a stored procedure where I am able to extract data in bit size piece.
For example, if I have 1000 rows of data, I need to get it in batches of maybe first 10 or 100 and in the next run the next 10 or 100.
SELECT TOP 100 will only keep giving the first 100 rows but will ignore anything else.
Thanks,
Vicky

Related

Increase the number of rows in sql

I am learning SQL. I have a Select-From command and the output result is limited to 25 rows. I would like to increase the number of rows to say 50 or 75 rows displayed. What syntax do I include in my Select - From code to make this possible?
I tried typing 50 in front of the Select * Command.
If you mean limiting query result, depending on your database you can use some keywords.
For example, for SQL SERVER you can get only first 50 rows via the command below:
select top 50 * from yourTable;
But if you are limiting your output display via an editor/IDE, you need to check manual of it.

Using SQL Server query desired table output for the given table

I have a following table in SQL Server 2008 and it needs to be shown in a particular format. I was thinking of what approach should I follow to get desired output. Whether cursor needs to be used or not or multiple while loops to traverse each rows. This table has multiple accounts and dt_id is unique.
Kindly find the table below:
Desired output:
The problem statements is for eg in first table for first non zero dt_debit amount is 100 and I need to check whether this value cancels out in dt_credit column on FIFO method.
So in this case in first loop 100 gets compared to 500. So here it's less than
500 therefore it will go in the 2nd table.
Then it will go to 2nd value that is 400 and it will compare previous remaining balance that is 500 - 100 = 400 so again since its not greater again entry will go in desired output table.
Now for value 50 it will compare with 300 and 250 balance will be left. Next value is 300 therefore for 300 values will go twice as it will come in 250 + it will also take 50 from 80 dt_credit.
Can this logic be done in SQL Server. I am using SQL Server 2008. Any help would be appreciated.

SQL Divide or group fixed lengthed entries together

i have the following SQL Problem, given a table with a specific column e.g tableX:
col1
123
321
456
321
982
666
100
...
the amount of rows in this table can vary (even be 0).
What i need is to divide the rows into a coma separate text.
For example, I could put all values into one string
using this
SET #LIST= (SELECT ',''' + col1+'''' FROM Table FOR XML PATH(''))
('123','321','456','321',...) -- < can be too long
but the Problem is, that #LIST gets too long, therefore I want to divide it based on the number of entries into multiple (sub)lists. For example a fixed size (e.g Maximum 3 Elements) would always look at three Elements until nothing is left.
I was thinking of using some kindof Loop
// some Kind of Loop
('123,321,456')
// end some Kind of loop
and in the next Loop
('321,982,66')
and finally (if less than 3 are remaining) only
('100')
how can i achieve this?
edit: the database is a MSSQL db. if necessary I could sort the entries but they also contain characters (not only numerical). in fact the order doesnt matter.

MDX limit usage

Lets say after a basic query in MDX my output was 100 rows. If I want to limit to top 10 rows, I can use topcount. But, this usage of Topcount will fetch entire 100 rows from database and then pick the top 10 from it.
I wonder if there is any other possible way to fetch top ten rows directly from database?

sql query to fetch only those records where sum(colNm)<xyz and store first and last records rowid/pk

I have a table with millions of records which holds information about a user, his or her documents in a BLOB, and a column holding the file size per row.
While reporting I need to extract all these records along with their attachments and store them in a folder. However, the constraint is that the folder size should not exceed 4GB.
What I need, is to fetch records only till that record, where the summation of file sizes is less than 4GB. I have hardly any experience in databases, and do not have any DB expert to refer.
for eg - say i need to fetch only records till sum(fileSize) < 9
Name fileSize
A 1
B 2
C 3
D 2
E 9
F 4
My query needs to return records A,B,C and D.
Also, i need to store the rowID/uniqueID of the first and last record for another subsequent process.
The DB being used is IBM DB2.
Thanks!
So here is some trick how you can find your file size. and in procedure you can manage with data.
select length(file_data) from files
where length(file_data)<99999999;
LENGTH(FILE_DATA)
82944
82944
91136
3 rows selected.
select dbms_lob.getlength(file_data) from files
where length(file_data)<89999;
DBMS_LOB.GETLENGTH(FILE_DATA)
82944
82944
2 rows selected.
dbms_lob.getlength() vs. length() to find blob size in oracle
hope this helps....