0

I have a certain IDs in a table that I would like to change a column value from false to true. How can I do a loop that goes through the specific IDs given and changes a column value from false to true?

I’m not very familiar with SQL and the only option that came to mind is through a loop.

This is a sample of my data.

enter image description here

Basically we have a stored procedure to change the “description” of the BetGenerationBetID given from FAILED to SUCCESSFUL example, the below will update the description of BetGenerationBetID 265809596 to SUCCESSFUL

enter image description here

With the above I can only update a record each time. So I want something that I can give all BetGenerationBetIDs that I want to update and changes the description of each ID at once.

4
  • How are you deciding which failed rows to change to successful? Does the stored procedure have any logic in it other than an update statement? Could you store the stored procedure? Couldn't it be made to take a criteria (or a set) instead of an individual ID? Commented Apr 10, 2019 at 13:04
  • @AaronBertrand the BetGenerationBetIDs are given to us by another team so we just input the ID and run the stored procedure. The stored procedure updates all relevant tables not just the one provided above. The parameter is BetGenerationBetID INT so I assume only 1 value.
    – chrissult
    Commented Apr 10, 2019 at 13:11
  • 2
    Given to you how? In an e-mail? Carrier pigeon? ...? What is the format of the list? Why aren't they just calling the stored procedure? Commented Apr 10, 2019 at 13:16
  • They raise a ticket with all bet generation IDs that need to update.
    – chrissult
    Commented Apr 10, 2019 at 13:17

1 Answer 1

0

If you have to do it inside of a procedure with a set amount of BetGenerationBetID's, then you could use user defined table types.

Step 1: Create Type and test table

CREATE TYPE dbo.BetGenerationBetIDS AS TABLE  
    ( BetGenerationBetID int);

CREATE TABLE dbo.TableBetGeneration(BetGenerationBetID int, [description] varchar(20));
INSERT INTO dbo.TableBetGeneration(BetGenerationBetId,[description])
VALUES (1,'Failed'),(2,'Failed'),(3,'Failed');

Step 2: Create the Procedure that uses the table type

CREATE PROCEDURE dbo.uspOverrideBetAsSuccesful
@BetGenerationBetIDTable dbo.BetGenerationBetIDS READONLY
AS
    UPDATE TBG
    SET TBG.[description]   = 'SUCCEEDED'
    FROM  dbo.TableBetGeneration TBG 
    INNER JOIN @BetGenerationBetIDTable AS BGID
    ON TBG.BetGenerationBetID  = BGID.BetGenerationBetID

GO

Step 3: Enter your values and run the procedure

DECLARE @BetGenerationTable as dbo.BetGenerationBetIDs
INSERT INTO @BetGenerationTable(BetGenerationBetID)
VALUES(1),(2)

EXEC dbo.uspOverrideBetAsSuccesful @BetGenerationTable

Validate

SELECT * FROM  dbo.TableBetGeneration

Result

BetGenerationBetID  description
1                   SUCCEEDED
2                   SUCCEEDED
3                   Failed
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.