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

  • Home
  • SEARCH
  • 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 5979641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:41:17+00:00 2026-05-22T21:41:17+00:00

i have a database table with a list of sql views i want to

  • 0

i have a database table with a list of sql views

i want to create a new view or stored procedure that based on which views are in that table will return those views unioned
like this

SELECT     ALINE1, HOME, EMAIL, EXPIRE, EDATE7, type
    FROM         dbo.campaign_membership_30
    UNION ALL
    SELECT     ALINE1, HOME, EMAIL, EXPIRE, EDATE7, type
    FROM         dbo.campaign_membership_30n
    UNION ALL
    SELECT     ALINE1, HOME, EMAIL, EXPIRE, EDATE7, type
    FROM         dbo.campaign_membership_60n
    UNION ALL
    SELECT     ALINE1, HOME, EMAIL, EXPIRE, EDATE7, type
    FROM         dbo.campaign_membership_today
    UNION ALL
    SELECT     ALINE1, BOOKNO, EMAIL, DEPART, DEP7, type
    FROM         dbo.depart_151days
    UNION ALL
    SELECT     ALINE1, BOOKNO, EMAIL, DEPART, DEP7, type
    FROM         dbo.depart_90Days
  • 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-22T21:41:18+00:00Added an answer on May 22, 2026 at 9:41 pm

    There are a lot of major caveats here.

    First, using dynamic SQL is often dangerous. It can open up your database to SQL injection attacks. If you don’t understand what these are then you need to do a lot of educating of yourself before I would suggest that you use dynamic SQL.

    Second, this kind of a pattern (storing tables and columns in a table from which to generate SQL) is a really bad pattern. There are many problems with it. I can’t suggest alternative solutions though without knowing more about your application/problem space.

    That said, the following is a simplistic version of how it might work in SQL Server 2005. This does not include error handling, etc.

    DECLARE @sql VARCHAR(MAX)  -- NVARCHAR if you use unicode characters in table names
    
    SELECT
        @sql = dbo.Concatenated_Rows('SELECT col1, col2 FROM ' + MT.table_name + ' UNION ALL ')
    FROM
        dbo.My_Table MT
    
    -- Remove the extra UNION ALL
    SELECT @sql = SUBSTRING(@sql, 1, LEN(@sql) - 11)
    
    EXEC(@sql)
    

    The function Concatenated_Rows would have to be written. You could alternatively use a cursor or maybe even FOR XML to create the concatenated string. Here’s a link that does a good job explaining many of the possible methods and does a good job of comparing them.

    Again, there are probably better solutions out there then going this route, especially if this is more than a one-off task.

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

Sidebar

Related Questions

I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have a database table that i want to allow my friends to update.
I have a Database table that I want to display in a DataGridView. However,
I have a List(Of String) which corresponds to types on our database table. We
I have a database table on a development server that is now fully populated
I have a database table (named Topics) which includes these fields : topicId name
I have a database table called Posts which stores all the information regarding an
I have a legacy SQL Server 2000 database that has one column that has
I have a table created in the sql ce database for my content part
Hi everyone i have one question i.e. i have some database table's which are

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.