Export data with bcp in SQL Server produces nothing - sql

I am exporting data in SQL Server 2005 with the following command but it produces nothing in SQL server 2008, no file is created although the query is executed succesfully:
execute xp_cmdshell 'bcp "SELECT * FROM MYDB.dbo.MYTABLE" queryout d:\file.csv -c -t; -S(local) -T'
What is wrong with my command?

I managed to execute the command. Solution:
I installed more than one sql server versions. And the management studio cannot find the bcp.exe because in the enviroment path, there are more than one paths for the bcp.exe. You can check this by executing bcp.exe from command prompt and it will say sqlncli.dll was not found.
So you have to put the exact path of bcp.exe to the enviroment path (or in front of other paths). So that windows can use it. Then dont forget to restart the sql server in order to change in paths takes effect. In conclusion, my command is not wrong but the path of bcp.exe is wrong.

Related

xp_cmdshell throwing 'null' but working in command prompt

I am using xp_cmdshell command to process csv file to a folder. My query is as below:
Exec master..xp_cmdshell 'C:\test\mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test'
In SSMS this command returns null. But in command prompt when I do:
cd C:\test
mydotnetapp.exe -s:03/09/2020 -t:1 -l:1,2,3,4,7 -d C:\test
The same query is working perfectly fine. A couple of things:
xp_cmdshell is enabled
SQL Server has access to the folder
Only this xp_cmdshell command is not working. my other xp_cmdshell is working fine
Any help on how this can be fixed?
If this is run as part of a SQL Job regularly scheduled, run it as a task outside of SQL Server.
Many production DBAs won't let any process run XP_CMDSHELL given the security issues.
If the CSV is well-formed, it can be mapped into SQL Server as a table (not recommended).
It can be imported directly into SQL Server.
Lastly, many production DBAs won't install any custom process code on a production SQL Server machine. The SQL Server machine is dedicated to running only SQL Server plus the minimum extra needed for security and monitoring.

Unable to run .sql file in SQL Server

I have a .sql dump file 20 gb and I am trying to run it on Mysql workbench using run script and after successful execution, using SSMA I'll migrate the data from Mysql workbench to SQL Server. I have migrated the data this way many times successfully however for 20 gb file it seems very time-consuming. Please let me know if there is any alternate way to achieve this quickly. I have followed the following link:
Steps to migrate mysql tables to sql server using SSMA!
From your Title "unable to run .sql file in SSMS" and "I have a .sql dump file 20 gb" are you trying to open a 20GB .sql in SSMS? That's never going to work. SSMS is a 32bit application, so the maximum addressable memory is 2GB. If you want to run your .sql file, I suggest using sqlcmd.
Open up Powershell, and then run the command below replacing the appropriate parts:
sqlcmd -S {Server Name/ServerIP} -U {Your Login} -i {Your full path to your script}
You'll be prompted for your password and then you the file will be run. So, as an example, you might run:
sqlcmd -S svSQL2017 -U Larnu -i \\svFileServer\SQLShare\Scripts\BigBatchFile.sql
If you are using integrated security, then don't pass the -U parameter for the command.
Edit: This answer is no relevant to the OPs question, as they were using "SSMS" as a synonym for SQL Server, which it is not. I have left this here for the moment so the OP can review my comments, and I will likely remove this answer at a later point.

Execute stored procedure from cmd.exe?

Is there anyway to run a stored proc from cmd.exe ?
p.s.
I know how to create exec file c# and to run it.
I'm asking without any code :
Just me and cmd.exe.
edit
me and cmd.exe : meaning I don't want to write any code. Internal SQL Server help exe files of sql is fine !
sorry for not clarifying this !
Try using the SQLCMD Utility :
An example:
sqlcmd -E -S server_name -d database_name -Q "EXEC schema.storedprocedure parameter01, parameter02"
If you'd like to explore other options I'd like to share this link:
Command Prompt Utilities: Applicable for SQL Server 2005
For sql server 2005+ use its SQLCMD utility, as #Nonim answered
for versions prior to 2005 use its OSQL utility, its usage is similar to SQLCMD
Wrap it into the .cmd file and you're in! No needs to write something, just click'n'go! 8-)
C# files are not executable. But the compiled program from them is.
CMD is not C# parser neither SQL client. From the CMD you call a program that serves a file. You and CMD can navigate to drive A: to execute file there, to delete files and so on.
CMD is emulated environment of DOS, which is also OS - you need a program to execute the SQL queries. See this (Sqlcmd program of SQL Server):
http://msdn.microsoft.com/en-us/library/ms165702.aspx

