Running multiple SQL files in Microsoft SQL Server - sql

I have some SQL files which I can run in a single script in oracle like this:
start create_tst_prod_class_tbl.sql
start create_tst_tran_ins_type_tbl.sql
start create_tst_order_type_tbl.sql
start create_tst_timezone_tbl.sql
Now I want to do that in MSSQL too but I could not figure out how. I have tried following things:
Writing a query like this:
:r "C:\Users\Ferid\Desktop\standard\create_tst_prod_class_tbl.sql"
:r "C:\Users\Ferid\Desktop\standard\create_tst_tran_ins_type_tbl.sql"
:r "C:\Users\Ferid\Desktop\standard\create_tst_order_type_tbl.sql"
:r "C:\Users\Ferid\Desktop\standard\create_tst_timezone_tbl.sql"
and activate SQLCMD mode but when I execute it this error occurs:
A fatal scripting error occurred.
Incorrect syntax was encountered while parsing EXIT.
Creating a batch file in the folder where the SQL files are with this content:
SET SQLCMD=sqlcmd -S<Servername> -d<databasename> -U<Username> -P<Password> -E
for %%d in (*.sql) do %SQLCMD% -i%%d
When I run it following error occurs:
Sqlcmd: The -E and the -U/-P options are mutually exclusive.
Creating a batch file in the folder where the SQL files are with this content:
for %%G in (*.sql) do sqlcmd /S <servername> /d <databasename>
-U <username> -P <password> -i"%%G"
pause
When I run it following error occurs:
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Named Pipes Provider:
Could not open a connection to SQL Server [53]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related
or instance-specific error has occurred while establishing a connection to SQL Server.
Server is not found or not accessible. Check if instance name is correct and if
SQL Server is configured to allow remote connections.
For more information see SQL Server Books Online..
Sadly I have no access to remote connections options.
Are there any other options or ways to fix these errors?

Your case 2. is working:
SET SQLCMD=sqlcmd -S<Servername> -d<databasename> -E
for %%d in (*.sql) do %SQLCMD% -i%%d
OR
SET SQLCMD=sqlcmd -S<Servername> -d<databasename> -U<Username> -P<Password>
for %%d in (*.sql) do %SQLCMD% -i%%d

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'..

Select after login via Batch (Microsoft SQL Server Management Studio)

I am trying to select data from table and save them info .csv file via .bat file.
My batch runs Microsoft SQL Server Management Studio and log me into database, but I am not able to execute sql scripts.
Here is what I have:
#echo off
cls
:begin
set /p SName=Server Name :
set /p DbName=Database Name :
set /p UName=User Name :
set /p DbPWD=Password :
pause
echo Running Microsoft SQL Server Management Studio, please wait
call ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD% "select * from MYTABLE" -s "," -o " Export.csv"
echo Done
pause
:end
It does not work after line: ssms -S %SName% -d %DbName% -U %UName% -P %DbPWD%
UPDATE:
Error picture
Thank you
SSMS does not accept an in-line script from the command line, as you are trying to do. Please use SQLCMD for this work. that's why you got the error message.
See SSMS Command Line
Straight from Microsoft
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql> -o <MyOutput.rpt>

SQL SERVER 2008 - Sqlcmd: Error: Internal error at ReadText (Reason: No mapping for the Unicode character exists in the target multi-byte code page)

I'm using MS SQL SERVER 2008 R2 and try to use sqlcmd from command prompt. But getting this error when I try to run sqlcmd with -i command, which is for input file.
This command which is executing simple test.sql(containing only SELECT ##SERVERNAME for test purposes) gives an error:
sqlcmd -S serverName -i test.sql
BUT, this command works well (I just specify command by -q query, instead of reading from file):
sqlcmd -S serverName -q "SELECT ##SERVERNAME"
the error message is:
Sqlcmd: Error: Internal error at ReadText (Reason: No mapping for the Unicode character exists in the target multi-byte code page).
Some issues on google say that this was a known bug and was fixed in SP1, I have installed it but still getting the same problem.
Changing Computer/VM locale and region to US solved the problem.

BCP unable to open host data file when migrating to Azure

I haver a sample GTFS data file I am attempting to load to Azure. The command I am using is:
bcp %fullTablePath% in %data_dir% -f %format_file% -S %server% -U %username% -P %password% -k -F %first_row%
where the parameters are replaced accordingly.
When I execute the command, I get:
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Unable to open BCP host data-file
This is not a file naming issue because the file is indeed there. If I deliberately spell the file incorrectly, I get the same error. Sounds like a permission issue but who do I grant what permission?
If you see this error in batch file but not from PowerShell check that your
%fullTablePath% or %data_dir% variables are full qualify path/absolute path and not relative path as PowerShell and Batch may have different default path settings which may give you the error of:
Unable to open BCP host data-file

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.