SQL: How to make table name in stored procedure dynamic

I am pretty new to SQL Server and hope someone here can help me with this (I’m using QL Server 2008).

The following is a small procedure that works as intended.

  • SQL query to extract text from a column and store it to a different column in the same record
  • What is the area in a LEVEL_4 cell in a geography spatial index in SQL Server?
  • How to avoid duplicates in INSERT INTO SELECT query in SQL Server?
  • New cardinality estimator (SQL Server 2014) is way off
  • SQL Server: Make all UPPER case to Proper Case/Title Case
  • SQL Server CLR TVF to return two new columns into dataflow
  • Now I would like to use the same procedure to update multiple tables as all these tables have exactly the same column names and column formatting, the only difference is the 2nd part of the table name for which I added XXX below.

    Can someone tell me how this could be made dynamic and also provide me some explanations on this ?
    I cannot provide much more here as I wasn’t sure about how to approach this – other than probably declaring @sql nvarchar(max) and wrapping the whole query in SET @sql = N'...' before executing it.

    My stored procedure:

        CREATE PROCEDURE [dbo].[Cal_UpdateTeam]
            @team nvarchar(100),
            @teamID int,
            @notes nvarchar(1000),
            @log nvarchar(100),
            @admin varchar(50)
        AS
        BEGIN
            SET NOCOUNT ON;
    
            BEGIN   
    
            IF NOT EXISTS 
            (
                    SELECT  * 
                    FROM    Cal_XXX
                    WHERE   teamID = @teamID
            )
            INSERT INTO Cal_XXX
            (
                    team,
                    teamID,
                    notes,
                    log,
                    admin
            )
            SELECT  @team,
                    @teamID,
                    @notes,
                    @log,
                    @admin
            ELSE
                    UPDATE  Cal_XXX
                    SET     team = @team,
                            teamID = @teamID,
                            notes = @notes,
                            log = @log,
                            admin = @admin
                    WHERE   teamID = @teamID
    
            END
    END
    

    Many thanks for any tips and advise on this, Mike.

  • Compress data on a key word in sql server
  • SQL: IF clause within WHERE clause
  • SQL Server - CAST AND DIVIDE
  • MSSQL select count where condition is met across a date range
  • Difference of create Index by using include column or not using
  • SSIS Error: insert values list doesn‘t match column list
  • One Solution collect form web for “SQL: How to make table name in stored procedure dynamic”

    you should wrap your sql query in an nvarchar and then execute that query as in the below example :

        declare @sql nvarchar(max)
        declare @TableName nvarchar(max)
        set @TableName = 'mytable'
        set @sql = 'Select * from ' + @TableName
        Exec sp_executesql @sql
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.