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 1014713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:17:26+00:00 2026-05-16T10:17:26+00:00

I have data in the SQL 2005 table as below. Table Name: FilterData CategoryID

  • 0

I have data in the SQL 2005 table as below.

Table Name: FilterData
CategoryID ColumnID  AnswerCode
--------------------------------
0349       Q15       02
0349       Q15       03
0349       Q16       04
Table Name: TransactionData
CategoryID Q15
---------------------------------
0349       01  
0349       02
0349       03
0349       04
0349       05
0349       06

FilterData table has information about what columns will be used to filter select statements on TransactionData. So when i execute Select * from TransactionData, i want to build Where Clause from FilterData table so that i can fire it on TransactionData table for that CategoryID.

Can someone please help me out that how can i do this?

  • 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-05-16T10:17:27+00:00Added an answer on May 16, 2026 at 10:17 am

    If I understand your question correctly, the code below should work. Note that I’ve created a function to help with the string concatenation. I’m not sure if you wanted to AND or OR your conditions. I’ve used AND in my sample. You can easily adjust it for OR if that’s appropriate.

    /* Set up sample data */
    create table FilterData (
        CategoryID char(4),
        ColumnID char(3),
        AnswerCode char(2)
    )
    
    insert into FilterData
        (CategoryID, ColumnID, AnswerCode)
        values
        ('0349','Q15','02')
    insert into FilterData
        (CategoryID, ColumnID, AnswerCode)
        values
        ('0349','Q15','03')
    insert into FilterData
        (CategoryID, ColumnID, AnswerCode)
        values
        ('0349','Q16','04')
    go
    
    /* Helper function to concatenate all AnswerCodes for a given ColumnID */
    create function dbo.fnStringAnswerCodes(@ColumnID char(3))
    returns varchar(1000)
    as
    begin
        declare @CodeString varchar(1000)
        set @CodeString = @ColumnID + ' in ('
    
        select @CodeString = @CodeString + '''' + AnswerCode + ''','
            from FilterData
            where ColumnID = @ColumnID
    
        /* Remove trailing comma and add closing parens */
        select @CodeString = left(@CodeString, len(@CodeString)-1) + ')'
    
        return @CodeString
    end
    go
    
    declare @CategoryID char(4)
    declare @SQLString varchar(1000)
    declare @WhereClause varchar(1000)
    
    set @CategoryID = '0349'
    set @SQLString = 'select * from TransactionData '
    set @WhereClause = 'where CategoryID=''' + @CategoryID + ''' and '
    
    
    select @WhereClause = @WhereClause + dbo.fnStringAnswerCodes(ColumnID) + ' and '
        from FilterData
        where CategoryID = @CategoryID
        group by ColumnID
    
    /* Remove Trailing 'AND' */
    set @WhereClause = LEFT(@WhereClause, len(@WhereClause)-3)
    
    set @SQLString = @SQLString + @WhereClause
    select @SQLString
    
    /* Last step would be to dynamically execute the string we built */
    /* exec sp_ExecuteSQL @SQLString */
    
    /* Clean Up */
    drop function dbo.fnStringAnswerCodes
    drop table FilterData
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Sql Server 2005 table with following data: idHearing is the primary
SQL 2005, 600,000,000 rows. I have a table called Location currently using the data
I have some varbinary data stored in a table in MS Sql Server 2005.
I have the following table and data in SQL Server 2005: create table LogEntries
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
I have data in an SQL Server 2005 database which I need to copy
i have using aqua data studio to run my query from sql server 2005.
I have several assemblies for SQL Server 2005. If invalid data is entered to
I have some data in an SQL table with an XML datatype column where
I have to pull data from a SQL database table to my DB2 table.

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.