It works as intended, I just wonder if there is any stylistic things I could improve. It already runs well fast enough, although I'd be open to speed increases.This SQL Server 2008 Scalar Function takes a tracking id from our internal system and finds the corresponding casing type for the products being processed. It distinguishes between where the products currently are in processing and will attempt to find casings for the first unresolved processing step first.Some notes:Variable names are translated from germanunderscore case is used for variable namesHere is the code:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION dbo.GetCasingFromTrackingId (    @tracking_id integer)RETURNS nvarchar(200)ASBEGIN    DECLARE @result nvarchar(200);    DECLARE @intermediate_result nvarchar(200);    DECLARE @first_procstep_id integer;    DECLARE @steps_ort_id integer;    DECLARE @procstep_config_id integer;    DECLARE @current_procstep_id integer;    DECLARE @current_procstep_ort_id integer;    DECLARE @current_procstep_config_id integer;    DECLARE @current_procstep_file_id integer;    DECLARE @loop_counter integer;    DECLARE @step_count integer;    SELECT TOP 1     @first_procstep_id = ar.Eintrag, @steps_ort_id = vg.OrtID, @procstep_config_id = ar.FileID    FROM [CompanyDB].[dbo].[ProcSteps] AS ar     INNER JOIN [CompanyDB].[dbo].[Workflow] AS vg ON ar.VorgangID = vg.Eintrag    WHERE ( vg.OrtID <> 0 ) AND ( vg.Name NOT LIKE '%entgur%' ) AND ( ar.AuftragID = @tracking_id )    ORDER BY ar.Position;    IF ( @first_procstep_id IS NOT NULL ) AND ( @procstep_config_id IS NOT NULL )        BEGIN            IF @steps_ort_id = 1                BEGIN                    SET @intermediate_result =                    (                     SELECT DISTINCT TOP 1                    c.Gehaeuse                    FROM [CompanyDB].[dbo].[Config] AS c                    INNER JOIN [CompanyDB].[dbo].[ProcSteps] AS ar ON ar.Config_ID = c.Eintrag                    INNER JOIN [CompanyDB].[dbo].[ProcTracking] AS lz on ar.AuftragID = lz.Eintrag                    WHERE ( c.Gehaeuse IS NOT NULL ) AND ( lz.Eintrag = @tracking_id )                    ORDER BY c.Gehaeuse                    );                    IF @intermediate_result IS NOT NULL                            SET @result = @intermediate_result;                    ELSE                            SET @result = 'n.a.'                    END            ELSE IF @steps_ort_id = 2                BEGIN                    SET @intermediate_result =                    (                    SELECT TOP 1                    tpl.[Gehuse]                    FROM [CompanyDB].[dbo].[Filelist] AS tpl                    WHERE ( tpl.Eintrag = @procstep_config_id )                    );                    IF @intermediate_result IS NOT NULL                            SET @result = @intermediate_result;                    ELSE                            SET @result = 'n.a.';                END            ELSE IF @steps_ort_id = 3                BEGIN                    SET @intermediate_result =                    (                    SELECT TOP 1                    gu.[Gehuse]                    FROM [CompanyDB].[dbo].[Bands] AS gu                    WHERE gu.Eintrag = @procstep_config_id                    ORDER BY gu.Eintrag                    );                    IF @intermediate_result IS NOT NULL                            SET @result = @intermediate_result;                    ELSE                            SET @result = 'n.a.'                END            ELSE IF @steps_ort_id = 4                BEGIN                    SET @result = 'n.a.';                END            ELSE IF @steps_ort_id = 5                BEGIN                    SET @result = 'n.a.';                END            ELSE IF @steps_ort_id = 7                BEGIN                    SET @result = 'n.a.';                END            ELSE                BEGIN                    SET @loop_counter = 1;                    SET @step_count =                     (                    SELECT                    COUNT(ar.Eintrag)                    FROM [CompanyDB].[dbo].[ProcSteps] AS ar                     INNER JOIN [CompanyDB].[dbo].[Workflow] AS vg ON ar.VorgangID = vg.Eintrag                    WHERE ( vg.OrtID <> 0 ) AND ( vg.Name NOT LIKE '%entgur%' ) AND ( ar.AuftragID = @tracking_id )                    );                    WHILE @loop_counter <= @step_count                        BEGIN                            SET @current_procstep_config_id = NULL;                            SET @current_procstep_file_id = NULL;                            SET @current_procstep_id = NULL;                            SET @current_procstep_ort_id = NULL;                            SELECT @current_procstep_id = lzschritte.Eintrag, @current_procstep_ort_id = lzschritte.Ort, @current_procstep_config_id = lzschritte.Config FROM                                 (                                SELECT                                 ar.Eintrag AS Eintrag,                                vg.OrtID AS Ort,                                ar.Config_ID AS Config,                                ROW_NUMBER() OVER (ORDER BY ar.Eintrag) AS current_row                                FROM [CompanyDB].[dbo].[ProcSteps] AS ar                                INNER JOIN [CompanyDB].[dbo].[Workflow] AS vg ON ar.VorgangID = vg.Eintrag                                WHERE ( vg.OrtID <> 0 ) AND ( vg.Name NOT LIKE '%entgur%' ) AND ( ar.AuftragID = @tracking_id )                                ) AS lzschritte                            WHERE ( lzschritte.current_row = @loop_counter );                            IF @current_procstep_ort_id IS NOT NULL                                BEGIN                                    IF @current_procstep_ort_id = 1                                        BEGIN                                            IF @current_procstep_config_id IS NOT NULL                                                BEGIN                                                    IF ( SELECT c.Geraet2 FROM [CompanyDB].[dbo].[Config] AS c WHERE c.Eintrag = @current_procstep_config_id ) IS NOT NULL                                                        BEGIN                                                            SET @intermediate_result = ( SELECT c.Gehaeuse FROM [CompanyDB].[dbo].[Config] AS c WHERE c.Eintrag = @current_procstep_config_id);                                                            IF @intermediate_result IS NOT NULL                                                                SET @result = @intermediate_result;                                                            ELSE                                                                SET @result = 'n.a.';                                                            BREAK;                                                        END                                                END                                        END                                    ELSE IF @current_procstep_ort_id = 2                                        BEGIN                                            SET @current_procstep_file_id = ( SELECT ar.FileID FROM [CompanyDB].[dbo].[ProcSteps] AS ar WHERE ar.Eintrag = @current_procstep_id );                                            IF @current_procstep_file_id IS NOT NULL                                                    BEGIN                                                    SET @intermediate_result = ( SELECT tpl.[Gehuse] FROM [CompanyDB].[dbo].[Filelist] AS tpl WHERE tpl.Eintrag = @current_procstep_file_id);                                                    IF @intermediate_result IS NOT NULL                                                        SET @result = @intermediate_result;                                                    ELSE                                                        SET @result = 'n.a.';                                                    BREAK;                                                END                                             END                                    ELSE IF @current_procstep_ort_id = 3                                        BEGIN                                            SET @current_procstep_file_id = ( SELECT ar.FileID FROM [CompanyDB].[dbo].[ProcSteps] AS ar WHERE ar.Eintrag = @current_procstep_id );                                            IF @current_procstep_file_id IS NOT NULL                                                    BEGIN                                                    SET @intermediate_result = ( SELECT gu.[Gehuse] FROM [CompanyDB].[dbo].[Bands] AS gu WHERE gu.Eintrag = @current_procstep_file_id);                                                    IF @intermediate_result IS NOT NULL                                                        SET @result = @intermediate_result;                                                    ELSE                                                        SET @result = 'n.a.';                                                    BREAK;                                                END                                        END                                    ELSE IF @current_procstep_ort_id = 4                                        BEGIN                                            SET @result = 'n.a.';                                            BREAK;                                        END                                    ELSE IF @current_procstep_ort_id = 8                                        BEGIN                                            SET @result = 'n.a.';                                            BREAK;                                        END                                END                            SET @loop_counter = @loop_counter + 1;                        END                END        END     ELSE        BEGIN            SET @result = 'n.a.';        END                             RETURN @result;ENDGO
FInd the correct Casing description from process tracking - Scalar Function
sql;sql server;t sql