Script SQLCMD to create DB with a variable: How to set it up?

  • Hello,

    I need to create a script to create some database : 1 to 3.

    I wrote the script with a variable called @NB_APP

    This variable should be asked to the user that runs the script.

    For now i run it like this in a command prompt:

    sqlcmd -v NB_APP="3" -i c:\CreateABA.sql

    Ultimately, i would like to click on the cmd file and the first question should be how many DB you need. That number is NB_APP variable.

    But I get an error saying :

    Must declare the scalar variable "@NB_APP".

    Can someone have a look at this please?

    Thanks

    DECLARE @NB_APP INT;

    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_01_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_01_log.ldf'
    GO

    IF @NB_APP <= 2
    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_02_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_02_log.ldf'
    GO

    IF @NB_APP <= 3
    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_03_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_03_log.ldf'
    GO
  • I am pretty proud of myself. I wrote this script and it worked.

    Now I need to know how to make the variable in the CMD.

    DECLARE @NBAPP INT;
    DECLARE @i INT;
    DECLARE @DBSource VARCHAR(250);
    DECLARE @DBTarget VARCHAR(250);
    DECLARE @NewDBTarget VARCHAR(250);
    DECLARE @NewDataDBTarget VARCHAR(250);
    DECLARE @NewLOGDBTarget VARCHAR(250);
    DECLARE @PathSourceDB VARCHAR(250);
    DECLARE @PathSourceDBData VARCHAR(250);
    DECLARE @PathSourceDBLog VARCHAR(250);
    DECLARE @PathFullSource VARCHAR(250)
    DECLARE @SourceDataDB VARCHAR(250);
    DECLARE @SourceLogDB VARCHAR(250);

    -------------------------------------
    -- The lines below need to be update
    SET @NBAPP = 5; --You need to update the number of apps you need.
    SET @DBSource = 'ABA_01' --You need to update this with the name of the database in your source bak file. If you do not know it, then do a manual restore and it will show up
    SET @DBTarget = 'ABA_' -- You can update that with the name
    SET @PathSourceDB = 'D:\SQL\BACKUP\' -- Here you put the path of the directory where the SOURCE BAK file is. Do not forget the \ at the end
    SET @PathSourceDBData = 'D:\SQL\DATA\' -- Here you put the path of the directory where the TARGET DATA file is going to be. Do not forget the \ at the end
    SET @PathSourceDBLog = 'D:\SQL\LOG\' -- Here you put the path of the directory where the TARGET LOG file is going to be. Do not forget the \ at the end
    -- No need to update below
    -------------------------------------

    SET @i = 1;
    SET @PathFullSource = @PathSourceDB + @DBSource + '.bak'
    SET @SourceDataDB = @DBSource + '_Data'
    SET @SourceLogDB = @DBSource + '_log'


    WHILE @i <= @NBAPP
    BEGIN

    SET @NewDBTarget = @DBTarget + convert(varchar(2),@i)
    SET @NewDataDBTarget = @PathSourceDBData + @DBTarget + convert(varchar(2),@i) + '_Data.mdf'
    SET @NewLOGDBTarget = @PathSourceDBLog + @DBTarget + convert(varchar(2),@i) + '_Log.ldf'

    PRINT @NewDBTarget
    PRINT @NewDataDBTarget
    PRINT @NewLOGDBTarget
    PRINT @PathFullSource

    RESTORE DATABASE @NewDBTarget
    FROM DISK = @PathFullSource
    WITH MOVE @SourceDataDB TO @NewDataDBTarget,
    MOVE @SourceLogDB TO @NewLOGDBTarget
    SET @i = @i +1
    END
  • Have you read this link:

    https://docs.microsoft.com/en-us/sql/ssms/scripting/sqlcmd-use-with-scripting-variables?view=sql-server-ver15

    It's pretty clear how to do this.

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • jbeclapez wrote:

    Hello,

    I need to create a script to create some database : 1 to 3. I wrote the script with a variable called @NB_APP This variable should be asked to the user that runs the script. For now i run it like this in a command prompt: sqlcmd -v NB_APP="3" -i c:\CreateABA.sql Ultimately, i would like to click on the cmd file and the first question should be how many DB you need. That number is NB_APP variable.

    But I get an error saying : Must declare the scalar variable "@NB_APP".

    Can someone have a look at this please?

    Thanks

    DECLARE @NB_APP INT;

    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_01_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_01_log.ldf'
    GO

    IF @NB_APP <= 2
    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_02_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_02_log.ldf'
    GO

    IF @NB_APP <= 3
    RESTORE DATABASE ABA_01
    FROM DISK = 'D:\SQL\BACKUP\ABA_01.bak'
    WITH MOVE 'ABA_01_Data' TO 'D:\SQL\DATA\ABA_03_Data.mdf',
    MOVE 'ABA_01_log' TO 'D:\SQL\LOG\ABA_03_log.ldf'
    GO

    Your problem in the code above is the batch separator GO. Your declaration of @NB_APP is only valid for the first bath (first restore).

     

  • My question would be...

    Why are you storing such a script on the root of the C: where some users could tamper with it or delete it?  This should be in a stored procedure, not in an unprotected SQLCMD script?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • totally correct - a procedure would be a quite good approach - you could check if any databases already exist with an option to replace them or generate more database e.g. ABA_04, ABA_05 and so on.

    I would prefer a powershell script for the task - with parameters like sqlInstance, databaseName, backupPath,... and you could do it with or without SqlServer Cmdlets (dotnet methods, SMO, dacpac, ...). With a powershell script you have a very broad flexibility...

  • I have the same concerns about a PoSh script as I do with an SQLCMD script.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply