In SQL server is there any way to get the 'use database' command to accept a variable

前端 未结 4 764
醉酒成梦
醉酒成梦 2020-12-17 22:37

I would like to write a \'generic\' script to operate on a number of databases. The \'use databasename\' commands will be distributed in the body of the script. I would rath

4条回答
  •  清歌不尽
    2020-12-17 22:48

    Check out Scripting Variables in SQLCMD.

    This enables you to put variables into your scripts in the form:

    USE $(db1_name)
    ...some code...
    
    USE $(db2_name)
    ...some code...
    

    And have the variable values interpolated from environment variables, parameters provided at runtime, or hard coded value assignments.

提交回复
热议问题