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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:01:28+00:00 2026-05-12T17:01:28+00:00

I have a sql server stored procedure that I use to backup data from

  • 0

I have a sql server stored procedure that I use to backup data from our database before doing an upgrade, and I’d really like it to be able to run the stored procedure on multiple databases by passing in the database name as a parameter. Is there an easy way to do this? The best I can figure is to dynamically build the sql in the stored procedure, but that feels like its the wrong way to do it.

  • 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-12T17:01:28+00:00Added an answer on May 12, 2026 at 5:01 pm

    There isn’t any other way to do this. Dynamic SQL is the only way; if you’ve got strict controls over DB names and who’s running it, then you’re okay just truncating everything together, but if there’s any doubt use QUOTENAME to escape the parameter safely:

    CREATE PROCEDURE doStuff
        @dbName NVARCHAR(50)
    AS
        DECLARE @sql NVARCHAR(1000)
    
        SET @sql = 'SELECT stuff FROM ' + QUOTENAME(@dbName) + '..TableName WHERE stuff = otherstuff'
    
        EXEC sp_ExecuteSQL (@sql)
    

    Obviously, if there’s anything more being passed through then you’ll want to double-check any other input, and potentially use parameterised dynamic SQL, for example:

    CREATE PROCEDURE doStuff
        @dbName NVARCHAR(50)
        @someValue NVARCHAR(10)
    AS
        DECLARE @sql NVARCHAR(1000)
    
        SET @sql = 'SELECT stuff FROM ' + QUOTENAME(@dbName) + '..TableName WHERE stuff = @pOtherStuff'
    
        EXEC sp_ExecuteSQL (@sql, '@pOtherStuff NVARCHAR(10)', @someValue)
    

    This then makes sure that parameters for the dynamic SQL are passed through safely and the chances for injection attacks are reduced. It also improves the chances that the execution plan associated with the query will get reused.

    • 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 stored procedure that is not returning any data when
On our SQL Server (Version 10.0.1600), I have a stored procedure that I wrote.
I have a stored procedure that executes much faster from Sql Server Management Studio
I have written a SQL Server stored procedure that includes a DateTime parameter. This
I have an app that runs a stored procedure in SQL Server, checking the
Let's say I have a stored procedure in Microsoft SQL Server 2005 that returns
We have a SQL Server 2008 database that has stored procedures to handle reads/writes/etc.
I have a stored procedure on SQL Server 2005. It is pulling from a
I am building up a dataset from SQL server - a stored procedure that
I have a SQL Server 2008 database which contains data that I need to

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.