SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQLCMD mode in Query Window using variables


SQLCMD mode in Query Window using variables

Author
Message
despag
despag
Old Hand
Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)

Group: General Forum Members
Points: 368 Visits: 24
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?



Red8Rain
Red8Rain
Right there with Babe
Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)

Group: General Forum Members
Points: 750 Visits: 372
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 :.
despag
despag
Old Hand
Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)

Group: General Forum Members
Points: 368 Visits: 24
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.



Red8Rain
Red8Rain
Right there with Babe
Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)

Group: General Forum Members
Points: 750 Visits: 372
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 :.
despag
despag
Old Hand
Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)

Group: General Forum Members
Points: 368 Visits: 24
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.



Red8Rain
Red8Rain
Right there with Babe
Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)Right there with Babe (750 reputation)

Group: General Forum Members
Points: 750 Visits: 372
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 :.
despag
despag
Old Hand
Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)Old Hand (368 reputation)

Group: General Forum Members
Points: 368 Visits: 24
Thanks.
I'll look into it



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search