Saturday, February 13, 2010

HOw to use CURSOR in PROCEDURE

set ANSI_NULLS OFF
set QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[PROCNAME]
(
@FeedbackIDStr varchar(5000)/*Selected FeedbackID like 2,4,5,6,7 */
)
AS
Declare @TFid int
Declare @tempTblFid TABLE (idx smallint Primary Key, FeedbackID int)

INSERT INTO @tempTblFid SELECT * from fn_Split(@FeedbackIDStr,',')

/*Declare Cursor for getting FeedbackID and update into tblName*/
Declare curTempFeedback Cursor Fast_Forward for
select FeedbackID from @tempTblFid
Open curTempFeedback
fetch next from curTempFeedback into @TFid
While @@Fetch_Status = 0
BEGIN
UPDATE [tblName]
SET IsAddedToFAQ ='True' where FeedbackID=@TFid

INSERT INTO [tblName]
SELECT Subject,Comment,getdate(),'False',FeedbackID from [tblName] where FeedbackID=@TFid;

fetch next from curTempFeedback into @TFid
END
CLOSE curTempFeedback
DEALLOCATE curTempFeedback

No comments:

Post a Comment