Export a csv using xp_cmdshell - sql-server

I'm trying export my results using xp_cmdshell but I recive this error :
"Sqlcmd: error: error while opening or using file (my_path)".
This is what I've wrote on sqlserver:
exec xp_cmdshell 'sqlcmd -S my_server -d my_db -E -Q "my_query" -s "," -o "my_path"'.
I've tried using command line and on it my instructions works.
I think its can be an permissions trouble but I'm new on this type of things and I can't solve by myself. I hope that some of you can help me, thanks

Related

ERROR: syntax error at or near "psql" when importing database

I am trying to import a .sql file into my new created database. I used
docker exec -it <idOfContainer> psql -U <user> <database_name>
to specify the database I want to use. Then I tried to import the .sql file to the database using the command
psql <databasename> -f "<path/to/file.sql>";
but this error appears
ERROR: syntax error at or near "psql"
LINE 1: psql <databasename> -f "<path/to/file.sql>";
^
I am new in using postgres and databases, so no idea what to do, thanks for the help!
Your docker exec is wrong, you only need the '-i'
The -it executes tty which will get you to run into your container.
Importing databases can be done outside the container.
cat database.sql | docker exec -i <dockercontainername> psql -U <user> <databasename>

Why is sqlcmd not importing anything?

I'm trying to restore a 500MB .sql file to SQL Server; I've tried running the script directly from a query page, but the memory isn't enough, so I tried with the sqlcmd command as follows:
sqlcmd -S <my server name> -d <my dbname>-U <user> -P <password> -i <pathfile> -a 32767
I've also tried with the -o command, too see if the log would report something useful, but from that command I didn't get any error.
Still, my database in SQL Server is empty, without any single table.
I'm running this on a Windows 10 system.
Any suggestions?
If you are using a SQL instance then the syntax will be like this:
sqlcmd -S <my server name>\instance -d <my dbname> -U <user> -P <password> -i <pathfile> -a 32767

SQL Server login issue with sqlcmd

I am trying to run a file in Ubuntu using the sqlcmd command as shown below. We have a complex password set as per standards. Using that I am getting an error saying that "Login failed for the user "username" ".
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P ${sqlpassword}
-I -i ${inputfile} -s"|" -r1 1>${logfile} 2>${errorfile}
I tried to execute the same by providing values with password in single quotes. It worked fine
As we cannot hard code the password. I tried the things shown here, but those didn't work.
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P '${sqlpassword}'
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P "${sqlpassword}"
sqlcmd -S ${servername} -d ${dbname} -U ${sqlusername} -P \'${sqlpassword}\'
Can someone please help me - am I doing something wrong?
Thank for #Larnu's comment.
The error caused by your password has has dollar sign in it.

Docker: launch a mssql script outside of the docker image

I've been trying to run an MSSQL script on my docker and it always fails... is my syntax wrong or something... I've looked this post but it doesn't work
Executing SQL scripts on docker container
docker exec mssql '/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql'
this returned an error
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ************** -q </CRE.sql\": stat /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P AucMa1633485 -q </CRE.sql: no such file or directory"
I also tried something like this...
sudo docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'gitgood12345' -q </CRE.sql
The command returns me this error, which I'm not sure why....
Sqlcmd: '-q': Missing argument. Enter '-?' for help.
Please help me... the second method used to work, I think I forget a parameter or something...
The sqlcmd -q option requires an argument of the command you want to run. As your example is piping commands in on standard input, try without the -q.
docker exec mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 < /CRE.sql
Quoting everything results in the container trying to execute a binary named "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql" which doesn't exist.

How to run a .sql file through query in SQL Server 2008

I have a stored procedure through which I am creating a dynamic .sql file on the server and the file have successfully generated. Now I want o execute the same file through query in the same stored procedure .
I have go through the links and found the below lines useful
osql -D db_name -S server_name -U username -P password -i sqlfile
or
sqlcmd -D db_name -S server_name -U username -P password -i sqlfile
I have tried to implement the above but it is giving me the error of
osql is not a recognized option
Please tell me the correct way. Thanks
sqlcmd is a command line tool and cannot be called directly from inside a stored procedure.
Use xp_cmdshell to execute it:
EXEC master.dbo.xp_cmdshell ‘sqlcmd -D db_name -S server_name -U username -P password -i sqlfile’

Resources