how to run tsql from a remote system in command prompt - sql

I wanted to know how to run a TSQL statement from a remote system by connecting to the other system and run a TSQL statement through command prompt.
I have tried the following code but this code doesnt run on the other system.
PS: other system doesnt have sql server installed so it is compulsory that we use just command prompt to run from that system.
sqlcmd -S 100,1433\MSSQLSERVER -U sa -P abc -i C:\table.sql -o C:\output.txt

SQL Server 2000 - osql Utility
There's file called OSQL.exe which I am using for this job.
File comes from SQL Server 2000 (52 KB).
You can run it in CMD with simmiliar arguments to sqlcommand.
Remember to use GO after each batch
Example
Select * from Sales [enter]
GO [enter]

Related

How to connect to an Azure SQL Server using command prompt?

I have an Azure SQL database that I am able to connect using local SSMS.
Server Name - <Server>.database.windows.net
UserName - zrana
Password - *****
The authentication mode I use is here is Active Directory - Password. Is it possible to connect to the database using the sqlcmd utility on the command line?
I am unable to connect using the following command
sqlcmd -S 910005-sql.database.windows.net -d 900046 -U zrana -P ****
There is another database that I created on Azure and tried to connect through Local SSMS.
This time the authentication method is "SQL Server password". I am able to connect to it using local SSMS and through sqlcmd using the same format mentioned above.
Is it possible to connect to an Azure SQL database using AAD? I tried using -G -I options in my command and didn't work.
This is the error I see:
(Adding an answer that is essentially the process we worked through in the comments)
Install the latest sqlcmd from https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-ver15
run sqlcmd -? to see what version of sqlcmd is actually running - if it isn't the latest version you just downloaded, check your PATH variable etc. You can use the dos command where sqlcmd to find where the command line is searching for sqlcmd
run sqlcmd with the -G switch to specify AAD cred checking, and include your user name an dpassword with the -U and -P options. Be careful with the case of these as -u and -p are different switches entirely.
sqlcmd -S 910005-sql.database.windows.net -d 900046 -G -U zrana -P ****
When you want to use an Azure Active Directory user name and password, you need to provide the -G option and also use the user name and password by providing the -U and -P options. On the -U option please see on my example you need to provide the username#domain,
When using the -G option to connect to SQL Database or SQL Data Warehouse and authenticate using Azure Active Directory, a timeout value of at least 30 seconds is recommended. Use "-l 30". Read more about it on this documentation.
Please see the following example:
sqlcmd -S Target_DB_or_DW.testsrv.database.windows.net -G -U bob#contoso.com -P MyAADPassword -l 30

How to execute generated script(.sql file) with schema and data in SQL Server 2008

Using SSMS 2008 I am able to generate a script for a database with huge amounts of data in file ABC.sql on my desktop.
The database has approx. 9 GB of data so I'm unable to open the file. Is there any way to execute the script?
When I try to open it in SSMS I get an error:
The operation could not be completed. not enough storage is available to complete this operation
The template specified cannot be found. Please check that the full path is correct
SQL Server offers 2 command prompt features that can se used for executing large queries - osql (will be removed in future), and sqlcmd
osql is located in the Tools\Binn subfolder. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the osql utility is located
Run the command in the following format:
osql –H <workstation name> -S <server_name[\instance_name]> -U <user login ID> -P <login password> –i <full path to script>
To execute the large.sql file located in the D:\test, against the Central database on the SQL Server instance Dell\SQL2012, as an sa with the 'sqladmin' password, run the following command:
osql -H Dell -S Dell\SQL2012 -i D:\test\large.sql -U sa -P sqladmin
The sqlcmd command line utility is also located in the SQL Server’s Tools\Binn sub-directory. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the sqlcmd utility is located
Run a command in the following format:
sqlcmd –S <server name> -d <database name> -i <full path to script> -U <user login ID> –P <login password>
To execute the same as above, run the following command:
sqlcmd -S Dell\SQL2012 -d Central -i D:\test\large.sql -U sa –P sqladmin
Start the sqlcmd Utility
Run Transact-SQL Script Files Using sqlcmd
I use sqlcmd to execute large SQL files.
You can generate script of your database by RightClick on your database Tasks>GenerateScripts> click next on Generate and Script window Check on select specific table choose tables you want Press next Click on Advance option on end of General Category select Type of data to script now choose which kind you want your database to.
Scheme Only: Means this script will create your database.
DataOnly:If you have created database and table this will insert data into it.
Press ok then Next.
Your file is by default save in
C:\Users[UserName]\Documents\ .

Need help to make sqlcmd run as per requirements

I wanted to run SQLCMD.EXE as mentioned here - http://blog.daringa.com/archives/tag/error-hresult-e_fail-has-been-returned-from-a-call-to-a-com-component
I used the line in link with proper path to sqlcmd, user and password I use to login to my SQL server - sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword -i C:\Database\hugescript.sql
Problem - I see a window and some message, but it vanishes so quickly that I cannot even see what it is. How do I see this window and how do I then execute an SQL file via SQLCMD.EXE
Why am I executing a script via SQLCMD and not SQL SERVER (ie SS) MGMT STUDIO ?
SS throws an error when sql files are big, ie about 100mb or more.
You can redirect the output from sqlcmd
sqlcmd -S .\MYSQLSERVER2008 -U MyUsername -P MyPassword
-i C:\Database\hugescript.sql > log.txt 2> error.txt
It will write the output to log.txt file and errors to error.txt file (you can specify the full path if you want). You can then see what's happening.

