How to open .sql file at startup in SQL Server Management Studio? - sql-server

I'm looking to open specific .sql files that have multiple select statements on specific servers at startup in SQL Server Management Studio 2014.
For example, I want to open file 'EmQueries.sql' when I open server sqlemdata9. When I open server sqlplant2, I would like to see file 'PlantQueries.sql' open at startup.
How can I accomplish something like this?

You can achieve the following procedure with Command Line Process, will help you connect to different databases related to specific files and user credentials.
This is Easy with command line (cmd)
ssms "C:\Users\Wizard\Documents\SQL Server Management Studio\query.sql" -S ServerAddressOrIP -d Database -U Username -P Passwordhere
Copy the following line to (.bat) batch file format and run it.

Related

Connect to sql server and do bulk load of data file from Linux server

Problem Statement:
I am trying to Load data file from Linux- source to SQL server table- target (sql server installed Windows server).
After many meetings with Microsoft we got resolve the connection issue.
Connection string:
[]$kinit windowsserverusername (Windows server AD Account) :- After this command system will prompt for password
[]$ sqlcmd -S servername,port -E -C :- After this command system will connect to sql server with ">"
BULK INSERT SQLSCHEMA.dbo.TABLENAME FROM '**/SOURCE/DataFile/INPUT_DATA.txt**' WITH (FIELDTERMINATOR=',',ROWTERMINATOR='\n');
go
And I got the following error:
Msg 4861, Level 16, State 1, Server SERVERNAME, Line 2
Cannot bulk load because the file '/SOURCE/DataFile/INPUT_DATA.txt' could not be opened. Operating system error code 3(The system cannot find the path specified.).
Issue:
Unable to use fetch the source data file path in sql statement. How to use the Linux server data file path in sql statement or in .sql file.
I have save the BULK load sql statement into .sql file and try to run the command & got same above error.
[]$ sqlcmd -S servername,port -E -C -i INPUTFILE.sql
How to use source file Linux path in sql server sql prompt or in .sql file ??
I have tried .py script by following connection string and we got trusted connection/ certificate issue and Microsoft suggested above sqlcmd method.
"pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)"
``

How to launch pgadmin4 and open a sql file in windows cmd?

I would like to be able to open PGadmin4, connect to a server/database and open an sql file using only command line (Windows).
Is there a way to do that ?
Opening pg admin is fairly simple :
start "" /B "C:\Program Files\PostgreSQL\12\pgAdmin 4\bin\pgAdmin4.exe"
but I don't seem able to find a way to connect to the appropriate server/database and open the sql file I want
I'm writing a .bat file to automate the initial opening of a bunch of application for a development project (django)
Try running following command:
Connect to a specific database:
\c database_name;
For example, the following command connects to the dvdrental database:
\c dvdrental;
You are now connected to database "dvdrental" as user "postgres".
For other commands, visit: https://www.postgresqltutorial.com/postgresql-cheat-sheet/

Command line that opens SSMS not actually logging into database

I am trying to log in to a remote MS SQL Server 2012 by using SSMS and I'm opening SSMS via command line. When I run my command SSMS starts up and it opens my .sql file that I specify but it doesn't actually log in to my database. Here's the two versions of my command that I've run:
"C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Ssms.exe"
-S 172.18.211.76 -U USERNAME -P PASSWORD -nosplash "full_path_to_my_.sql_file"
and
"C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Ssms.exe"
"full_path_to_my_.sql_file" -S 172.18.211.76 -U USERNAME -P PASSWORD -nosplash
Both commands start SSMS successfully and open my .sql file, but neither commands actually logs into my database for me. What I mean by that is that after I run my command the object explorer in SSMS looks like this:
so before I can do anything in my database I have to click on the connect icon and enter my credentials so the object explorer now looks like this:
In the past I was doing this and if I remember correctly I was able to use a command prompt to open SSMS and have it log into a database all at once but I can't find the command that I used to do it previously. If someone could help me out I'd greatly appreciate it. Thanks in advance.
EDIT: here's the version of SSMS I'm using, just in case it's important:
Microsoft SQL Server Management Studio 13.0.15600.2
After looking at this further I have 2 observations:
I didn't specify the name of the database I wanted to connect to in my command using
the -d switch.
I checked and either way, using the -d switch or without, even though the object
explorer doesn't show that I have an active connection I can still run the .sql file
that I specified to be loaded.
So, I guess the problem isn't necessarily that the command I listed doesn't connect to the database, the problem is that a connection is made but it isn't reflected in the object explorer. Thanks everyone.
EDIT: as #larnu pointed out I was running an older version of SSMS. I installed v17.9.1 and tried to run the command but got the same result, after the command is run a connection is made to the database but it is not reflected in the object explorer. Not having the connection display in the object explorer is minor seeing as the connection is actually made and statements can be run in SSMS successfully after SSMS has been opened by the command.

running script in mssql local server group via batch file

A created a mssql local server group containing all our production server. And now i am planning to automate running the script via batch file, is there a way to connect/run my script in my local server group via batch file?
Yes, you can use the sqlcmd command line utility in your batch file to connect and execute queries and / or SQL script files.

Script MSSQL query on remote server

I have a bit of an odd requirement to script some MSSQL queries on a remote host.
I need to be able to run these queries via an Enterprise Scheduler, in this case TIDAL 6.1, and save the output of the query as a CSV or XML file.
I'm assuming I would have to set up a local SQL server to act as a bridge to the remote host, and I'm hoping there is a way to tell the script/query to output the results as a file on the local host.
You need to have SQL Client in the machine where you will be running this query. You can run the SQLCMD command below to run the query and save output to file.
sqlcmd -S(servername) -E -i(sql file) -o(output filename) -d(databasename) -s,
-E option is for trusted connection
Hope this helps.

Resources