Wednesday, January 5, 2011

Cursor Loop example in MSSQL server,

Cursor Example In SQl server 2008,

USE [skbbl]
GO
/****** Object: UserDefinedFunction [dbo].[get_account_holder]
Script Date: 01/05/2011 16:01:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[get_account_holder](@acc_no nvarchar(50))
RETURNS nvarchar(2500)
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @Holder_Name nvarchar(255)
DECLARE @Ret_Holder_Names nvarchar(255)

DECLARE Cursor_Acc_Holder CURSOR READ_ONLY

FOR
SELECT m.full_name FROM mem_general_mcg m join sav_acc_holder_mcg sa
ON m.mid=sa.mid and sa.acc_no=@acc_no
--SELECT au_id FROM authors
OPEN Cursor_Acc_Holder
SET @Ret_Holder_Names=''

FETCH NEXT FROM Cursor_Acc_Holder INTO @Holder_Name
WHILE @@FETCH_STATUS = 0
BEGIN

--PRINT @Holder_Name
SET @Ret_Holder_Names=@Ret_Holder_Names+@Holder_Name+','
FETCH NEXT FROM Cursor_Acc_Holder
INTO @Holder_Name

END

CLOSE Cursor_Acc_Holder
DEALLOCATE Cursor_Acc_Holder
RETURN @Ret_Holder_Names
END

No comments:

Post a Comment