[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Returning multiple tables from a stored procedure and dataset

hi
i need show tow table in my procedure  
but when get dataset wizard and select procedure  show only first table how to get next result to datagridview  
project in attach
dataset1.png
dataset2.png
dataset3.png
dataset4.png
dataset-test.zip
0
mahmuod
Asked:
mahmuod
  • 3
  • 2
3 Solutions
 
ste5anSenior DeveloperCommented:
This depends on how you access the data in C#.

When you use a SqlDataReader then it's quite simple: Use the NextResult() method. E.g. Retrieving Data Using a DataReader.
0
 
mahmuodAuthor Commented:
i am used Designer
DataSet1.Designer.cs
DataSet1.xsd
how to alter in DataSet1.Designer.cs code.

i cant use  NextResult() method.
0
 
mahmuodAuthor Commented:
Jim Horn- ican not alter after Comment u can alter .
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Mike EghtebasDatabase and Application DeveloperCommented:
Make two stored procedures. One per table.

Mike
0
 
mahmuodAuthor Commented:
eghtebas- i know tow procedures but this just example. because i have other procedures hvae #table and more work
and return more table
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
Mahmuod,

It seems, "you can’t access its second result set. The solution is to render same data as XML and return this XML as output parameter."

You have something like this:
create procedure GetBlogWithDetails(@BlogId int) as 
begin
select BlogId, BlogName 
  from Blog 
  where BlogId = @BlogId

  select PostId, BlogId, PostName 
  from Post 
  where BlogId = @BlogId
end

Open in new window

But, it has to be like this:
create procedure GetBlogWithDetailsXml(
  @blogId int, 
  @xml xml output) as 
begin
  -- first, create 2 table variables for blogs and for posts
  declare @blogRows table(BlogId int, BlogName nvarchar(256))
  declare @postRows table(PostId int, BlogId int, PostName nvarchar(256))

  -- (#1) this is where you implement your useful logic
  insert into @blogRows
  select BlogId, BlogName 
  from Blog 
  where BlogId = @blogId

  insert into @postRows
  select PostId, BlogId, PostName 
  from Post
  where BlogId = @blogId

  -- here you render your 2 tables containing useful data as XML
  declare @blogRowsXml xml = (
    select BlogId, BlogName 
    from @blogRows as Blog
    for xml auto)

  declare @postRowsXml xml = (
    select PostId, BlogId, PostName 
    from @postRows as Post
    for xml auto)

  -- here you build a single XML with all the data required
  set @xml = 
    cast(@blogRowsXml as nvarchar(max)) + 
    cast(@postRowsXml as nvarchar(max))

  -- (#2) here you return the data as "raw" result sets.
  select * from @blogRows
  select * from @postRows
end

Open in new window

There may be other ways to go about. But see if you can make it work using XML which works in T-SQL environment as well. For more see: www.codeproject.com/Articles/620635/Stored-procedures-and-multiple-result-sets-in-T-SQ

Return:
declare @xml xml
exec GetBlogWithDetailsXml 1, @xml output
select @xml

Open in new window

Mike
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now