Scheduled SQL Server Data Export to CSV using SQLCMD - sql-server-2005

This is my command line:
sqlcmd -S DEVSERVER\SQLEXPRESS -i c:\SQL_Query.sql -o c:\CSV_Output.csv -s”,”
And my sql script:
SELECT * FROM dbo.pay
The file with this error message:
Msg 208, Level 16, State 1, Server DEVSERVER\SQLEXPRESS, Line 1
Invalid object name 'dbo.pay'.

Specify database name.
sqlcmd -S [sql server name] –d [database name] –U [user name] –P [password] –Q "your sql query" –s "," –o "D:\demo.csv"

Related

Error while trying to import .sql file in SSMS

I'm trying to import an sql file of size 3.2GB. So definitely I would need to do it using cmdsql.
But the problem is that the server I'm trying to connect with in SSMS has no password and that is giving me the error.
I'm usig the following command in cmd:
sqlcmd -S DESKTOP-7QKBPG7\SQLEXPRESS -d XVMS-4.6 -U CSNA\kamran.bajwa -P -i C:\Users\kamran.bajwa\Downloads\XVMS\SCHEMA-DATA-16-06-2022\SCHEMA-DATA.sql -o C:\Users\kamran.bajwa\Downloads\XVMS\SCHEMA-DATA-16-06-2022\logs.txt
As of the"-P" there is no password so if I use an empty string "" then it gives me the following error in the log file:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'CSNA\kamran.bajwa'..

SQL Server : batch with specific characters of column

I wrote a batch script with SQL statements in order to export data to a .csv file:
sqlcmd -S DBServer -U User -P Password -d DBName -s","
-Q "SET NOCOUNT on;SELECT <=0.1%, (0.1%,0.5%] FROM t" -o D:\output.csv ;
But the specific characters in column name <=0.1%、(0.1%,0.5%] make the batch file is not working.
What should be the correct approach for this select statement?
Any help will be much appreciated.

How do I open an sql database that I downloaded using the terminal?

How can I import a database with MySQL from terminal?
the syntax from this thread
mysql -u username -ppassword databasename < filename.sql
my input for the file (lahman591) i placed on my root for simplicity sake
mysql> mysql -u root -p lahman591 <c:\lahman591.sql
i get an Error:
ERROR 1064 (42000) You have an error in your sql syntax.
You need to do it from your normal shell (the command is calling the mysql bin), not mysql shell.

How to export query results to csv in Microsoft SQL Server Management Studio?

Trying to export custom query to csv file I wrote the following command:
sqlcmd [-S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,total_events_count ,event_duration FROM dbo.event_daily_stats ORDER BY column1" -o "D:\MyData.csv" -h-1 -s"," -w 700]
but it returned the following error message:
The identifier that starts with '-S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,' is too long. Maximum length is 128.
Does anyone know how this issue could be solved?
Thank you!
I executed the command without "[" and "]" with no problem, have you tried in this way?
sqlcmd -S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,total_events_count ,event_duration FROM dbo.event_daily_stats ORDER BY column1" -o "D:\MyData.csv" -h-1 -s"," -w 700
I think the problem is that you're trying to run this inside of SSMS when it should be run at a command prompt instead.

What is wrong with my SQL command?

I am trying to put the query result into a PowerPoint file like the below
EXEC XP_CMDSHELL 'SQLCMD -Q "Select * From test_tbl" -O "E:test_tbl.ppt"'
but it is not working.
Below is the summary that I am getting
**output**
Sqlcmd: Warning: '-O' is an obsolete option and is ignored.
Msg 208, Level 16, State 1, Server SUMERU-49484E24, Line 1
Invalid object name 'TableName'.
NULL
Before executing this query I have already enabled the xp_cmdshell by using sp_configure.
Also I have the .ppt file in the proper location.
I have doing this just for my own sake of experiment.
Kindly help me in executing the same.
Using SQL Server 2005.
Edit:
I have changed the query like the below
EXEC XP_CMDSHELL 'SQLCMD -Q "Select * From test_tbl" -s "SUMERU-49484E24\SQLEXPRESS2008" -d "test" -E -o "D:t2.ppt"'
But the below is the error in the pptfile
HResult 0xFDC, Level 11, State 1
Cannot open database requested in login 'test'. Login fails.
HResult 0x4818, Level 14, State 1
Login failed for user 'NT AUTHORITY\SYSTEM'.
HResult 0x4, Level 16, State 1
Shared Memory Provider: I/O Error detected in read/write operation [4].
Sqlcmd: Error: Microsoft SQL Native Client : Communication link failure.
Thanks
I see a few problems:
the parameter for sqlcmd to define the output file is -o (lowercase) - not the -O (uppercase) you're using
you're not defining what server and database to connect to - you need to add -S (servername) and -d (database name) to your SQLCMD command line
you're not defining how to connect security-wise - either supply -E for a trusted connection (integrated Windows security) or define user and password using -U (login) and -P (password)