Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQLCMD mode in Query Window using variables Expand / Collapse
Author
Message
Posted Wednesday, January 26, 2011 12:01 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 26, 2011 12:09 PM
Points: 278, 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?



Post #1054126
Posted Friday, January 28, 2011 12:31 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 16, 2013 6:59 PM
Points: 168, Visits: 350
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 :.
Post #1055124
Posted Friday, January 28, 2011 8:58 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 26, 2011 12:09 PM
Points: 278, 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.



Post #1055379
Posted Friday, January 28, 2011 9:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 16, 2013 6:59 PM
Points: 168, Visits: 350
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 :.
Post #1055394
Posted Friday, January 28, 2011 9:30 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 26, 2011 12:09 PM
Points: 278, 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.



Post #1055403
Posted Friday, January 28, 2011 10:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 16, 2013 6:59 PM
Points: 168, Visits: 350
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 :.
Post #1055456
Posted Friday, January 28, 2011 11:32 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 26, 2011 12:09 PM
Points: 278, Visits: 24
Thanks.
I'll look into it



Post #1055508
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse