简体   繁体   中英

Is there a better way to write this sql code? [on hold]

Coworkers put nasty comments in my code. Is there a better way to write this stored procedure?

This is how I write SQL. I've been doing it for years. I'm not a newb.

Are they just being mean? I think I write better SQL than them.

This stored procedure builds wheres depending on what the classic asp sends as parameters.

I put it all together using a thing called dynamic sql.


-- ==================================================================
-- Author:      SQL Master Daniel
-- Create date: <Create Date,,>
-- Description: Holy hell!  I suppose this is one way to write SQL.
-- ===================================================================
ALTER PROCEDURE [mstr08].[usp_mstr08_admlist]
                @CoverageDB nvarchar(100), --Database Name
                @Type int, --Search Type
                @Value nvarchar(25), --Search Criteria
                @Value1 nvarchar(19), --Date 1
                @Value2 nvarchar(19), --Date 2
                @IsISPO bit
AS
BEGIN
DECLARE @FinSQL nvarchar(4000)
DECLARE @Fields nvarchar(3000)
DECLARE @Where nvarchar(280)
DECLARE @fkLogin nvarchar(40)
DECLARE @VType nvarchar(200)
DECLARE @TimeStamp nvarchar(200)
DECLARE @TimeStampOE nvarchar(200)
DECLARE @FName nvarchar(200)
DECLARE @LName nvarchar(200)
DECLARE @Email nvarchar(200)
DECLARE @Start nvarchar(200)
DECLARE @End nvarchar(200)
DECLARE @Mod nvarchar(200)
DECLARE @Dept nvarchar(200)
DECLARE @OE nvarchar(200)
DECLARE @medPlan nvarchar(200)
DECLARE @intID nvarchar(200)
DECLARE @ParamDefinition AS NVarchar(2000)
DECLARE @intA AS nvarchar(200)
DECLARE @intB AS nvarchar(200)
DECLARE @intD AS nvarchar(200)
DECLARE @intForm AS nvarchar(220)
DECLARE @intFormOE AS nvarchar(200)
DECLARE @ISPO NVarChar(200)

If @Type = 0 --Today's Date
BEGIN
    SET @Where = ' WHERE CONVERT(VARCHAR(19),timestamp,101) = CONVERT(VARCHAR(19),GETDATE(),101) AND intFormType <> 3 and ISPO = '+ cast(@IsISPO as nvarchar) +' GROUP BY fkLogin ORDER BY [Time Stamp]'
END
ELSE IF @Type = 1 --GPA ID
BEGIN
    SET @Where = ' WHERE fkLogin = ' + @Value + ' AND intFormType <> 3 and ISPO = '+ cast(@IsISPO as nvarchar) +' GROUP BY fkLogin'
END
ELSE IF @Type = 2 --Last Name
BEGIN
    SET @Where = ' WHERE Last_name LIKE ''' + @Value + '%'' AND intFormType <> 3 AND fklogin is not null and ISPO = '+ cast(@IsISPO as nvarchar) +' GROUP BY fkLogin ORDER BY MAX(Last_name) ASC'
END
ELSE IF @Type = 3 --first Name
BEGIN
    SET @Where = ' WHERE First_name LIKE ''' + @Value + '%'' AND intFormType <> 3 AND fklogin is not null and ISPO = '+ cast(@IsISPO as nvarchar) +' GROUP BY fkLogin ORDER BY MAX(First_name) ASC'
