Run sql scripts using batch file - sql

I am trying to create a batch file that connects to my local Postgres 14 server and creates tables by opening a .sql file that contains all the table scripts.
For now, I was able to connect to the server using this code:
psql -U postgres -h localhost -d test
This command opens the command prompt and asks for the password. After this, how do I update the batch file to insert the password and automtically install the tables from my .sql file?

Related

Restore Database from sql file docker

I am trying to restore a database with a sql file from a dockerized postgresql using this command:
cat pathfile.sql | docker exec -i dbcontainer psql -U user
But when I run this command the console doesn't do anything, it doesn't even throw an error.
I have verified the database and there is nothing created

batch file for executing sql query in a production server

is it possible to create a batch file which run from local computer to execute a query in production server, production server can be accessible only through remote connection. ? if not possible please suggest some ways,
Requirement: Need a batch files to get query results from production server?
Try like this,
Step-1:
Enter the below five lines and Save this as execute.bat file
set /P TargetServer=Enter Target DB Server IP:
set /P TargetDb=Enter Target DB Name:
set /P UserName=Enter UserName:
set /P Passwd=Enter Password:
sqlcmd -S %TargetServer% -U %UserName% -P %Passwd% -d %TargetDb% -i MyScript.sql -o MyOutput.txt
Step-2
Provide your sqls in this file and save it as MyScript.sql
select *from tablename
Step-3
Place execute.bat and Myscript.sql files in a folder then double click the execute.bat file. Your output of the MyScript.sql is saved in MyOutput.txt file.
Create a Batch file and Keep it production server itself by scheduling a task Task Scheduler (Windows assumed OS). So that no need of Remote access to Production server every time.
This is my suggestion from what I understood from your question.

WinSCP command to execute command on the server

I just wanted to automate the file transfers without opening WinSCP, through script.
Up to some extent I performed the file transfer operation except PuTTY session.
I prepared a sftprun.cmd batch file like:
D:\winscp\winscp.com /script=D:\winscp_auto\sftpscriptJAC.txt
Can you please suggest me to open PuTTY through above mentioned script to execute command on the server?
mount -o remount,rw /
The putty details should be provided under the script file "sftpscriptJAC".
WinSCP can execute remote commands on its own from the script (with some limitations), using the call command:
open sftp://user#example.com/
put C:\file.txt
call mount -o remount,rw /

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

how to run tsql from a remote system in command prompt

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]