SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cusor - weird results
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Chris_Kelley
Posting Yak Master

102 Posts

Posted - 08/09/2014 :  10:48:49  Show Profile  Reply with Quote
Hi everyone, I am getting a duplicate when using a cursor

CREATE TABLE #TEST
(
BIRTHDAY DATE,
FIRSTNAME VARCHAR(255),
LASTNAME VARCHAR(255)
)
----------------------------------------------------
INSERT INTO #TEST
SELECT A.BIRTHDAY, P.FIRSTNAME, P.LASTNAME
FROM Age AS A
INNER JOIN PERSONS AS P ON A.ID = P.PersonID
----------------------------------------------------
DECLARE @FIRSTNAME VARCHAR(255),
@LASTNAME VARCHAR(255),
@BIRTHDAY VARCHAR(20)
----------------------------------------------------
DECLARE TEST_CURSOR CURSOR FAST_FORWARD
FOR
SELECT FIRSTNAME,LASTNAME,BIRTHDAY FROM #TEST ORDER BY BIRTHDAY ASC
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @FIRSTNAME,@LASTNAME,@BIRTHDAY
PRINT @FIRSTNAME + ' ' + @LASTNAME + ' ' + @BIRTHDAY
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM TEST_CURSOR
INTO @FIRSTNAME,@LASTNAME,@BIRTHDAY
PRINT @FIRSTNAME + ' ' + @LASTNAME + ' ' + @BIRTHDAY
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
DROP TABLE #TEST

--------------------------------------------------------------------
results --
------------------------------
Christopher Kelley 1981-09-21
Angelito Rodriguez 2006-11-18
Izaiah Rodriguez 2009-09-12
Izaiah Rodriguez 2009-09-12
------------------------------
---------------------------------------------------------------------

select * from #test (commenting out drop)results --
-----------------------------------------
BIRTHDAY FIRSTNAME LASTNAME
1981-09-21 Christopher Kelley
2009-09-12 Izaiah Rodriguez
2006-11-18 Angelito Rodriguez
NULL teser NULL
-----------------------------------------

The IDE I'm using is visual studio 2013


Any thoughts why the cursor is duplicating my son in the results?

Thanks,
Chris
Jr Programmer

namman
Constraint Violating Yak Guru

USA
272 Posts

Posted - 08/09/2014 :  11:34:28  Show Profile  Reply with Quote
DECLARE TEST_CURSOR CURSOR FAST_FORWARD
FOR
SELECT FIRSTNAME,LASTNAME,BIRTHDAY FROM #TEST ORDER BY BIRTHDAY ASC
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @FIRSTNAME,@LASTNAME,@BIRTHDAY
PRINT @FIRSTNAME + ' ' + @LASTNAME + ' ' + @BIRTHDAY
WHILE @@FETCH_STATUS = 0
BEGIN
-- code should be here
PRINT @FIRSTNAME + ' ' + @LASTNAME + ' ' + @BIRTHDAY
-- ------
FETCH NEXT FROM TEST_CURSOR
INTO @FIRSTNAME,@LASTNAME,@BIRTHDAY
PRINT @FIRSTNAME + ' ' + @LASTNAME + ' ' + @BIRTHDAY
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR

Edited by - namman on 08/09/2014 11:35:27
Go to Top of Page

Chris_Kelley
Posting Yak Master

102 Posts

Posted - 08/09/2014 :  12:36:27  Show Profile  Reply with Quote
Awesome, ty ty, worked perfect

Thanks,
Chris
Jr Programmer
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000