HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Problems with sqlcmd -q and quotations marks

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
withquotationsmarkssqlcmdandproblems

Problem

I have a SQL Server 2008 R2 Express edition. Because it is only an Express edition I don't have a SQL Server Agent for automation of tasks.

So I tried to do something with command line program sqlcmd.

I try to execute this command:

use DB; EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)";


If I do this in SQL Server Management Studio everything works fine. But if I try to execute this via sqlcmd it doesn't work.

sqlcmd -S .\INSTANCE -Q "use DB; EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)";" -o C:\FOLDER\LOG_DB_REINDEX.txt


I think this is because of the quotation marks. I also tried to use ` and ' but I didn't get it to work...

How can I resolve this issue?

Solution

Try to use this. ;-)

use DB; EXEC sp_MSforeachtable @command1=N'print ''?'' DBCC DBREINDEX (''?'', '' '', 80) ';


Double quotation marks will escape the following quotation mark.

Here an example of sqlcmd:

sqlcmd -S YOURSERVER -Q "use net_temp_test; EXEC sp_MSforeachtable @command1=N'print ''?''';"

Code Snippets

use DB; EXEC sp_MSforeachtable @command1=N'print ''?'' DBCC DBREINDEX (''?'', '' '', 80) ';
sqlcmd -S YOURSERVER -Q "use net_temp_test; EXEC sp_MSforeachtable @command1=N'print ''?''';"

Context

StackExchange Database Administrators Q#104300, answer score: 4

Revisions (0)

No revisions yet.