Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:13:27+00:00 2026-06-10T11:13:27+00:00

I have a couple of dynamically built query’s with a composite primary key on

  • 0

I have a couple of dynamically built query’s with a composite primary key on the tblWarr_SWH_WarrCLHD_35 and [Account No], tblWarr_SWH_WarrCLHD_35.[Claim No] columns.

A new requirement has recently come out asking us to provide exceptions on the results, so the users can ask to filter out each row by [Account No] and [Claim No]. There will be an indeterminate number of row exceptions.

What would be the best method for the client to pass in the exceptions list [Account No] and [Claim No] combinations?

Do I need to pass in a string and parse it within the T-SQL? I could build it like this

AND NOT ([Account No] = 'abc' AND [Claim No] = 'cde') 
AND NOT ([Account No] = 'fgh' AND [Claim No] = 'ijk')

The results are displayed to the client software via a typed data set with two tables.

Raw Data

DECLARE @SQL nvarchar(4000)
DECLARE @SQLWhere nvarchar(4000) 
DECLARE @SQLOrder nvarchar(MAX)

SET @SQL = 'SELECT tblWarr_SWH_WarrCLHD_35.[Account No], tblWarr_SWH_WarrCLHD_35.[Claim No], tblWarr_SWH_WarrCLHD_35.ClaimCost, 
                      tblWarr_SWH_WarrCLHD_35.[Serial No], tblWarr_SMT_MCType_47.[Machine Type], tblWarr_SMH_MCHist_34.[Date Built], 
                      tblWarr_SMH_MCHist_34.[Date Sold], tblWarr_SVC_ServiceCodes.[Service Code], tblWarr_DefectCodes.Code AS [Defect Code], 
                      tblWarr_SPN_PartNo_01.[Part No], tblWarr_SPN_PartNo_01.[Part Description], tblWarr_SWH_WarrCLHD_35.[Failed Part No Qty], 
                      tblWarr_MachineHours.[Machine Hours At Failure], tblWarr_SWH_WarrCLHD_35.[Claim Date], tblWarr_SNA_WarNar_77.Narrative, 
                      tblWarr_SSU_Supplier_09.[Supplier Name],tblWarr_SWH_WarrCLHD_35.[ClaimStateID]
                FROM tblWarr_SMH_MCHist_34 INNER JOIN
                      tblWarr_SWH_WarrCLHD_35 ON tblWarr_SMH_MCHist_34.[Serial No] = tblWarr_SWH_WarrCLHD_35.[Serial No] INNER JOIN
                      tblWarr_SMT_MCType_47 ON tblWarr_SMH_MCHist_34.MachineTypeID = tblWarr_SMT_MCType_47.ID INNER JOIN
                      tblWarr_SVC_ServiceCodes ON tblWarr_SWH_WarrCLHD_35.ServiceCodeFullID = tblWarr_SVC_ServiceCodes.ID INNER JOIN
                      tblWarr_DefectCodes ON tblWarr_SWH_WarrCLHD_35.DefectCodeID = tblWarr_DefectCodes.ID INNER JOIN
                      tblWarr_SPN_PartNo_01 ON tblWarr_SWH_WarrCLHD_35.FailedPartNoID = tblWarr_SPN_PartNo_01.ID INNER JOIN
                      tblWarr_MachineHours ON tblWarr_SWH_WarrCLHD_35.MachineHoursID = tblWarr_MachineHours.ID INNER JOIN
                      tblWarr_SNA_WarNar_77 ON tblWarr_SWH_WarrCLHD_35.NarrativeID = tblWarr_SNA_WarNar_77.ID INNER JOIN
                      tblWarr_SSU_Supplier_09 ON tblWarr_SWH_WarrCLHD_35.SupplierID = tblWarr_SSU_Supplier_09.ID'
SET @SQLWhere = ' WHERE ((tblWarr_SPN_PartNo_01.[Part No] = @_PartNumber) AND '
SET @SQLOrder = ' ORDER BY CAST(tblWarr_SMH_MCHist_34.[Date Built] AS smalldatetime) DESC'

