I know its weird, please dont make me explain or defend it, but I need to update multiple records in a table with a replace and I need to do so a single record at a time. I find the following code works SOMETIMES:
-------------------x-------------------
DECLARE @CNT INT
SET @CNT = 1
WHILE(@CNT < (select max(queryid)+1 from v_CollectionRuleQuery where collectionid = 'HCT009A1'))
BEGIN
update v_CollectionRuleQuery
set QueryExpression = replace(cast(QueryExpression as nvarchar(1000)),'DDT','REMOVED')
where collectionid = 'HCT009A1' and queryID = @CNT
SET @CNT = @CNT + 1
END
-------------------x-------------------
But that works only if the [QUERYID] field I am MAX'ing to set my counter ceiling always starts at 1 and includes sequential values...which I have quickly discovered is not always the case
Very annoying! Can anyone suggest another TSQL strategy to do this? Just in case anyone is confused about the CAST thing, I found that because the [QUERYEXPRESSION] field is a text type, I could not use REPLACE() which is kind of weird, but seems to be by design.
-------------------x-------------------
DECLARE @CNT INT
SET @CNT = 1
WHILE(@CNT < (select max(queryid)+1 from v_CollectionRuleQuery where collectionid = 'HCT009A1'))
BEGIN
update v_CollectionRuleQuery
set QueryExpression = replace(cast(QueryExpression as nvarchar(1000)),'DDT','REMOVED')
where collectionid = 'HCT009A1' and queryID = @CNT
SET @CNT = @CNT + 1
END
-------------------x-------------------
But that works only if the [QUERYID] field I am MAX'ing to set my counter ceiling always starts at 1 and includes sequential values...which I have quickly discovered is not always the case
Very annoying! Can anyone suggest another TSQL strategy to do this? Just in case anyone is confused about the CAST thing, I found that because the [QUERYEXPRESSION] field is a text type, I could not use REPLACE() which is kind of weird, but seems to be by design.