Results 1 to 17 of 17

Thread: [RESOLVED] [SQL 2000] Conditional Conditions in SP

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Resolved [RESOLVED] [SQL 2000] Conditional Conditions in SP

    I have a sp that filters records in my datagrid (ASP.NET) but the optional parameters are throwing me off as the user can select any combination of date range only, date range and status, date range and scope, date range and status and scope, status and scope, status only, scope only.

    How can I best create the dynamic where clause for this. This is what I have so far.

    Thanks.

    VB Code:
    1. SET ANSI_NULLS ON
    2. GO
    3. SET QUOTED_IDENTIFIER ON
    4. GO
    5.  
    6. CREATE PROCEDURE [dbo].[usp_LoansFilter] AS
    7.     @From datetime = Null,
    8.     @To datetime = Null,
    9.     @Status nvarchar (15) = Null,
    10.     @Scope nvarchar (15) = Null
    11. AS
    12. BEGIN
    13.     SET NOCOUNT ON;
    14.     SELECT
    15.         [tblProspects].ProspectID,
    16.         [tblProspects].FirstName + ', ' + [tblProspects].LastName AS [Prospect], 'For display purposes only
    17.         [tblLoans].LoanID,
    18.         [tblLoans].Status,
    19.         [tblLoans].DateSubmitted,
    20.         [tblLoans].PriceComplete,
    21.         [tblLoans].FMA,
    22.         [tblLoans].JFMA,
    23.         [tblLoans].Purpose,
    24.         [tblLoans].LoanAmount,
    25.         [tblLoans].Created,
    26.         [tblLoans].Last_Updated
    27.     FROM
    28.         tblLoans
    29.     INNER JOIN
    30.         tblProspects
    31.         ON tblLoans.ProspectID = tblProspects.ProspectID
    32.     WHERE
    33.         (([tblLoans].Created >= @From) AND ([tblLoans].Created <= @To))
    34.             AND
    35.         (([tblLoans].Last_Updated <= @From) AND ([tblLoans].Last_Updated <= @To))
    36.             AND
    37.         (([tblLoans].Status = @Status))
    38.             AND
    39.         (([tblLoans].PriceComplete = @Scope))
    40. END
    41. GO
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL 2000] Conditional Conditions in SP

    I would personally go for something like this:
    VB Code:
    1. WHERE
    2.         ((@From Is Null) OR ([tblLoans].Created >= @From)) AND ((@To Is Null) OR ([tblLoans].Created <= @To)))
    3.             AND ...

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    So this would equate to nothing if the @From is not passed? I always thought OR's concatenated values but I'm probably stuck on VB thinking and not SQL thinking.

    Wouldnt it evaluate to ...

    WHERE
    AND AND ...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL 2000] Conditional Conditions in SP

    If you look at just the first part:
    Code:
    ((@From Is Null) OR ([tblLoans].Created >= @From))
    If @From is Null then the first half will be True, so that will be:
    Code:
    ((@True) OR ([tblLoans].Created >= @From))
    =>True (as "True Or *" is True)
    ..so all rows will match with that condition

    If @From is not Null then the first half will be false, so will be ignored (and the original clause will be used), eg:
    Code:
    ((False) OR ([tblLoans].Created >= @From))
    =>(([tblLoans].Created >= @From))  (as "False Or *" is *)
    ..so the original condition applies

  5. #5

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Ok, that takes care of the Date range part but for the status part I have a "All" condition where it would equate to all status' so that should be treated as a Null using similar logic....

    ((@Status Is Null) OR ([tblLoans].Status = @Status))

    But if "All" is passed then it wont be Null. Hmm, would it be good logic to eval the selection and only pass a status value if a specific one is selected vs. evaluating "all" in the sp?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL 2000] Conditional Conditions in SP

    Do you mean the value will be something like "All"? If so, you can just compare against the value:
    Code:
    ((@Status = 'All') OR ([tblLoans].Status = @Status))
    ..and set that as the default value for the parameter instead of Null.

    If you mean that you will be sending multiple values, then something like an In clause would be better.. but I've had too much to drink to think of a method!

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: [SQL 2000] Conditional Conditions in SP

    For the Date Fields, instead of a Null default, I would give them a default value that would include all records in the range.

    Code:
    CREATE PROCEDURE [dbo].[usp_LoansFilter] AS 
    	@From datetime = '01-Jan-1800',
    	@To datetime = '31-Dec-2100 23:59:59',
    	@Status nvarchar (15) = Null,
    	@Scope nvarchar (15) = Null

    Or if you use Null for a default, give them a value before executing the Select.

    Code:
    BEGIN
       SET NOCOUNT ON;
    
       If @From Is Null
           Set @From = '01-Jan-1800'
    
       If @To Is Null
           Set @To = '31-Dec-2100 23:59:59'
    As for Status, same idea. If "ALL" is passed set the parameter to Null.

    If @Status = "All"
    Set @Status = Null

    Where clause would not change.
    ((@Status Is Null) OR ([tblLoans].Status = @Status))

  8. #8

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    This is the whole clause then ...

    VB Code:
    1. WHERE
    2.         (((@From Is Null) OR ([tblLoans].Created >= @From)) AND ((@To Is Null) OR ([tblLoans].Created <= @To)))
    3.             AND
    4.         (((@From Is Null) OR ([tblLoans].Last_Updated <= @From)) AND ((@To Is Null) OR ([tblLoans].Last_Updated <= @To)))
    5.             AND
    6.         ((@Status Is Null) OR ([tblLoans].Status = @Status))
    7.             AND
    8.         ((@Scope Is Null) OR ([tblLoans].PriceComplete = @Scope))
    Basically...

    Option one:
    Date From - date value or Nullstring
    Date To - date value or Nullstring

    Option two:
    Status - Nullstring, InProcess, Pending, Complete etc.

    Option three:
    Scope - Nullstring, Date Submitted, Date Created, Last Updated


    So any combination of any of the three options is where I'm stuck.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Hmm, makes sense I think but could I or is it better to use a dynamic To date function?

    Code:
    CREATE PROCEDURE [dbo].[usp_LoansFilter] AS 
    	@From datetime = '01-Jan-1800',
    	@To datetime = GetDate(),
    	@Status nvarchar (15) = Null,
    	@Scope nvarchar (15) = Null
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL 2000] Conditional Conditions in SP

    As long as you send the appropriate parameters, the Where clause will work (no matter what combination you use), as each of your original clauses basically gets 'ignored' if the defaults haven't been changed.

    I like Bruce's idea, but it means that checks need to be run on each record, whereas the idea I suggested would get short-circuted, and the entire clauses ignored (if the value is Null/default)

  11. #11

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    I think I'm going to leave the Date params with the Nulls as default but change the status and scope to his suggestion...

    If @Status = "All"
    Set @Status = Null

    Then I can keep the where caluse as Si suggested.

    Testing time
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Not giving an error but not getting records back yet since the where clause is still kicking my butt.

    The Scope parameter is for specifying which field the From and To date range encompus. If the Scope is CreatedDate then the where clause will be looking in that field. If the Scope is LastUpdated then @Scope looks at that.

    So I am trying to get something like this to work.

    VB Code:
    1. WHERE
    2.         IF (@Scope = 'Created')
    3.             BEGIN
    4.                 (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) <= @To)))
    5.                     AND
    6.             END
    7.         IF (@Scope = 'Last Updated')
    8.             BEGIN
    9.                 (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @To)))
    10.                     AND
    11.             END
    12.         IF (@Scope = 'Date Submitted')
    13.             BEGIN
    14.                 (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) <= @To)))
    15.                     AND
    16.             END
    17.         ((@Status Is Null) OR ([tblLoans].Status = @Status))
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Got a little farther.

    Valid syntax but not returning records yet. Not sure if its a date format issue or an eval issue.


    VB Code:
    1. WHERE
    2.     (
    3.         (@Scope = 'Created')
    4.             OR
    5.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) <= @To)))
    6.     )
    7.         AND
    8.     (
    9.         (@Scope = 'Last Updated')
    10.             OR
    11.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @To)))
    12.     )
    13.         AND
    14.     (
    15.         (@Scope = 'Date Submitted')
    16.             OR
    17.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) <= @To)))
    18.     )
    19.         AND
    20.     ((@Status Is Null) OR ([tblLoans].Status = @Status))
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [SQL 2000] Conditional Conditions in SP

    It looks to me as if you have a bit of a logic error.. you basically have:
    "(scope=X OR (from....) AND (scope=Y ...", but should have:
    "(scope=X AND (from....) OR (scope=Y ..."

    eg:
    VB Code:
    1. WHERE
    2.    [B]([/B]
    3.     (
    4.         (@Scope = 'Created')
    5.             AND
    6.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Created, 101) <= @To)))
    7.     )
    8.         OR
    9.     (
    10.         (@Scope = 'Last Updated')
    11.             AND
    12.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].Last_Updated, 101) <= @To)))
    13.     )
    14.         OR
    15.     (
    16.         (@Scope = 'Date Submitted')
    17.             AND
    18.         (((@From Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) >= @From)) AND ((@To Is Null) OR (CONVERT(nvarchar(10), [tblLoans].DateSubmitted, 101) <= @To)))
    19.     )
    20.    [B])[/B]
    21.         AND
    22.     ((@Status Is Null) OR ([tblLoans].Status = @Status))
    I assumed that the Status part should apply to all variations of Scope, so that still needs an And, with braces around the Scope part.

  15. #15

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Statuis is a separate condition. I did some tracing and seens the @From and @To are in a different date format - Jan 1 2006 vs. the convert of 1/1/2006 but I wont be able to test until tonight. I have to leave as I am teaching a tranning class on some software.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [SQL 2000] Conditional Conditions in SP

    Rob - we have tried dozens of different methods to arrive at solutions to this and have found greate success with this:

    For example - we have these parameters - which be be left blank or filled in:

    Code:
    	,@Yr int
    	,@Bldg varchar(50)
    	,@Sort_Order_Grade_Hrm_Team_Zip varchar(20)
    	,@Grade_Range varchar(5)
    	,@Homeroom varchar(5)
    	,@Team varchar(2)
    and we build a table variable with the primary keys we want to actually select:

    Code:
    Declare @IdList Table (StuId int, Bldg int, Grade varchar(2), Team varchar(2), HomeRoom varchar(5))
    
    Insert into @IdList Select StuId,Bldg,Grade,Null,Null From ActiveStudent_V Where Yr=@Yr and Bldg=@Bldg
    
    If IsNull(@Grade_Range,'')<>'' Delete From @IdList Where Grade<@LowGrade or Grade>@HighGrade or Grade is null
    
    Update @IdList Set Homeroom=(Select Top 1 TV.Room From Class_T CL 
    				Left Join Course_T CR on CR.Yr=CL.Yr and CR.Bldg=CL.Bldg and CR.Crs=CL.Crs
    				Left Join Time_T TV on TV.Yr=CL.Yr and TV.Bldg=CL.Bldg and TV.Crs=CL.Crs and TV.Sectn=CL.Sectn
    							and TV.TimeVec=CL.TimeVec
    				WHERE (CL.StuId = IL.StuID 
    					AND CL.Yr = @Yr AND CL.Bldg = @Bldg AND CR.CrsType='H')
    					ORDER BY CL.AddDate Desc)
    		From @IdList IL
    
    If IsNull(@Homeroom,'')<>'' Delete From @IdList Where Homeroom<>@Homeroom or Homeroom is null
    
    Update @IdList Set Team=RT.Team
    		From @IdList IL 
    		Left Join Room_T RT on RT.Yr=@Yr and RT.Bldg=@Bldg and RT.Room=IL.HomeRoom
    
    If IsNull(@Team,'')<>'' Delete From @IdList Where Team<>@Team or Team is null
    We build the table variable in a specific order that arrives at what we want in the fastest way possible.

    We then come FROM the table variable:

    Code:
    	FROM @IdList IL
    	INNER JOIN Student_T ST ON (ST.StuId = IL.StuId)
    If you have a potential for lots of rows you want to use a TEMP TABLE instead of a table variable.

    This has taken some of our reporting SPROCS that took 10's of minutes to run and dropped them to under a minute.

    We also use the SI method often - the ((...is null) or (col matches)) concept. That's our typical method unless we run into speed or complexity issues.

    What I like most about the method I showed here is that it's easy to upgrade and maintain - as it's done in stages. Sometimes we get asked for SORT or SELECT by ZIP for some output that never had it before. We simply add ZIP to the TABLE VARIABLE and we can now SELECT or ORDER on it.

    Since we have dozens of common reportings SPROCS we typically just rip this stuff and copy/paste it around.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [SQL 2000] Conditional Conditions in SP

    Seem to have it working correctly now (finally lol).

    I used Si's updated where caluse


    Thanks guys
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width