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

x
?
Solved

array output from stored proc?

Posted on 2003-11-10
14
Medium Priority
?
914 Views
Last Modified: 2008-01-16
How do I return an array from a stored procedure?
A short example?

Thanks.
0
Comment
Question by:samliam
14 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 9720681
There are no arrays in stored procedures. You do have recordsets.

What kind of information do you need to return?
0
 

Author Comment

by:samliam
ID: 9720819
I want to return one column of a table.
0
 
LVL 6

Expert Comment

by:lausz
ID: 9721844
From this link :

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/itcommunity/chats/trans/sql/sql0724.asp

Host Guest_ken_ms
Q: Pancha - How can I use arrays as Input parameter in a Store procedure?

A: You can't - there are no array types in T-SQL. You can simulate arrays using delimited strings or XML strings (or the UDFs/xprocs in my last book ;-) but there's no built-in way to do it.



Host Guest_ken_ms
Q: tc : in sql server 7.0, what is the best way to get around not having an array datatype, if I need to pass an array into a sproc

A: You can use a delimited string and shred the string into 'elements' or you can use the xprocs in my last book :-) The best strategy is probably not to do this unless there's no other way.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 29

Expert Comment

by:leonstryker
ID: 9722066
Depending on what you plan to do with the data once you retrieved it, you may may keep it in a recordset, which is an array of fields and rows, in your code; or if you are still in SQL you may keep the results in a temp table for further manipulation.

What are you trying to accomplish with your code?

Leon
0
 

Author Comment

by:samliam
ID: 9723624
I want to put the column in a dropdown list.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9723638
Please maintian your many old open questions:

1 04/04/2001 50 monitoring system resources  Open Windows Programming
2 02/03/2002 100 read pdf from IE6  Open Windows NT
3 10/13/2003 50 automatically generated statement  Open C#
4 10/13/2003 50 the Duwamish online project  Open C#
5 10/20/2003 100 webform creation at runtime?  Open C#
6 09/25/2001 50 network cards  Open Windows NT
7 10/07/2003 125 IE6 runtime error  Open JavaScript
8 10/07/2003 20 IE6 debug?  Open Browsers Issues

Thanks,
Anthony
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9724182
Assuming you are using VB, you would need to loop through the recordset while adding the items, something like this:

rsData.MoveFirst
While Not rsData.EOF
    lstBox.AddItem rsData(0)
    rsData.MoveNext
Loop
0
 

Author Comment

by:samliam
ID: 9724673
I use SqlCommand to execute a stored proc.
I use a SqlDataAdapter to 'Fill' a dataset. SqlDataAdapter contains a selectCommand. What should the selectCommand be, now that I am calling a stored proc?

If I can get the column from the stored proc into a dataset, how do I read the dataset? what is the table name? (e.g., ds.Tables["?
"])

SqlCommand takes a connection. SqlDataAdapter also takes a connection. What happens with 2 connections?
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 200 total points
ID: 9724833
samliam

You are using VB.NET and ADO.NET. Since you are only trying to populate a list box your best bet is DataReader object.  Look at the following links on how to do that.  

ADO.NET tutorial --> http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/adoplusoverview.aspx
VB.NET sample ----> http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/adoplus/adooverview3/adooverview3.src

Instead of:

      do while (myReader.Read())
        Console.Write(myReader("CustomerID").ToString() + "    ")
        Console.WriteLine(myReader("CompanyName").ToString())
      loop

in the sample code you will need to do something like this:

      do while (myReader.Read())
         lstBox.AddItem(myReader(0)ToString() + "")
      loop

Leon



0
 

Author Comment

by:samliam
ID: 9724837
Reason I use SqlCommand to call the proc is that I need to pass in a paramete.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9724870
Look at the VB.NET sample link above.
0
 

Author Comment

by:samliam
ID: 9725038
That works! Thanks.
What if I want to use SqlDataAdapter?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9725198
0
 

Author Comment

by:samliam
ID: 9725487
I figure that one out. I still feel that SqlDataAdapter is a strange class. I had to use Tables[0] and Rows[i][0] since I don't know what the table name should be..

Thanks for the help. I am crediting the points now.

Would you also be able to answer my other question regarding how to make the table name in a stored proc a parameter to be passed in? Here is the link to the question:

http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_20793984.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question