Questions tagged [sqlcmd]
SQLCMD is Microsoft SQL Server's command line utility that allows you execute T-SQL commands from a command prompt.
181 questions
0
votes
1
answer
177
views
No logins showing under SQL Instance as admin and SA
I have an issue where when I log into SSMS I am not seeing any logins.
I am running as an administrator and logging in with SA.
It does not matter which account I log in with. I only ever see the two ...
0
votes
1
answer
80
views
Sqlcmd: The -W and the -y/-Y options are mutually exclusive
I am working on SQL server task. I need a sqlcmd result file with complete records with headers.
Here is the command and error message below
sqlcmd -S [Server_Name] -U [User_Name] -P [Password] -d [...
1
vote
0
answers
105
views
SQL script to run from batch jobs
I have a sql script that truncates the table and insert into database from a linked server.Since the server is SQL express I will need to use batch jobs to schedule the jobs. Below is a snippet of the ...
0
votes
0
answers
281
views
SQLCMD - Access is Denied After Running Script
I'm working with a vendor about this, but a supplied script that they have to run to upgrade their software, after running, ruins access to SQLCMD.
Before running the script I can open a Command ...
0
votes
1
answer
862
views
"Fatal scripting error" when parsing SQLCMD query
Similar issue seen here.
I am trying to run the following command from SSMS running in SQLCMD mode.
:r C:\dev\test scripts\sample.sql
But I get the following error:
A fatal scripting error occurred.
...
0
votes
1
answer
217
views
Neatly print sp_help output similar to psql \d+
I've just been granted access to a SQL Server data warehouse and I'm trying to get to grips with sqlcmd. Coming from a Postgres/psql background, I'm used to being able to quickly look up a table ...
1
vote
0
answers
741
views
What is the relationship between sqlcmd and ODBC driver
I'm trying to install sql server 2022 on a build machine, and then test that I can connect to that server with sqlcmd. I previously had ODBC driver 17 and was getting errors from that command, so I ...
0
votes
1
answer
543
views
Failed to initialize sqlcmd library with error number -2147467259
This is my code:
SET @msg = 'Customer Voice replies.';
SET @query=
'SELECT [column1],[column2] FROM [StagingMX].[dbo].[_CVA]
WHERE DATEDIFF(day, [Completion time] ,GETDATE()) = 1
AND [...
0
votes
1
answer
160
views
Backup database on local pc
I am using this script to backup database on computer A:
sqlcmd -U MyDatabase -P MyPassword -S .\SQLEXPRESS -Q "EXEC sp_BackupDatabases @backupLocation = 'c:\shared', @databaseName='MyDatabase', @...
2
votes
2
answers
765
views
SQLCMD fails if -c; is specified in parameters on SQL Server 2019
The following sqlcmd fails with no error and no output, just returns silently.
SQLCMD -U dbuser -d dbname -w 255 -h-1 -P mypassword -S dbserver -Q "ALTER TABLE xyz ADD abcd NUMERIC(1)" -c;
...
0
votes
1
answer
159
views
Restoring SQL Server database on Windows instance from Linux SQLCMD
I have a SQL Server that is running under Windows. And I have a Linux client. AFAIK SQLCMD requires "local" (server-related) path to backup files. So the question is - how to specify windows ...
4
votes
2
answers
1k
views
Suppressing SQL Server Management Studio's 'Messages' from output of SQLCMD via SQL Server Agent
I've created a script that outputs data in an XML format, and was using SQL Server Agent (Job Step of Type 'Operating System (cmdexec)') to run a job monthly/quarterly to output the data to a specific ...
0
votes
2
answers
3k
views
Which install kit installs sqlcmd.exe?
I recently had to uninstall Visual Studio 2012 and DTS, then install VS 2017 and DTS again. Afterwards, The directory containing sqlcmd.exe was not in my PATH variable. I installed the following.
...
0
votes
1
answer
284
views
How do I stop SQLCMD from misinterpreting variable parameters?
I have a server config script we call with SQLCMD that runs a lot of other scripts in a folder, using :R \\folder\path\script.sql.
One of those scripts creates an Agent job that has a TSQL step that ...
0
votes
0
answers
2k
views
Comparing DATETIME value with the one in previous row and selecting it if its bigger by at least 5 minutes
I'm trying to write a query to select all rows in a table where the timestamp column's value is bigger by at least 5 minutes than the value in the previous row. My main problem is trying to get ...
0
votes
1
answer
684
views
SQLCMD :CONNECT with a specific port?
In SSMS with TSQL is it possible to do a SQLCMD :CONNECT to a SQL Server with a specified port?
Something like this:
:CONNECT MySQLServer, 49603
I know there is the -S switch for SQLCMD in the ...
2
votes
2
answers
460
views
In SSMS, are IntelliSense and SQLCMD mode mutually exclusive?
In SSMS, are IntelliSense and SQLCMD mode mutually exclusive?
Is there any way to make IntelliSense work when SQLCMD mode is enabled? (I use SQLCMD a lot)
I'm trying to understand why IntelliSense ...
1
vote
1
answer
821
views
SQLCMD Using Powershell Gives No Response
I have SQL Server 2019 Standard installed on my Server 2019. I also have installed the SQL CMD Line tools V21.1.18246. When I open my PowerShell in Administrator mode and do a sqlcmd -? the command ...
0
votes
1
answer
333
views
sqlcmd is running but database shown no running query
We are facing this weird issue on our Azure setup. The database is SQLServer and client is CentOS.
We fire queries using sqlcmd. Sometimes it happens that you see the sqlcmd running, but there is no ...
-1
votes
1
answer
757
views
How to not include the query in sqlcmd output file [closed]
I have a batch script that I am using to generate a dataset from a stored procedure but it keeps including the query that calls the stored procedure in the output file and I need it to stop.
The batch ...
0
votes
1
answer
3k
views
Does SQLCMD "!!" command work in Azure Data Studio
In SQL Server Management Studio, it is possible to run batch commands when in SQLCMD mode, using the !! command. However, in Azure Data Studio, this does not seem to work (SQLCMD has been available in ...
0
votes
1
answer
356
views
Dumping mssql db from one server and restoring on another on Mac/Linux
Using either MacOS or Linux, my objective is to copy a database from one MSSQL database server to another. The source database server is remote, while the target database server (which will be ...
1
vote
1
answer
6k
views
In mssql 2019 where sqlcmd utility is?
I am working on the tool that will silently deploy SQL server instance and I need to run a few scripts after installation, but I struggle to find where SQLCMD utility is.
For example, I use this:
...
4
votes
2
answers
3k
views
using SQLCMD to export/extract data withot trailing spaces
I am trying to extract data from some of my tables using sqlcmd utility as SSMS does not generate appropriate csv-s for me.
My issue: all csv extract includes trailing spaces according to table ...
1
vote
1
answer
458
views
Lost SQL credentials and unable to connect to SQL via SQLCMD
I have a machine running SQL 2014 but have neither the windows authentication or sa authentication details due to mismanagement of a previous support team.
Having tried logging in to SQL Management ...
0
votes
2
answers
5k
views
How to find a list of local MS SQL Servers?
I looked at How can I determine installed SQL Server instances and their versions? already but CMD returns nothing:
C:\Windows\system32>sqlcmd -L
C:\Windows\system32>
But when using Visual ...
0
votes
1
answer
212
views
sqlcmd returns Msg 701 when importing +10MB queries
I have a powershell script that exports data from DBF files, saves them as .SQL files and then imports the SQL files into MS SQL 2016 using the sqlcmd command.
There's one SQL file per table in my ...
0
votes
0
answers
3k
views
Remove (XX.. rows affected) from SQLCMD output to file [duplicate]
Using SQLCMD with -o to file.
How to get rid of the (XX... rows affected) message at the end for a clean output?
0
votes
1
answer
4k
views
Encrypt connection when using sqlcmd
Trying to connect database with an encrypted connection, tried below sql queries:
sqlcmd -N -E -S tcp:SQLSERVER,1234 -U username -P password -d Database -Q "INSERT INTO table1 VALUES(1,2,2)
ERROR:...
2
votes
2
answers
1k
views
:!!echo command not working from sqlcmd
I have a sql script file which is executed from a batch file and is supposed to execute some sql scripts and write errors and output to files. Here is an example of how the script file looks like:
:...
0
votes
2
answers
756
views
Copy last modified files from network path into SQL server box and rename them using Agent job
I have to copy latest backup from a network path and bring that to my SQL server physical drive for restore, I have to do that using agent job.
This is what i am doing.
Created a SQL server agent ...
2
votes
1
answer
206
views
SQLCMD external editor
I have set the environment variable SQLCMDEDITOR=gvim to invoke an vim with the ED command of SQLCMD. From my old Windows 7 computer, it used to work.
I have now a new laptop (windows 10) and, when ...
3
votes
2
answers
16k
views
How to print results in SQLcmd window
I'tm trying to show in SQLcmd window the results of these queries:
SELECT SYSDATETIME() AS "date";
PRINT 'EXTRAÍDO POR:'
SELECT SUSER_NAME() AS "Owner"
PRINT 'PASSWORD SETTINGS';
exec ...
1
vote
1
answer
88
views
Why do I need DAC if I am able to connect to the server via sqlcmd?
DAC attracted my attention at the moment when I was unable to connect to the SQL Server using a traditional way (the reason was deleting logon trigger). I made some searches on the web and it turned ...
2
votes
1
answer
5k
views
Linux sqlcmd connecting to Windows Sql Server using kerberos
I'm pretty sure I am going to have to expand upon this a bit as I am new, and my question is probably not clear.
I have an Active Directory environment that includes a Microsoft SQL Server running ...
1
vote
2
answers
2k
views
How to create a csv with SQLCMD which can be imported into another database system
I may be doing this all the wrong way so please point me in the right direction if I am missing something.
I have an Ubuntu server with SQLCMD installed and a Windows Server with a MSSQL database.
I ...
1
vote
3
answers
4k
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
2
answers
2k
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
1
answer
452
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 ...
1
vote
1
answer
244
views
Restore ALL databases with sqlcmd in SQL Server (Express, Docker, 2017)
These documentation articles
https://learn.microsoft.com/en-us/sql/linux/tutorial-restore-backup-in-sql-server-container?view=sql-server-2017
and
https://learn.microsoft.com/en-us/sql/linux/sql-...
0
votes
3
answers
3k
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 ...
1
vote
1
answer
5k
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 ...
5
votes
1
answer
8k
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
2
answers
814
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
1
answer
740
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
1
answer
1k
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
1
answer
2k
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
1
answer
3k
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 ...
3
votes
1
answer
7k
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 ...
4
votes
1
answer
3k
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 ...