END
ELSE IF @Type = 4 --Date Range
BEGIN
    SET @Where = ' WHERE CONVERT(VARCHAR(19),timestamp,101) >= CONVERT(DATETIME,''' + @Value1 + ''',101) and ISPO = '+ cast(@IsISPO as nvarchar) +' AND CONVERT(VARCHAR(19),timestamp,101) <= CONVERT(DATETIME,''' + @Value2 + ''',101) AND intFormType <> 3 AND fklogin is not null GROUP BY fkLogin ORDER BY [Time Stamp]'
END
ELSE IF @Type = 11 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 1 and fklogin is not null and ISPO = '+ cast(@IsISPO as nvarchar) +' and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 12 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 2 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 13 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 3 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 14 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 4 and fklogin is not null and ISPO = '+ cast(@IsISPO as nvarchar) +' and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 15 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 5 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 16 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 6 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 17 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 7 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 18 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 8 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END
ELSE IF @Type = 19 --Status
BEGIN
    SET @Where = ' WHERE StatusId = 9 and ISPO = '+ cast(@IsISPO as nvarchar) +' and fklogin is not null and intId = (select max(intId) from '+@CoverageDB+' WHERE fkLogin = parent.fklogin)'
END

-- Looks like Christmas code.
SET @fkLogin = 'fkLogin as [XXX ID], '
--SET @fkLogin = 'DISTINCT(fkLogin) as [XXX ID], '
SET @TimeStamp = '(SELECT TOP 1 timestamp FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND (intFormType = 1 OR intFormType = 2) ORDER BY intID DESC) as [Time Stamp], '
SET @TimeStampOE = '(SELECT TOP 1 timestamp FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND intWizard = 10 AND intFormType = 4 ORDER BY intID DESC) as [Time Stamp OE], '
--SET @VType = '(SELECT TOP 1 Visiting_type FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Type], '
SET @FName = '(SELECT TOP 1 isNull(First_name,'' '') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [First Name], '
SET @LName = '(SELECT TOP 1 isNull(Last_name,'' '') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Last Name], '
SET @Email = '(SELECT TOP 1 isNull(email,'' '') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Email], '
SET @Start = '(SELECT TOP 1 Start_date FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Start], '
SET @End = '(SELECT TOP 1 End_date FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [End], '
--SET @Mod = '(SELECT TOP 1 IIF(intFormType = 2,''Y'',''N'') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Change Form], '
SET @intA = 'isnull((SELECT TOP 1 IIF(intFormType = 1,''Y'',''N'') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND intFormType = 1 ORDER BY intID DESC),''N'') as [New Form], '
SET @intB = 'isnull((SELECT TOP 1 IIF(intFormType = 2,''Y'',''N'') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND intFormType = 2 ORDER BY intID DESC),''N'') as [Mod Form], '
SET @intD = 'isnull((SELECT TOP 1 IIF(intFormType = 4,''Y'',''N'') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND intFormType = 4 ORDER BY intID DESC),''N'') as [OE Form], '
--SET @Dept = '(SELECT TOP 1 Dept FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as Dept, '
--SET @OE = '(SELECT TOP 1 isnull(Enroll,''N'') as Enroll FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND Enroll = ''O'' ORDER BY intID DESC) as OE, '
--SET @medPlan = '(SELECT TOP 1 isnull(medPlan,''NONE'') FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as [Med Plan], '
SET @intForm = '(SELECT TOP 1 isnull(intWizard,0) FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND (intFormType = 1 OR intFormType = 2 OR intFormType = 4) ORDER BY intID DESC) as [Latest Submission], '
SET @intFormOE = '(SELECT TOP 1 isnull(intWizard,0) FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin AND intFormType = 4 ORDER BY intID DESC) as [Latest OE Submission], '
SET @intID = '(SELECT TOP 1 intID FROM ' + @CoverageDB + ' WHERE fkLogin = parent.fkLogin ORDER BY intID DESC) as intID '

--SET @Fields = @fkLogin + @TimeStamp + @VType + @FName + @LName + @Email + @Start + @End + @Mod + @Dept + @OE + @medPlan + @intID
SET @Fields = @fkLogin + @TimeStamp + @TimeStampOE + @FName + @LName + @Email + @Start + @End + @intA + @intB + @intD + @intForm + @intFormOE + @intID

SET @FinSQL = 'SELECT ' + @Fields + ' FROM ' + @CoverageDB + ' AS parent ' + @Where

print @FinSQL
    Set @ParamDefinition =  ' @CoverageDB nvarchar(100)'

EXECUTE sp_Executesql @FinSQL, 
        @ParamDefinition, 
        @CoverageDB


I do not see anything wrong with your query. Unless there is an internal process (your organization has a SOP for this) and this process violates that SOP, then I would say yes, your coworker is just being mean.

I am not familiar with dynamic SQL. Regarding efficiency, I would advise reviewing with fellow coworkers for optimization if they suggest there is a better way to operate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM