0
DECLARE
    @vLiczba int = 0,
    @vWynik varchar (30),
    @min int;

BEGIN
    WHILE @vLiczba < 1
    BEGIN
        SET @min = (SELECT MIN(RIGHT(LEFT(query_transaction, 36), 4))
                    FROM testowa_tabela1 
                    WHERE query_transaction LIKE 'update%');
        PRINT 'min value: ' + CAST (@min as varchar(10)) ;
        
        UPDATE testowa_tabela1
        SET query_transaction = REPLACE(query_transaction, '@min', '9999') 

        SET @vLiczba = @vLiczba + 1;
    END;

    PRINT 'Verified record: ' + CAST (@vLiczba as varchar(10)) + ', min value: ' + CAST (@min as varchar(10)) ;
END;

In row 14 in REPLACE(query_transaction, '@min', '9999'), @min is not treated as variable.

Can you help me fix my problem? REPLACE statement is executed in the loop as I checked but without result.

1 Answer 1

2

You just need to get rid of the single quotes around the @min. When you use single quotes, The variable is treated as a string, not as a variable. Also, you need a semi-colon after your updated statement. So your updated code would be -

DECLARE
    @vLiczba int = 0,
    @vWynik varchar (30),
    @min int;
BEGIN
    WHILE @vLiczba < 1
    BEGIN
       SET @min = (select min(right(left(query_transaction,36),4))
                     from testowa_tabela1
                    where query_transaction like 'update%');
     PRINT 'min value: ' + CAST (@min as varchar(10));
        
    UPDATE testowa_tabela1
       SET query_transaction = REPLACE( query_transaction,@min,'9999');
       SET @vLiczba = @vLiczba + 1;
    END;
    PRINT 'Verified record: ' + CAST (@vLiczba as varchar(10)) + ', min value: ' + CAST (@min as varchar(10)) ;
END;
Sign up to request clarification or add additional context in comments.

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.