Solved

array output from stored proc?

Posted on 2003-11-10
14
894 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 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