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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:35:37+00:00 2026-05-23T00:35:37+00:00

I need to use the same Stored Procedures against many tables all with the

  • 0

I need to use the same Stored Procedures against many tables all with the same structure in my DB. This is data loaded from customers,with one table/customer and the data needs calculations/checks run before it’s loaded to our DataWarehouse.

So far these are the options and issues I’ve found and I’m looking for a better pattern/approach.

  1. Create a view that points to the
    table I want to process, the SPs
    then talk to that view. This works
    well (especially once I’d worked out
    how to create views ‘automagically’
    based on their columns). But the
    view can only be used with one table
    at a time, forcing the system to
    deal with one customer at a time.

  2. Use dynamic sql within each SP –
    makes the SPs much harder to
    read/debug and for those reasons has
    been ruled out

  3. Create a partitioned view across
    all the tables and then use a
    paramatised table function to return
    just the data we’re interested in –
    ah but then I can’t update the data
    as the function returns a table that
    can be only used for select

  4. Use dynamic sql inside a function
    (can’t be done) to create a view
    (which also can’t be done) …. give
    up
  5. Within the SP create a temp table
    with over the target table using
    dynamic sql, but then the temp table
    only exists in the session that runs
    the dynamic sql not the ‘parent’
    session that’s running the SP …
    give up
  6. Create a global temp table using
    dynamic SQL to avoid the scope issue
    of 5, then run the SP against the
    global temp table. Still run into
    the single customer issue.
  7. Create the view as in 1 within a
    transaction and then run all the SPs
    and then commit – works fine for one
    user, but any others are now blocked
    trying to create a new view of the
    same name
  8. Use a temporary view … can’t in
    T/Sql
  9. Move all the code into .Net – but
    we have environment issues where
    tsql is much easier to host/run

I know I’m not the only person who has this problem, have any of you good people solved it, please help.

  • 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-23T00:35:37+00:00Added an answer on May 23, 2026 at 12:35 am

    Maybe your approach is wrong, I will go deep in details in a while but it seems that your problem can be solved using SSIS

    — Updated answer:

    First, the big picture:

    The most affordable way to process the tables dynamically is using a script instead of a stored procedure. If you want to make table access randomly chosen, you certainly will not use any of the performance advantages of stored procedures, i.e. execution plans. A SQL Script can be easily upgraded to point one table at runtime using placeholders and replacing it before executing.

    The script can be loaded from the filesystem, a variable, a text column in a table, etc. The loading process consists in read the script content to a string variable. This step occurs once.

    The next step is the preparation stage. This step will be executed for each table to be processed. The main business of this step is to replace the table placeholders with the current table being processed. Also is possible to set parameter values like any parameter you can need to pass into the sp that you already wrote.

    The last step is the execution of the script. As is already loaded into a variable and the placeholders were set to the current table name, you can safely call a ExecuteSQLTask with the sql variable as the input. This process of course happens for each table you want to process.

    Ok. Now let’s see this in action.

    This is a sample database model:

    CREATE TABLE [dbo].[t_n](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [name] [varchar](50) NOT NULL,
      [start] [datetime] NULL,
      CONSTRAINT [PK_t_n] PRIMARY KEY CLUSTERED ([id] ASC)
    ) ON [PRIMARY]
    

    where t_n represents any table (t_1, t_2, t_3, etc).

    This is your current stored procedure:

    CREATE PROCEDURE SpProcessT_n 
    AS
    BEGIN
        SET NOCOUNT ON;
        SELECT * FROM [t1]; 
    END
    GO
    

    Now, transform this stored procedure to a Sql script, placing a placeholder instead of the table name

        SET NOCOUNT ON;
        SELECT * FROM [$table_name];
    

    I choose to save this in a .sql file in the filesystem to keep the POC as simple as possible.

    Next, create a SSIS Package like this:

    Basic SSIS Package

    These are the settings I choose to set up the loop:
    enter image description here

    And this is the way you can assign the table name to a variable called appropriately _table_name_
    enter image description here

    This is the setup of the script task, here you find that the variable _table_name_ has read only access, while a new variable called SqlExec has read/write access:

    enter image description here

    And this is it’s Main function:

        public void Main()
        {
            String Table_Name = Dts.Variables["table_name"].Value.ToString();
            String SqlScript;
            Regex reg = new Regex(@"\$table_name", RegexOptions.Compiled);
            using (var f = File.OpenText(@"c:\sqlscript.sql")) {
                SqlScript = f.ReadToEnd();
                f.Close();
            }
            SqlScript = reg.Replace(SqlScript, Table_Name);
            Dts.Variables["SqlExec"].Value = SqlScript;
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    

    You can notice that the Dts Variable SqlExec contains the sql script that will be executed. Now you can set the following options in your ExecuteSqlTask:

    enter image description here

    Successfully tested in MSSQL 2008, if you put a insert inside the script file you will notice new rows in each table.

    Hope this helps!

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

Sidebar

Related Questions

I've always done transactions from within stored procedures but now I need to wrap
I need to use sed to convert all occurences of ##XXX## to ${XXX} .
I need to use NSImage which appears need to be imported from <AppKit/AppKit.h> .
I need to use a many to many relationship in my project and since
I need to use sendmail from Macs in an office. At the moment, I
I have a number of search functions (stored procedures) which need to return results
We have 2 databases, say DB1 and DB2. DB1 contains all the stored procedures
I need to use an alias in the WHERE clause, but It keeps telling
I need to use a byte array as a profile property in a website.
I need to use a datetime.strptime on the text which looks like follows. Some

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.