IF (@ClaimDateFrom IS NOT NULL AND @ClaimDateTo IS NOT NULL)AND (LEN(@ClaimDateFrom) > 0 AND LEN(@ClaimDateTo) > 0)
    SET @SQLWhere = @SQLWhere + '(tblWarr_SWH_WarrCLHD_35.[Claim Date] BETWEEN @_ClaimDateFrom AND @_ClaimDateTo) AND '

IF (@BuildDateFrom IS NOT NULL AND @BuildDateTo IS NOT NULL)AND (LEN(@BuildDateFrom) > 0 AND LEN(@BuildDateTo) > 0)
    SET @SQLWhere = @SQLWhere + '(CAST(tblWarr_SMH_MCHist_34.[Date Built] AS SmalldateTime) BETWEEN @_BuildDateFrom AND @_BuildDateTo) AND '

IF (@ClaimStates IS NOT NULL)AND (LEN(@ClaimStates) > 0)
    SET @SQLWhere = @SQLWhere + '(tblWarr_SWH_WarrCLHD_35.[ClaimStateID] IN (SELECT value FROM Split(''' + ',' + ''',@_ClaimStates))) AND '

IF LEN(@SQLWhere) > 0
    SET @SQL = @SQL + LEFT(@SQLWhere, LEN(@SQLWhere)-4) + ')' + @SQLOrder

EXEC sp_executesql @SQL,
    N'@_PartNumber nvarchar(20), @_ClaimDateFrom smalldatetime, @_ClaimDateTo smalldatetime, @_BuildDateFrom smalldatetime, @_BuildDateTo smalldatetime, @_ClaimStates nvarchar(50)',
    @_PartNumber = @PartNumber, @_ClaimDateFrom = @ClaimDateFrom, @_ClaimDateTo = @ClaimDateTo, @_BuildDateFrom = @BuildDateFrom, @_BuildDateTo = @BuildDateTo, @_ClaimStates = @ClaimStates

Aggregate Data

DECLARE @SQL nvarchar(4000)
DECLARE @SQLWhere nvarchar(4000) 
DECLARE @SQLOrder nvarchar(MAX)
DECLARE @Date1 as nvarchar(5)   

SET @Date1 = '01/'

SET @SQL = 'SELECT CAST(@_Date1 + CAST(DATEPART(mm,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(2)) + ''' + '/' + ''' + CAST(DATEPART(yyyy,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(4)) as smalldatetime) AS ClaimDate , COUNT(tblWarr_SWH_WarrCLHD_35.[Claim No]) As FaultCount
                FROM tblWarr_SMH_MCHist_34 INNER JOIN
                      tblWarr_SWH_WarrCLHD_35 ON tblWarr_SMH_MCHist_34.[Serial No] = tblWarr_SWH_WarrCLHD_35.[Serial No] INNER JOIN
                      tblWarr_SMT_MCType_47 ON tblWarr_SMH_MCHist_34.MachineTypeID = tblWarr_SMT_MCType_47.ID INNER JOIN
                      tblWarr_SVC_ServiceCodes ON tblWarr_SWH_WarrCLHD_35.ServiceCodeFullID = tblWarr_SVC_ServiceCodes.ID INNER JOIN
                      tblWarr_DefectCodes ON tblWarr_SWH_WarrCLHD_35.DefectCodeID = tblWarr_DefectCodes.ID INNER JOIN
                      tblWarr_SPN_PartNo_01 ON tblWarr_SWH_WarrCLHD_35.FailedPartNoID = tblWarr_SPN_PartNo_01.ID INNER JOIN
                      tblWarr_MachineHours ON tblWarr_SWH_WarrCLHD_35.MachineHoursID = tblWarr_MachineHours.ID INNER JOIN
                      tblWarr_SNA_WarNar_77 ON tblWarr_SWH_WarrCLHD_35.NarrativeID = tblWarr_SNA_WarNar_77.ID INNER JOIN
                      tblWarr_SSU_Supplier_09 ON tblWarr_SWH_WarrCLHD_35.SupplierID = tblWarr_SSU_Supplier_09.ID'
SET @SQLWhere = ' WHERE ((tblWarr_SPN_PartNo_01.[Part No] = @_PartNumber) AND '
SET @SQLOrder = ' GROUP BY CAST(@_Date1 + CAST(DATEPART(mm,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(2)) + ''' + '/' + ''' + CAST(DATEPART(yyyy,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(4)) as smalldatetime) ORDER BY CAST(@_Date1 + CAST(DATEPART(mm,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(2)) + ''' + '/' + ''' + CAST(DATEPART(yyyy,CAST(tblWarr_SMH_MCHist_34.[Date Built] as smalldatetime)) as nvarchar(4)) as smalldatetime)'

IF (@ClaimDateFrom IS NOT NULL AND @ClaimDateTo IS NOT NULL)AND (LEN(@ClaimDateFrom) > 0 AND LEN(@ClaimDateTo) > 0)
    SET @SQLWhere = @SQLWhere + '(CAST(tblWarr_SWH_WarrCLHD_35.[Claim Date] AS SmalldateTime) BETWEEN @_ClaimDateFrom AND @_ClaimDateTo) AND '

IF (@BuildDateFrom IS NOT NULL AND @BuildDateTo IS NOT NULL)AND (LEN(@BuildDateFrom) > 0 AND LEN(@BuildDateTo) > 0)
    SET @SQLWhere = @SQLWhere + '(CAST(tblWarr_SMH_MCHist_34.[Date Built] AS SmalldateTime) BETWEEN @_BuildDateFrom AND @_BuildDateTo) AND '

IF (@ClaimStates IS NOT NULL)AND (LEN(@ClaimStates) > 0)
    SET @SQLWhere = @SQLWhere + '(tblWarr_SWH_WarrCLHD_35.[ClaimStateID] IN (SELECT value FROM Split(''' + ',' + ''',@_ClaimStates))) AND '

IF LEN(@SQLWhere) > 0
    SET @SQL = @SQL + LEFT(@SQLWhere, LEN(@SQLWhere)-4) + ')' + @SQLOrder

EXEC sp_executesql @SQL,
    N'@_PartNumber nvarchar(20), @_ClaimDateFrom smalldatetime, @_ClaimDateTo smalldatetime, @_BuildDateFrom smalldatetime, @_BuildDateTo smalldatetime, @_Date1 nvarchar(5), @_ClaimStates nvarchar(50) ',
    @_PartNumber = @PartNumber, @_ClaimDateFrom = @ClaimDateFrom, @_ClaimDateTo = @ClaimDateTo, @_BuildDateFrom = @BuildDateFrom, @_BuildDateTo = @BuildDateTo, @_Date1 = @Date1, @_ClaimStates = @ClaimStates
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:13:28+00:00Added an answer on June 10, 2026 at 11:13 am

    May I suggest the exceptions be passed in like this:

    “account_A,claim_1;account_B,claim_2;account_C, claim_3;”

    Then, you can create a function to parse this string to return a table @ExceptionAccountsAndClaims:

    ExceptionAccount | ExceptionClaim

    account_A | claim_1

    account_B | claim_2

    account_C | claim_3

    Then, in your dynamic query, use the condition:

    WHERE (SELECT COUNT(*) FROM @ExceptionAccountsAndClaims e WHERE
    tblWarr_SWH_WarrCLHD_35.[Account No] = e.ExceptionAccount AND
    tblWarr_SWH_WarrCLHD_35.[Claim No] = e.ExceptionClaim) = 0

    Note: The condition may not be 100% correct. Please correct it to work for you if you choose to.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form wich dynamically creates a new row with a couple of
I have a couple of input boxes that get dynamically created, and then filled.
I have an XYPlot on which are series and a couple of dynamically added
So I have a couple of <span> elements that are being generated dynamically. The
I have couple of hyperlinks on my website. What I want is to remove
I have couple of dozen pieces of data that I need to save and
I have couple of formulas and data coming from database. I want to refresh
I have couple questions regarding some C++ rules. Why am I able to call
I have couple resource DLLs that I currently load when application starts using following
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.