How to Use a Cursor in SQL

by ssi 11. February 2016 09:32

USE [LENDER_LOAN_SERVICE]
GO

/****** Object:  UserDefinedFunction [dbo].[uf_ssi_GetHMDARaceString]    Script Date: 02/11/2016 09:30:40 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE  FUNCTION [dbo].[uf_ssi_GetHMDARaceString]( @LoanID int , @BRW varchar(50))
RETURNS nchar(5)
AS
BEGIN
DECLARE @Event_ID varchar(100)
DECLARE  @Return varchar(500);
 
--set @LoanID ='1344'
DECLARE my_cursor CURSOR FOR
SELECT [_Type] FROM  [HMDA_RACE] where  [loanGeneral_Id] =@LoanID and [BorrowerID] = @BRW
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO @Event_ID
set  @Return =  ''
--print @Event_ID
WHILE @@FETCH_STATUS = 0
BEGIN
     if @Event_ID = 'AmericanIndianOrAlaskaNative'    set  @Return =   @Return+ '1'
     if @Event_ID =  'Asian'  set  @Return =   @Return+ '2'
     if @Event_ID = 'BlackOrAfricanAmerican' set  @Return =   @Return+ '3'
     if @Event_ID = 'NativeHawaiianOrOtherPacificIslander' set  @Return =   @Return+'4'
     if @Event_ID = 'White' set  @Return =   @Return+'5'
     --else set  @Return =   @Return + '-'
    --print @Event_ID
    --print @Return
    FETCH NEXT FROM my_cursor
    INTO @Event_ID
END
CLOSE my_cursor
DEALLOCATE my_cursor
 if (SELECT [RaceNationalOriginRefusalIndicator] FROM  [GOVERNMENT_MONITORING] where [loanGeneral_Id] =@LoanID and [BorrowerID] = @BRW)=1 set @Return='6'
RETURN substring(@Return+'     ',1,5)

END;


GO


Tags: ,

SQL

Add comment

biuquote
  • Comment
  • Preview
Loading

Calendar

<<  May 2026  >>
MoTuWeThFrSaSu
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"Fill the unforgiving minute with sixty seconds worth of distance run."
Rudyard Kipling