failed to load resource file sqlcmd.rll

I have a sql script file which is 1.5GB
i want to run it but i cant use it using SQL Management Console, cause it can't load the file. I came up to the solution that run the query with SQLCMD utility but when i want to execute the file it returns (Failed to load resource file SQMCMD.rll)
sqlcmd -s (local) -i C:\myScript.sql
anyone knows what is the problem and how i can run this large script file ?
I had the same problem, and I have solved.
In my case the problem was I had installed more than one version of SQL (2005, 2008, 2012), and I had uninstalled the two first ones, so at this moment I only have the Sql Sever Express 2012.
After playing a lot of minutes I discovered that old installations left his respective sqlcmd.exe files in my machine, but the uninstall process removed the corresponding rll file.
When I hit in the cmd window the sqlcmd it seems the system tries to run the sqlcmd for one of the old sql installation.
Then I went to the folder of the installed version and that works.
It is (in my case, in your case you can find the file sqlcmd.exe):
1- cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn
Inside that folder, then
2- SQLCMD -d databaseName -i C:\MyScripts\specificScript.sql
And that did the trick!
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=228de03f-3b5a-428a-923f-58a033d316e1&displaylang=en
I'd download the sqlcmd installer and run it - see if that fixes you up.
It's under "Microsoft SQL Server 2008 Command Line Utilities".
The installer should create that file and you'll be set. You could also try to run osql, which is the older command line SQL utility.
Edit:
Try this:
Please check the permission on the sqlcmd.exe and sqlcmd.rll and make sure the user has the read&execute permission on them. You also could specify the full path of the sqlcmd.exe in the command, such as:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe" -L
I just had this problem on SQL Server 2008 R2. Reinstalling the Command Line Utilities from the Feature Pack is what did it. The trick is if you are on 32-bit only install the x86 package, but if you are on 64-bit install both the x86 and the x64 package. The reason for this being is that some SQL Server tools are only 32-bit which means that both C:\Program Files\Microsoft SQL Server\100\Tools\Binn and C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn are in your PATH variable in order to ensure all of the tools are accessible, which means either of them could be run, therefore both of them need to reinstalled to ensure you have them run properly.
In my case the issue arised after migrating from SQL Server 2008 R2 to SQL Server 2012.
The problem was pretty simple. It was the path. Just correct your SQL Server path in the System path pointing to the new directory.
I got a variant of this error in an Linux installation of the MS SQL Server CLI for Linux, which stated,
# bcp
Unable to load BCP resource DLL. BCP cannot continue.
# sqlcmd
Unable to load SQLCMD resource file(s)
I found that I had not followed the installation direction exactly, in that rather than modify the system PATH to add the installation directory (/opt/mssql-tools/bin), I had used 'install' to copy the binaries to /usr/local/bin without the corresponding /opt/mssql-tools/share directory.
for I in /opt/mssql-tools/bin/*; do install $I /usr/local/bin; done
When I removed those copies and did export PATH="$PATH:/opt/mssql-tools/bin" as the instructions show, then it worked.
# bcp
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[...]
# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.1.0000.1 Linux
[...]

How to run .sql file from a batch file?

I am running sql server 2008 express and i need to schedule some stored procedures to run nightly...so i have built out these .sql files which i would want to run from .bat file...i need to know the command to execute these .sql files one by one and store their results i guess...can anyone help me out?
I answered this in this other question:
You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:
sqlcmd -E -S yoursqlinstance -i backup.sql
-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.
See the sqlcmd utility:
http://msdn.microsoft.com/en-us/library/ms165702.aspx
This allows you to run sql scripts from the command line
osql:
http://www.di-mgt.com.au/osqlUtility.htm
I don't use SQL Server, but a batch file is just a list of DOS commands. So whatever you use to execute SQL files from the commandline can be used in a batch file.
A quick google search turns up:
sqlcmd -i <inputfile> -o <outputfile>
Hope this helps you :
sqlplus UserName/Password#DataBase #C:\sqlFolder\sqlFile.sql
P.S : Don't forget to add the command "commit;" at the end of sql file (sqlFile.sql), this command order Oracle to save performed changes in database