May 19, 2024

Srikaanth

Update column today date minus one and in loop decrees that date again

 Update column today's date -1 and in loop decrees that date again -1


If you need to update each column with a different date value, where each date is derived from today's date minus a different number of days, you can do so by calculating the dates before entering the loop. Here's how you can achieve this:

```sql server query
DECLARE @Date1 DATE
DECLARE @Date2 DATE
DECLARE @Date3 DATE

SET @Date1 = DATEADD(DAY, -1, GETDATE())
SET @Date2 = DATEADD(DAY, -2, GETDATE())
SET @Date3 = DATEADD(DAY, -3, GETDATE())

DECLARE @ID INT

DECLARE cursor_name CURSOR FOR
SELECT ID
FROM YourTableName
-- Add any condition here if needed

OPEN cursor_name

FETCH NEXT FROM cursor_name INTO @ID

WHILE @@FETCH_STATUS = 0
BEGIN
    UPDATE YourTableName
    SET Column1 = @Date1,
        Column2 = @Date2,
        Column3 = @Date3
    WHERE ID = @ID

    FETCH NEXT FROM cursor_name INTO @ID
END

CLOSE cursor_name
DEALLOCATE cursor_name
```

In this example, `@Date1`, `@Date2`, and `@Date3` are calculated using `DATEADD` function with different values for subtracting days from today's date. Adjust these calculations according to your specific requirements. The cursor loops through each record fetched, and for each record, it updates the columns with the corresponding date values.
Subscribe to get more Posts :