2

I have a stored procedure that should return an array of ids, depending on the result of a SELECT query. How can I do this in SQL Server?

Here is my stored procedure:

ALTER PROCEDURE [dbo].[PS_testGar] 
    @CODE_B,
    @ids OUTPUT
AS 
BEGIN  
   SELECT @ids = code_gar
   FROM  [dbo].[GARANTIE]
   WHERE CODE_B= @CODE_B
END 
7
  • remove the "@ids = ". SQL Server will natively return the list of IDs. What language are you going to call this from?
    – Jeremy
    Commented Oct 2, 2017 at 15:41
  • Do you really need an output parameter? If so you will need a user defined table type. The simpler approach is to simply have the select statement in your procedure and have that be the resultset.
    – Sean Lange
    Commented Oct 2, 2017 at 15:41
  • @Jeremy i'll call it from a WCF service. i removed the"@ids =" , how should i declare the output param ?
    – KhadBr
    Commented Oct 2, 2017 at 15:48
  • 1
    I wouldn't use an output parameter for this. It is more complicated than it needs to be.
    – Sean Lange
    Commented Oct 2, 2017 at 15:50
  • 1
    read the documentation link I sent you. There are many .NET technologies for reading data from sql server.
    – Jeremy
    Commented Oct 2, 2017 at 15:55

1 Answer 1

2

No need for an output parameter here. This is what your procedure should look like.

ALTER PROCEDURE [dbo].[PS_testGar] 
(
    @CODE_B int --or whatever your datatype is
) AS  
BEGIN  
    SET NOCOUNT ON;

    SELECT code_gar
    FROM  [dbo].[GARANTIE]
    WHERE CODE_B = @CODE_B
END 
2
  • How to handle that resultset from my wcf service that is calling this SP ?
    – KhadBr
    Commented Oct 2, 2017 at 15:55
  • You would use ExecuteReader. Look at the link Jeremy posted in the comments above. It provides several examples.
    – Sean Lange
    Commented Oct 2, 2017 at 15:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.