Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Cursor and Update rows based on value/date

$
0
0

SQL Server 2012

Microsoft SQL Server Management Studio11.0.3128.0
Microsoft Analysis Services Client Tools11.0.3128.0
Microsoft Data Access Components (MDAC)6.1.7601.17514
Microsoft MSXML3.0 4.0 5.0 6.0 
Microsoft Internet Explorer9.11.9600.16518
Microsoft .NET Framework4.0.30319.18408
Operating System 6.1.7601

The objective of this is to test the Cursor and use it on a production environment after this is fixed. What I would like to do is update rows in a column i duplicated originally called 'HiredDate' from AdventureWorks2012 HumanResources.Employee table. I made a duplicate column called 'DateToChange' and would like to change it based on a date I have picked, which returns normally 2 results (i.e. date is '04/07/2003'). The code runs but will not change both dates. It did run however with an error but changed only 1 of the 2 rows because it said ['nothing available in next fetch'].

The code to add the columns and perform the query to get the results I am running this against:

-- ADD column 'DateToChange'
ALTER TABLE [HumanResources].[Employee] ADD DateToChange Date NOT NULL;
-- Copy 'HireDate' data to 'DateToChange'
UPDATE HumanResources.Employee SET DateToChange = HireDate;
-- Change 'DateToChange' to NOT NULL
ALTER TABLE [HumanResources].[Employee] ALTER COLUMN DateToChange Date NOT NULL;


	
SELECT BusinessEntityID,HireDate, CONVERT( char(10),[DateToChange],101) AS [Formatted Hire Date] 
	FROM HumanResources.Employee
		WHERE [DateToChange] = '04/07/2003';


Code:

USE AdventureWorks2012;
GO

-- Holds output of the CURSOR
DECLARE @EmployeeID INT
DECLARE @HiredDate DATETIME
DECLARE @HiredModified DATETIME
DECLARE @ChangeDateTo DATETIME


--Declare cursor
-- SCROLL CURSOR ALLOWS "for extra options" to pul multiple records: i.e. PRIOR, ABSOLUTE ##, RELATIVE ##
DECLARE TestCursor CURSOR SCROLL FOR
	-- SELECT statement of what records going to be used by CURSOR 
	-- Assign the query to the cursor.
	SELECT /*HumanResources.Employee.BusinessEntityID, HumanResources.Employee.HireDate,*/ CONVERT( char(10),[DateToChange],101) AS [Formatted Hire Date]
		FROM HumanResources.Employee 
		WHERE DateToChange = '01/01/1901' 
			/*ORDER BY HireDate DESC*/ FOR UPDATE OF [DateToChange];
-- Initiate CURSOR and load records
OPEN TestCursor	
	-- Get first row from query
	FETCH NEXT FROM TestCursor
		INTO @HiredModified 
	-- Logic to tell the Cursor while "@@FETCH_STATUS" 0 the cursor has successfully fetched the next record.
	WHILE (@@FETCH_STATUS = 0 AND @@CURSOR_ROWS = -1)
	BEGIN
		FETCH NEXT FROM TestCursor
			IF (@HiredModified = '04/07/2003')/*05/18/2006*/
			-- Sets @HiredModifiedDate data to use for the change
			SELECT @ChangeDateTo = '01/01/1901'
			UPDATE HumanResources.Employee
				SET [DateToChange] = @ChangeDateTo --'01/01/1901'
				FROM HumanResources.Employee
				WHERE CURRENT OF TestCursor;
	END
-- CLOSE CURSOR
CLOSE TestCursor;
-- Remove any references held by cursor
DEALLOCATE TestCursor;
GO


This query is run successfully but it does not produce the desired results to change the dates04/07/2003 to 01/01/1901.

I would like the query to essentially be able to run the initial select statement, and then update and iterate through the returned results while replacing the necessary column in each row.

I am also open to changes or a different design all together. 

For this query I need:

1. To narrow the initial set of information

2. Check if the information returned, in particular a date, is before [i.e. this current month minus 12 months or12 months before current month]

3. Next replace the dates with the needed date

[Haven't written this out yet but it will need to be done]

4. After all this is done I will then need to update a column on each row:

if the 'date' is within 12 months to 12 months from the date checked

NOTE: I am new to TSQL and have only been doing this for a few days, but I will understand or read up on what is explained if given enough information. Thank you in advance for anyone who may be able to help.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>