0

I want to store values from a SELECT statement into a variable which is capable of holding more than one value because my SELECT statement returns multiple values of type INT. This is how my SP looks like so far.

ALTER PROCEDURE "ESG"."SP_ADD"
AS
BEGIN

DECLARE @Id table(identifiers VARCHAR);

INSERT INTO @Id (identifiers) VALUES('axaa1aaa-aaaa-a5aa-aaaa-aa8aaaa9aaaa');
INSERT INTO @Id (identifiers) VALUES('bxbb1bbb-bbbb-b5bb-bbb4-bb8bbbb9bbbf');

DECLARE @tranID INT = (SELECT 
DOCUMENT_SET_.DOCUMENT_SET_TRANSACTION_ID 
                            FROM DOCUMENT_SET_TRANSACTION 
                                    WHERE DOCUMENT_SET_TRANSACTION.IDENTIFIER IN (SELECT identifiers FROM @Id));  

END

Variable @tranID should be a list or an array to hold the ids. Is it possible to do it SQL Server?

5
  • 3
    Why don't you declare it as Table? DECLARE @tranID TABLE (IDs INT);, then insert your data, INSERT INTO @tranID SELECT ID FROM .... Commented Nov 7, 2017 at 22:07
  • It complaints for Incorrect syntax near '='. if I declare it as a table Commented Nov 7, 2017 at 22:08
  • Because you INSERT into a table. You don't SET it with = Commented Nov 7, 2017 at 22:09
  • 1
    What are you going to do with @tranID from there? Just return it to the client? If so just do a SELECT and don't "return" a value. Commented Nov 7, 2017 at 22:09
  • Actually once I got the ids I will loop through and I have to do some ore operations. First I need to get the ids Commented Nov 7, 2017 at 22:10

2 Answers 2

6

You can declare a variable of type table

DECLARE @tblTrans TABLE (
    tranID INT
);

INSERT INTO @tblTrans 
SELECT DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID 
FROM ESG.DOCUMENT_SET_TRANSACTION 
WHERE DOCUMENT_SET_TRANSACTION.IDENTIFIER 
    IN (SELECT identifiers FROM @envelopeId);

Depending on what you want to do with the values after this, you could declare a cursor to loop through them or select straight from the variable.

You could also look into using a temporary table depending on what scope you need.

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

3 Comments

Looks like it is working but how can I make sure that @tranId has the values.
@David You may need to declare the size of the INT column if it's saying the data would be truncated. I think the default is 4 places so just increase the number depending on what the size of the column you select from is. To check the values are correct, you can just SELECT * FROM @tblTrans in SSMS at the end of the procedure, it should print the results to the output window.
Awesome. I've increased the size. Thanks sRoyal.
0

Try this, only take the firs row of example. Do u try this?

select DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID,
(STUFF((SELECT '-' + convert(varchar(max),DOCUMENT_SET_TRANSACTION.DOCUMENT_SET_TRANSACTION_ID) 
         FROM  ESG.DOCUMENT_SET_TRANSACTION  
         FOR XML PATH ('')), 1, 2, '')) AS example
FROM ESG.DOCUMENT_SET_TRANSACTION

1 Comment

convert the values for xml and then take everthing into a string concated with "-". that what you want to achieve?

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.