Cannot execute script: Insufficient memory to continue the execution of the program

I have a 123MB sql file which I need to execute in my local PC. But I am getting
Cannot execute script: Insufficient memory to continue the execution of the program
How to solve this issue?
use the command-line tool SQLCMD which is much leaner on memory. It is as simple as:
SQLCMD -d <database-name> -i filename.sql
You need valid credentials to access your SQL Server instance or even to access a database
Taken from here.
It might help you! Please see below steps.
sqlcmd -S server-name -d database-name -i script.sql
Open cmd.exe as Administrator.
Create Documents directory.
Put your SQL Script file(script.sql) in the documents folder.
Type query with sqlcmd, server-name, database-name and script-file-name as like above highlighted query or below command line screen.
For Windows Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
Note: If there is any space in the sql file path then use " (Quotation marks) "
For SQL Server Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -U sa -P sasa -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
-S TestSQLServer\SQLEXPRESS: Here specify SQL Server Name
-U sa: Username (in case of SQL Server Authentication)
-P sasa: Password (in case of SQL Server Authentication)
-d AdventureWorks2018: Database Name come here
-i "d:\document\sql document\script.sql": File Path of SQLFile
You can also simply increase the Minimum memory per query value in server properties. To edit this setting, right click on server name and select Properties > Memory tab.
I encountered this error trying to execute a 30MB SQL script in SSMS 2012. After increasing the value from 1024MB to 2048MB I was able to run the script.
(This is the same answer I provided here)
My database was larger than 500mb, I then used the following
C:\Windows>sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -d DATABASE -i C:\FILE.sql
It loaded everything including SP's
*NB: Run the cmd as Administrator
If I understand your problem correctly, you are trying to restore (transact sql) xyz.sql - database + schema. You can try this command which worked for me:
SQLCMD -U sa -i xyz.sql
Try this step,
1)Open PowerShell
2)Write this command:
sqlcmd -S PCNAME\SQLEXPRESS -U user -P password -d databanse_name -i C:\script.sql
3)Press Return
:-)
Below script works perfectly:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Symptoms:
When executing a recovery script with sqlcmd utility, the ‘Sqlcmd: Error: Syntax error at line XYZ near command ‘X’ in file ‘file_name.sql’.’ error is encountered.
Cause:
This is a sqlcmd utility limitation. If the SQL script contains dollar sign ($) in any form, the utility is unable to properly execute the script, since it is substituting all variables automatically by default.
Resolution:
In order to execute script that has a dollar ($) sign in any form, it is necessary to add “-x” parameter to the command line.
e.g.
Original:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql
Fixed:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Sometimes, due to the heavy size of the script and data, we encounter this type of error. Server needs sufficient memory to execute and give the result. We can simply increase the memory size, per query.
You just need to go to the sql server properties > Memory tab (left side)> Now set the maximum memory limit you want to add.
Also, there is an option at the top, "Results to text", which consume less memory as compare to option "Results to grid", we can also go for Result to Text for less memory execution.
sqlcmd -S mamxxxxxmu\sqlserverr -U sa -P x1123 -d QLDB -i D:\qldbscript.sql
Open command prompt in run as administrator
enter above command
"mamxxxxxmu" is computer name
"sqlserverr" is server name
"sa" is username of server
"x1123" is password of server
"QLDB" is database name
"D:\qldbscript.sql" is sql script file to execute in database
If you need to connect to LocalDB during development, you can use:
sqlcmd -S "(localdb)\MSSQLLocalDB" -d dbname -i file.sql
As in most answers given here use the command-line tool. In my case the script already has database creation code. If your script contains CREATE DATABASE command, for example
USE [master]
GO
CREATE DATABASE [your-database-name]
Then do not use the -d your-database-name, instead use the following command.
For Windows Authentication use the command
sqlcmd -S ServerName\InstanceName -i "script.sql" -x
For SQL Server Authentication use the command
sqlcmd -S ServerName\InstanceName -U usename -P password -i "script.sql" -x

Windows 7 OSQL Batch

I have just got a new work PC from running Windows 7. I have installed SQL Server 2008 and I have a batch file that runs an OSQL command as follow:
osql -S MyServer -E -d MSDB -n -o results.txt -i MyScript.sql
For some reason it is giving me exception below:
Specified driver could not be loaded due to system error 126: The specified module could not be found. (SQL Server Native Client 10.0, C:\Windows\system32\sqlncli10.dll).
I am not sure what the issue is and I have even tried to run the batch file using 'Run as administrator'.
OSQL is being deprecated; I would consider converting your code to use sqlcmd or PowerShell. However if you're missing SQL native client you might consider adding it. You can download the sqlncli.msi file for your platform from this URL (more than halfway down the page).