Questions tagged [sqlcmd]

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

Filter by
Sorted by
Tagged with
1
vote
3answers
26 views

Invoke-SQLCmd Date output format

I’m having a problem with a result set that I am getting from a stored procedure when running a PowerShell script (Using Invoke-SQLCmd) to get the results and send the output to a .csv file. The date ...
3
votes
2answers
117 views

Suppress database change message in SQLCMD.exe

I am writing a script generation script. The script is generating list of DDL statments. The util_generator.sql generation starts with database connection: USE [TARGET_DATABASE] GO ... Many queries ...
2
votes
1answer
922 views

SQLCMD -L Returns Bizarre Response Instead of Server List

I have a server with three installed SQL Server 2014 instances with Service Pack 1 (build number is 12.0.4213.0) installed. When I run SQLCMD -L on the server I am getting the bizarre response below. ...
1
vote
1answer
38 views

Session ID in SQLCMD running multi-batch script

My script contains multiple GO keywords. I know that SQLCMD interprets GO as a batch delimiter, so the code is not run as a single block but instead each part between the delimiters (a batch) is run ...
3
votes
3answers
24k views

Pass parameters to a procedure with SQLcmd

I use this SQL command: sqlcmd -v first_date="30/09/2015" last_date="15/09/2016" And I would like to pass that parameters to a stored procedure: ALTER PROCEDURE [dbo].[_testProcedure] WITH ...
7
votes
3answers
28k views

How to connect to SQL Server using sqlcmd on Linux?

I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name. The ...
1
vote
1answer
451 views

Update mysql DB when data has a single quote with Dynamic values

I am writing data to mysql DB from my linux machine using sqlcmd. My script is updating the data Dynamically based on the employee number. I have a situation where the the CN attribute might have ...
1
vote
1answer
55 views

Restore ALL databases with sqlcmd in SQL Server (Express, Docker, 2017)

These documentation articles https://docs.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-2017 and https://docs.microsoft.com/en-us/sql/linux/sql-...
1
vote
3answers
1k views

help in connecting the sql server 2017 developer edition

I have installed the sql server 2017 for non production environment in my PC. It has installed successfully. when connect using sqlcmd it was throwing an error login failed for login sa. it didn't ...
4
votes
0answers
3k views

Can not connect to SQL Server from Ubuntu sqlcmd client

I'm trying to connect to a SQL Server Express server from a Linux Ubuntu 16.04 computer. I Installed all SQL commad line utilities for the linux machine and follow instructions from Microsoft SQL ...
0
votes
1answer
473 views

SQLCMD does not prints Rows affected in log file

I am calling stored procedure using SQLCMD which will insert data into multiple tables. And output of that procedure is captured in log file using -o. But, i can't see Row(s) affected any where in the ...
0
votes
3answers
84 views

Check if a SQL Login exists without SA

How can I check if a SQL Login exists for an arbitrary domain user if the account I'm running doesn't have SA rights to the instance (but will be dbo on at least one DB) Ideally I'd like the solution ...
17
votes
2answers
23k views

Can I enable SQLCMD mode from inside a script?

Is there something like a directive which I can use in a script to force SSMS to enable/disable SQLCMD mode?
1
vote
1answer
2k views

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

I'm using sqlcmd (SQL Server 2008 R2) to execute a simple query to output data to a .csv file. In the SQL file, I have the following code: USE [OSCAR_UK] SELECT DISTINCT ISNULL(LTRIM(RTRIM(il.[...
1
vote
1answer
101 views

exporting a sql view to a csv file

Whilst I can just about script to pull data via queries, I'm finding it very difficult to figure out how to export the results of a query to a .csv file and save in a network storage location (novice ...
1
vote
2answers
2k views

SQLCMD: “Login failed for user” on SQL Azure, but it works with SSMS

I have an SQL Azure database, and I'm able to connect to it using SQL Server Management Studio. When connecting to it, it displays myServer.database.windows.net (SQL Server 12.0.2000.8 - myUser) ...
1
vote
1answer
104 views

Verify failed SQL Server connections to instances on a given server and email them

I am trying to monitor the SQL Server instances on standalone server / cluster server. I would like to setup something from commandline to check for connectivity to each SQL Server instance on the ...
5
votes
1answer
241 views

How to Natively Return Output when using Invoke-Sqlcmd?

