Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 10832

SQLCMD is Microsoft SQL Server's command line utility that allows you execute T-SQL commands from a command prompt.

3 votes
Accepted

Messages in SQLCMD script output are not in order

PRINT output can be buffered. A great way around this is to use RAISERROR (@msg, 0, 1) WITH NOWAIT; instead. For example: DECLARE @msg nvarchar(1000); SET @msg = 'Do the first thing'; RAISERROR (@ …
Hannah Vernon's user avatar
  • 70.5k
1 vote
Accepted

How to automatically fix Orphaned Users with PowerShell and SQLCMD

SELECT will generate a resultset that sqlcmd will print to the screen along with header info. The dashes are part of this header info. … $RepairOrphansSQL actually contains any commands before running it; if the variable is empty, sqlcmd is not called. …
Hannah Vernon's user avatar
  • 70.5k
0 votes

SQLCMD Using Powershell Gives No Response

sqlcmd -? via PowerShell should return version information and usage guidelines, as in: Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. … Try the new cross-platform PowerShell https://aka.ms/pscore6 PS C:\Users\xxx> sqlcmd -? Microsoft (R) SQL Server Command Line Tool Version 15.0.4123.1 NT Copyright (C) 2019 Microsoft Corporation. …
Hannah Vernon's user avatar
  • 70.5k
3 votes
Accepted

SQLCMD :connect directive - how can I specify connection protocol?

If you must specify the port, do so like this: sqlcmd.exe -S tcp:myserver,1433 By the way, documentation for SQLCMD is available directly from Microsoft, where it states the -S parameter syntax as: … -S [protocol:]server[instance_name][,port] SQLCMD Mode in SQL Server Management Studio accepts precisely the same syntax. …
Hannah Vernon's user avatar
  • 70.5k
1 vote

What methodology is being used to poll SQL instances when using sqlcmd with the -L switch?

SQL Server discovery is attempted using UDP port 1434. You'd need to get UDP traffic destined for 255.255.255.255 on port 1434 opened in your local router to see servers from other subnets. See ht …
Hannah Vernon's user avatar
  • 70.5k
1 vote

sqlcmd returning literal 'NULL' for null columns when outputting to CSV

\sqlcmd -S "My2008R2Instance" -E -d tempdb -Q "set nocount on; create table #t (col1 varchar(10) null, col2 varchar(10) null); insert into #t(col1, col2) values (null, null); select coalesce( …
Hannah Vernon's user avatar
  • 70.5k
3 votes
Accepted

In SSMS, are IntelliSense and SQLCMD mode mutually exclusive?

When running in SQLCMD mode, SSMS doesn't even attempt to obtain the Intellisense data. … If you disable SQLCMD mode via the menu on a session that is already connected, while the TSQL XEvent Profiler is running, you'll see SSMS query the system schema for the relevant info. …
Hannah Vernon's user avatar
  • 70.5k
12 votes

How to make sqlcmd return an ERRORLEVEL other than 0 when the .sql script fails?

From the MSDN SQLCMD Utility page at: http://msdn.microsoft.com/en-us/library/ms162773.aspx If RAISERROR is used within a sqlcmd script and a state of 127 is raised, sqlcmd will quit and return the message … @echo off "C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd" -S "someinstance" -E -i . …
Hannah Vernon's user avatar
  • 70.5k
3 votes

exporting a sql view to a csv file

if you enable xp_cmdshell, you can run it via the command-line using this code inside a SQL Server Agent Job (or query window in SQL Server Management Studio): DECLARE @cmd varchar(1000); SET @cmd = 'sqlcmd
Hannah Vernon's user avatar
  • 70.5k