473,671 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get multiple resultsets from multiple tables

1 New Member
Hi all,

I’ve been messing around with the following question for 2 days now, and i still haven’t find a fix. So i hope you can help me.

I’ll quickly show you guys the situation:

There are 2 tables in my database (MainRecords and RecordLines)



What i now want to return is multiple resultsets, 1 resultset for each line in the MainRecords table, like this:



The receiving of multiple resultsets is necessairy! (one resultset is no option).

I’ve written the following script. The script uses 2 for loops, the First to get over every “Main” record, and the second to process al the RecordLines that are referred to the main record.:

Expand|Select|Wrap|Line Numbers
  1.     SET @MAXROWS = 'select max() from UNI452BFUB.MAINRECORDS;';
  2.  
  3.  
  4.  
  5.     SET @ROWCNT = 1;
  6.  
  7.  
  8.  
  9.     FOR_LOOP: FOR EACH_ROW AS C1 CURSOR FOR
  10.  
  11.         select distinct REFERENCENO, COMPANYCODE from UNI452BFUB.MAINRECORDS DO
  12.  
  13.  
  14.  
  15.         FOR_LOOP2: FOR EACH_ROW2 AS C2 CURSOR FOR
  16.  
  17.             SELECT
  18.  
  19.                 UNI452BFUB.RECORDLINES.ID,
  20.                 UNI452BFUB.RECORDLINES.REFERENCENUMBER,
  21.                 UNI452BFUB.RECORDLINES.AMOUNT            
  22.  
  23.             FROM
  24.  
  25.                 UNI452BFUB.RECORDLINES
  26.  
  27.             WHERE
  28.  
  29.                 UNI452BFUB . SROBTR . INREFX = EACH_ROW.BTREFX
  30.  
  31.             DO
  32.  
  33.             INSERT INTO UNI452BFUB.TEMP(ID, REFERENCENUMBER, AMOUNT) VALUES(EACH_ROW2.ID, EACH_ROW2.REFERENCENUMBER, EACH_ROW2.AMOUNT);    
  34.  
  35.         END FOR;
  36.         RETURN(
  37.             SELECT
  38.                            UNI452BFUB.TEMP.ID,
  39.                            UNI452BFUB.TEMP.REFERENCENUMBER,
  40.                            UNI452BFUB.TEMP.AMOUNT            
  41.                       FROM
  42.                            UNI452BFUB.TEMP)
  43.  
  44.     END FOR;
But unfortunately, this totally won’t work. I never get any results, only errors, errors, errors… like the following, i get when i try to create the stored procedure which contains this query:

Expand|Select|Wrap|Line Numbers
  1. SQL State: 42601
  2. Vendor Code: -199
  3. Message: [SQL0199] Keyword END not expected. Valid tokens: UNION EXCEPT INTERSECT. Cause . . . . . :   The keyword END was not expected here.  A syntax error was detected at keyword END.  The partial list of valid tokens is UNION EXCEPT INTERSECT. This list assumes that the statement is correct up to the unexpected keyword.  The error may be earlier in the statement but the syntax of the statement seems to be valid up to this point. Recovery  . . . :   Examine the SQL statement in the area of the specified keyword.  A colon or SQL delimiter may be missing. SQL requires reserved words to be delimited when they are used as a name. Correct the SQL statement and try the request again.
Processing ended because the highlighted statement did not complete successfully

The “END” that is marked is the last end that you can find in the query above.

So, does anyone have an idea why it isn’t working, or does somebody have a much much better solution to this problem?

Thanks in advance!


Andrew
Dec 6 '07 #1
0 1925

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2598
by: Top Gun | last post by:
In order to avoid multiple trips to the database, I would like to fill several tables in a DataSet with a single call to a stored procedure that will return resultsets for the appropriate tables. Can the SqlDataReader be used for this?
2
3555
by: Keith B via SQLMonster.com | last post by:
Hi! I want to return a derived table along with 4 simple tables in a stored procedure as follows: Input parameter: @FtNum (==Order Number, selects one Order and all associated data) Table 1: Orders Table 2: Items
4
2629
by: Amy | last post by:
Hello, I've been struggling to learn C#.NET for a while now. I've made some progress, but I'm easily stumped. :( What's stumping me today is this: I've got a stored procedure (SQL) that returns one row from a table, and up to 3 rows from another table. I want to read the values of the three rows into an array, but I can't figure out how to do it. The code I have compiles, but I suspect it's not quite what I'm looking
3
2222
by: Jon Spivey | last post by:
Hi, using vb.net/sql server 2000 migrating an app from asp to asp.net. I have a stored procedure that returns 3 recordsets. In asp to move to the next recordset I'd just do <% set rs = rs.nextrecordset ' display data set rs = rs.nextrecordset ' display data 'etc
0
1302
by: Crazy Cat | last post by:
Hi, Using Visual Basic 2005 -- I have a stored procedure that returns multiple resultsets. I fill a datareader (SQLDataReader to be exact) with the results from a command object's ExecuteReader method and copy the resultset to the datareader by using a DataTable's Load method. However when I execute NextResultset it comes back false and I know that this stored procedure is returning two resultsets! Can anyone help????
5
25486
by: praveen | last post by:
Hi, I am using nested resultsets to execute queries on two different tables. The code structure is: stmt1 = conn.createStatement(); rs1 = stmt1.executeQuery(query1); while (rs1.next()) { ..... stmt2 = conn.createStatement();
1
2070
by: Robson Siqueira | last post by:
Folks, For designing WinApps, I do prefer to have the controls dragged and dropped into the screen, mainly for datagridview controls. For that end, I normally use the DataSet designer but reading the dataset information from my tables/views/stored procedures. Now I have a case in which I want to use the designer, but with a procedure returning multiple resultsets. I found a lot of articles on the internet, but they say everything but...
4
9125
by: Chris | last post by:
This might be a stupid question.... I have a stored procedures, which uses two selects. When I run the SP I get two resultsets, one very big and the other much smaller, which is the one I want in the dataset. How do a ensure only the last one goes in the dataset.
10
2079
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm using this coding to get 2 resultsets thru datareader and then load them into 2 datatables and bind the datatables to datagridviews. But sdrGrid.NextResult() is returning false for some reason. Is that possible the connection is closed? Dim strConn As String = "Server=localhost;Database=northwind;" + _ "Integrated Security=SSPI" Dim cnnNwind As SqlConnection = New SqlConnection(strConn) Try Dim strSql As String = "select * from...
0
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.