4

I need to return an array from stored procedure. And at client side,finally need to retrieve the data by using c# and ado.net.

CREATE PROCEDURE [dbo].[usp_testing]

AS
BEGIN

DECLARE @ARRAY TABLE(sno int,id nvarchar(50)

INSERT @ARRAY(sno,id) values(1,'v123')
INSERT @ARRAY(sno,id) values(2,'v124')
INSERT @ARRAY(sno,id) values(3,'v124')
END

This @Array table need to be returned and to be retrieved to the client side

4
  • And what have you tried? What specific difficulties did you encounter? Commented Aug 31, 2012 at 9:52
  • you can use XML as a output parameter or use table value function in this situation. Commented Aug 31, 2012 at 9:56
  • i am trying to return this array as return @array but its not doing Commented Aug 31, 2012 at 9:57
  • @Vids you can only return an integer expression; Commented Aug 31, 2012 at 10:01

2 Answers 2

7

@ARRAY is not an array: it is a table. The mechanism for obtaining a table is simple and well-practiced:

select * from @ARRAY

To get that at the client, use either ExecuteReader or something like a DataTable if it makes you happy. Or something like dapper:

var arr = connection.Query(
    "usp_testing", commandType: CommandType.StoredProcedure)
    .Select(row => Tuple.Create((int)row.sno, (string)row.id)).ToArray();

which is an array of tuples.

Sign up to request clarification or add additional context in comments.

Comments

2
create procedure strange_procedure as
begin

DECLARE @ARRAY TABLE(id int not null identity primary key, value varchar(50))

insert into @ARRAY (value) values ('This value')

select  *  from @ARRAY


end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.