How do I natively log output from the PowerShell Invoke-Sqlcmd? I am using Invoke-Sqlcmd to loop through a large (50,000-90,000 SQL files w/ 1-10 inserts in each) set of small SQL insert statements. ...
0
votes
2answers
97 views

Are the switches for the SQLCMD command case sensitive?

I have MS SQL Server 2014 Management Studio installed on two computers, both of which run on Windows 10. The switches for SQLCMD command are case-sensitive on one computer, but it is not on the other ...
2
votes
1answer
151 views

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

In a script I have the directive :connect DbServer and it seems that if I run this script in SQLCMD (like this: sqlcmd -i script.sql then sqlcmd attempts to connect using Named Pipes (which doesn'...
0
votes
1answer
145 views

SQLCMD cannot connect my named instance

I am using SQL Server 2017 based on Windows server 2016. I am trying to connect my named instance, but I got the error message as below screenshot: So I tried to reinstall ODBC Driver 13. But I got ...
0
votes
1answer
438 views

Msg 102, Level 15, State 1, Server DESKTOP-U368A1B, Line 1 Incorrect syntax near '1'

I get the above error message when i run the following perl script from the cmd, that is supposed to do some transaction in my database. #!/usr/bin/perl. use warnings; use strict; my ( $pid1, $...
3
votes
2answers
3k views

How can I output more than 256 characters to a file?

I am using the following command to write the output from a stored procedure to a file for further processing: DECLARE @shellCommand VARCHAR(2000) SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -...
3
votes
1answer
251 views

How do I force windows to use a specific version of SQLCMD?

I have several versions of SSMS installed on my machine. We have legacy servers running several versions of SQL Server, mainly 2008, 2012, and 2014. I use SQLCMD all the time, but it seems to default ...
1
vote
1answer
508 views

How to format the output of SQL queries on SQLCMD for SQL Server 2017 on RHEL Linux 7.3?

I have installed SQL Server 2017 on RHEL Linux 7.3, and using SQLCMD utility for querying the SQL Server. When I try to query for example: 'SELECT * FROM SYS.DATABASES'. I get output like the ...
0
votes
0answers
312 views

Easiest way to update XML inside of a SQL table column?

I have an SQL DB, Directory. It contains a table, dbo.User. Within that table there is a column, Info. The Info column contains ~1000 lines of xml tags. Well I want to modify only one of those tags, ...
0
votes
2answers
208 views

How to report stats on restore via sqlcmd?

I've inherited a batch file script that calls sqlcmd with parameters. One of the parameters is the path to a SQL file. That file includes a database restore command. The command in the SQL file ...
3
votes
1answer
220 views

SSMS generated script failing to execute

So I am in a situation where I need to import a small DB (under 1GB) from a newer version of SQL Server (I am restoring to a 2014 SQL Server from a 2016 SQL server), without a direct connection ...
1
vote
1answer
859 views

How to setup db credentials for `sqlcmd` using a config file?

When I'm using mysql client, I can configure my db credentials in ~/.my.ini. How can I achieve similar thing with sqlcmd on Linux? Where and what filename I should create? My goal is to avoid keep ...
2
votes
2answers
324 views

How do I install SQL-Server Tools if I can't use curl?

Attempting to install mssql-tools, I get $ sudo apt-get install mssql-tools Reading package lists... Done Building dependency tree... 50% Building dependency tree Reading state information......
4
votes
1answer
2k views

What are valid connection strings for SSMS login box?

What are valid connection strings for SSMS login box? Every once in a while I will have to connect to an instance of SQL Server with some specific configuration setting or in a DMZ where no host ...
-1
votes
1answer
87 views

How to update the first row in the exported query from sqlcmd

I am trying to find an easy way of updating the first row in the export of a sqlcmd query to CT_EVENT,,,,,,,,,,,,,,,,,,,,,,,,. I found this article in stack overflow which builds the csv format. I've ...
0
votes
1answer
543 views

Permission issue on restore of the db from primary to secondary [duplicate]

while restoring the User DB from primary to secondary replica through query in cmd prompt and got following error message Msg 262, Level 14, State 1, CREATE DATABASE permission denied in database '...
2
votes
2answers
169 views

Sqlcmd: 'KReadOnly': Unknown Option

I am using SQL Server 2014 SP2 with AG, running this command: sqlcmd -S ABC-ISELL -E -d ABP_SFA_BMB -K ReadOnly Where ABC-ISELL is the listener name, I am getting this error: Sqlcmd: 'KReadOnly': ...
5
votes
3answers
2k views

sqlcmd :out not working as expected?

Consider the following code: DECLARE @db sysname; DECLARE @filename varchar(260); DECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY FOR SELECT d.name FROM sys.databases d; OPEN cur; FETCH NEXT ...
1
vote
1answer
3k views

Ola Hallengren's Backup solution error message after moving files using cmdexec stops job, this disagrees with faq

I am using Ola Hallengren's Maintenance script, and I am having an issue which seems documented in the FAQ. For context, there was an issue where the existing SQL Server backups had to be moved to ...
5
votes
2answers
1k views

SQL Server 2016 SQL Agent Token Not Working

I have this in scores of SQL Agent Job steps throughout my enterprise and it works as expected: sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE MyStoredProc etc..." But on my new SQL ...
4
votes
3answers
1k views

large insert script (generated from tasks) - errors when running from sqlcmd

I have a large generated .sql file for the data from our UAT database. I'm trying to load that into my (localdb)\v11.0 using sqlcmd. I was able to use the following: sqlcmd -S (localdb)\v11.0 -d ...
0
votes
1answer
1k views

How To Import Binary Dump File To SQL Server

I am trying to import a dump file which is in binary format to SQL Server using sqlcmd command. After running the import command, I am getting this error, Msg 2812, Level 16, State 62 I'm suspecting ...
1
vote
0answers
477 views

Unable to access an SQL Server 2016 named instance locally via sqlcmd when only using @@SERVERNAME value

I have a server with two instances of SQL Server 2016 SP1 CU7 running, one is the default instance (on a custom port, 9433) and the second is a named instance (on another custom port, 9434). I've ...
5
votes
1answer
10k views

How to output more than 4000 characters in sqlcmd

I've made a stored procedure restoredatabase that returns code to restore a database. The output script looks like this: restore database [DB] from disk='F:\FULL1.bak' with norecovery restore ...
0
votes
2answers
472 views

String replacement in SQLCMD

I'm trying to set up a test environment that simulates (very roughly) having three servers. I'm doing this by creating 3 databases on my local instance of SQL which I'll call [A], [B], and [C]. Why I ...
0
votes
1answer
207 views

sqlcmd gives no sign of failure yet doesn't work

I am running sqlcmd from a linux bash script. My target server is an Azure SQL. I execute an sql file with a lot of sql statements (around 2500). These are Insert/update/delete statements. I used the ...
0
votes
1answer
265 views

Import database from MSSQL export dump on Ubuntu localhost

I have an export file from an online database server that I need to use to generate the database locally on my system. I'm running MSSQL 2017 on Ubuntu 16.04. The export file has Program(application/...
0
votes
1answer
405 views

SQLCMD to connect to Linked server

I'm getting a login error with the below sqlcmd to connect to a linked server: SQLCMD -E -dlinkedserver -ig:\temp\script.sql -o g:\temp\test.txt My linked server which is an oracle connects through ...
2
votes
1answer
588 views

sqlcmd returning NUL instead of NULL for tinyint column

sqlcmd -E -d MyDatabase -Q "sp_helpsubscription" -s "," -o file.csv -W -u Column 23 which is suppose to be a tinyint (subscriber_type) is output as NUL instead of NULL when the value is NULL. I ...
4
votes
3answers
25k views

Can I get OSQL if I install SQL Server Client Tools?

We have some legacy .bat scripts full of osql commands to existing SQL Server instances / dbs on our network. We've tried running the .bat scripts from our app server (Win Server 2008) but get the ...
7
votes
5answers
9k views

How to display MS SQL results vertically?

Here is what I've got currently when executing queries in sqlcmd: 1> SELECT 1,2,3,4 2> GO ----------- ----------- ----------- ----------- 1 2 3 4 (1 ...
6
votes
3answers
2k views

Are there any way to stop typing 'go' in sqlcmd?

If I remember correctly, last time Microsoft Sqlcmd require each command to end with ';' and hit Enter key to execute the command. However, currently, we need to type the GO each time to execute the ...
0
votes
1answer
160 views

How can I increase the length of the string returned by object_definition in sqlcmd?

I have an sql script that uses select object_defintion(object_id(...)) to extract the definition from a view. This script is run with sqlcmd: sqlcmd -i extract.sql -o %oudir%\extracted.dat. ...