SQLCMD mode in Query Window using variables

  • While the follwing example is possible

    :SETVAR DB_NAME_ASSIGN "TEST TEXT"

    :SETVAR DB_NAME_TEXT "EXPECT INSERT OF DB_NAME_ASSIGN HERE: "

    /*Get output*/

    SELECT '$(DB_NAME_TEXT)' + '$(DB_NAME_ASSIGN)' AS OUTPUT

    /* Delivers:

    EXPECT INSERT OF DB_NAME_ASSIGN HERE: TEST TEXT

    */

    how can I make the opposite this possible? That is, to declare variables in T_SQL and pass them to SQLCMD mode and execute them? For example, consider the following:

    declare

    @src_db_name sysname,

    @target_db_name sysname,

    @src_web_svr_name varchar(75),

    @target_web_svr_name varchar(75),

    @SQLCMDLine varchar(250)

    SELECT@src_db_name = 'DEVELDB',

    @target_db_name = 'TESTDB',

    @src_web_svr_name = 'DEVELSERVER',

    @target_web_svr_name = 'TESTSERVER'

    SELECT @SQLCMDLine = 'xcopy \\' + @src_web_svr_name + '\Apps\FTPServer\FSCM90\' + @src_db_name + '\*.* '

    SELECT @SQLCMDLine = @SQLCMDLine + ' \\' + @target_web_svr_name + '\Apps\FTPServer\FSCM90\' + @target_db_name + '\*.* '

    /* This is where I can't it to work */

    !! sqlcmd @SQLCMDLine

    Anybody ever dealt with this one?

  • despag (1/26/2011)


    While the follwing example is possible

    :SETVAR DB_NAME_ASSIGN "TEST TEXT"

    :SETVAR DB_NAME_TEXT "EXPECT INSERT OF DB_NAME_ASSIGN HERE: "

    /*Get output*/

    SELECT '$(DB_NAME_TEXT)' + '$(DB_NAME_ASSIGN)' AS OUTPUT

    /* Delivers:

    EXPECT INSERT OF DB_NAME_ASSIGN HERE: TEST TEXT

    */

    how can I make the opposite this possible? That is, to declare variables in T_SQL and pass them to SQLCMD mode and execute them? For example, consider the following:

    declare

    @src_db_name sysname,

    @target_db_name sysname,

    @src_web_svr_name varchar(75),

    @target_web_svr_name varchar(75),

    @SQLCMDLine varchar(250)

    SELECT@src_db_name = 'DEVELDB',

    @target_db_name = 'TESTDB',

    @src_web_svr_name = 'DEVELSERVER',

    @target_web_svr_name = 'TESTSERVER'

    SELECT @SQLCMDLine = 'xcopy \\' + @src_web_svr_name + '\Apps\FTPServer\FSCM90\' + @src_db_name + '\*.* '

    SELECT @SQLCMDLine = @SQLCMDLine + ' \\' + @target_web_svr_name + '\Apps\FTPServer\FSCM90\' + @target_db_name + '\*.* '

    /* This is where I can't it to work */

    !! sqlcmd @SQLCMDLine

    Anybody ever dealt with this one?

    what's the error you are getting? For your statement, I wouldn't use SELECT, I would use SET. Does your server have robocopy? that will make your life easier. any of the following robocopy syntax will do the trick.

    robocopy source\folder destination\folder *.* /E

    robocopy source\folder destination\folder /E

    robocopy source\folder destination\folder /MIR

    why is robocopy better than xcopy? robocopy has retries, will resume where it failed, able to ID old and new files.

    -----------------------------
    www.cbtr.net
    .: SQL Backup Admin Tool[/url] :.

  • we do have robocopy. The issue is to pass T-SQL declared and set variables to the SQLCMD. I just can't get the syntax right......

    From SQLCMD to T-SQL is easy, but the opposite, no so easy.

    In the meantime I created a SQL job and update its steps by T-SQl and then start it.

  • What's the error?

    from your bit of code, you are trying to make a call to a cmdline app, I don't see anywhere where you are making use of xp_cmdshell.

    xp_cmdshell @sqlcmd. If u get an error, you need to enable it.

    -----------------------------
    www.cbtr.net
    .: SQL Backup Admin Tool[/url] :.

  • We cannot make use of xp_cmdshell. It has been deleted from our extended procedures list for security reasons.

    This is the error I get:

    HResult 0x2, Level 16, State 1

    Named Pipes Provider: Could not open a connection to SQL Server [2].

    Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..

    Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.

  • despag (1/28/2011)


    We cannot make use of xp_cmdshell. It has been deleted from our extended procedures list for security reasons.

    This is the error I get:

    HResult 0x2, Level 16, State 1

    Named Pipes Provider: Could not open a connection to SQL Server [2].

    Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections..

    Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.

    I don't understand why some org decided that xp_cmdshell is a security risk. Everything is a security risk, it is how you manage it.

    in any case, i have not encounter your issue but hopefully the following two articles can help:

    http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/

    http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/5d9e84c2-1c05-449a-aae5-503e2ad6f9ca/

    -----------------------------
    www.cbtr.net
    .: SQL Backup Admin Tool[/url] :.

  • Thanks.

    I'll look into it

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

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