Script MSSQL query on remote server - sql-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.

Related

How do I configure Snowsql to automatically run a sql script as soon as it connects Snowflake?

I setup my snowsql in mac and it works fine for general sql commands. What I'm trying to achieve here is to config the snowsql automatically run a pre-configured sql script upon the connection is established.
It's similar to Aginity's pre-execute script configuration in the connection property page.
Appreciate any advice.
Create batch file , to wrap up snowsql run which will connect to SNowflake and subsequent run command to run the default sql script.

Do scripts executed remotely via SQLCMD get processed by the host or target machine?

When running a command like the following:
SQLCMD -S MYREMOTECOMPUTER\DB -E -i CreateDBCompany.sql
Does the CPU of the host or target machine get used to execute the bits within CreateDbCompany.sql?
Context
I have a host machine that's copied a BAK to a remote machine. I'd like to also trigger the restore of the BAK remotely, but not if it consumes network resources (by using the host machine to actually execute the restoration - effectively doing a remote restore) as I'll be triggering ~20 of these simultaneously.
If you doubt on which server the command will be executed you can try different things like running Profiler against remote server or do some debugging. The easies way to be sure is to run following script:
SQLCMD -S MYREMOTECOMPUTER\DB -E -q "SELECT ##SERVERNAME"

How do I call a stored procedure in SQL Server with Data Pipeline in ShellCommandActivity (AWS Data Pipeline)

I know you can call a MySQL procedure with the script below, but is the same possible for SQL Server?
mysql --host host_url --port port_number --user username --password password --execute="CALL stored_proc_name;
I have SQL Server Express, and need to setup a procedure to be run daily. It's on RDS, and SQL Server Express doesn't have a task scheduler..
The following should work:
Download the SQL Server JDBC Driver. Choose to download the tar.gz file and unzip it. Among the extracted files should be file named sqljdbc.jar. Upload this to S3. For licensing reasons, AWS is not able to distribute this file.
Create a Data Pipeline JdbcDatabase object to refer to your SQL database. Set the jdbcDriverJarUri to your S3 location. Set the driver class name to "com.microsoft.sqlserver.jdbc.SQLServerDriver".
Invoke the EXEC command from a Data Pipeline SqlActivity. The SqlActivity will refer to the JdbcDatabase object that you've defined.

Using sqlcmd to capture the data from a remote server and putting the excel file in some other server

I have used sqlcmd to capture the required data from a remote server ,which is coming fine .
I have used the below query in a batch file -
>>"Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now This batch file is kept on a local machine ,thus when it runs it produces an output _hvac excel file .
I want that this excel file be stored in some other server which is not having sql server installed .The batch has to run on the local machine and the excel sheet to be updated on the server .Now I have access to the server but if i am trying to do this with the following query ,it shows access denied .
>>"//172......./d$/Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now how would i do this .
You need to formally connect to a remote server when you use the sqlcmd command.
Check out the -S argument:
-S [protocol:]server[\instance_name][,port] Specifies the instance of SQL Server to which to connect. It sets the sqlcmd scripting variable SQLCMDSERVER.
Specify server_name to connect to the default instance
of SQL Server on that server computer. Specify server_name [
instance_name ] to connect to a named instance of SQL Server on that
server computer.
If no server computer is specified, sqlcmd connects
to the default instance of SQL Server on the local computer. This
option is required when you execute sqlcmd from a remote computer on
the network. protocol can be tcp (TCP/IP), lpc (shared memory), or np
(named pipes). If you do not specify a server_name [ \instance_name ]
when you start sqlcmd, SQL Server checks for and uses the SQLCMDSERVER
environment variable.
Full article on msdn.

PostgreSQL Database Creation on Remote machine

Can we create a database on remote machine in postgresql DB, if yes then how?
Yes. Assuming the remote server is Linux, and assuming you have sudo or root privileges on that server, you could SSH into the remote server and use a package manager (like yum, synaptic, or apt) to install the PostgreSQL server.
You can configure it using vi (or emacs or other text editor). Usually, the files you edit are found in the /var/lib/pgsql/data folder, and an /etc/init.d/postgresql startup script for whenever the server reboots. You'll usually need to edit the pg_hba.conf file to allow a connection from your client's subnet. That part can be troublesome if overlooked.
You'll be able to initially run psql as the postgres user, and can create datatabases, run scripts, etc from the command line. Once you allow a remote connection, you can do the same from the comfort of your own gui, such as PgAdminIII or any Java-based database manager that can use a JDBC connector.
Example:
psql -U postgres
create database foo;
\q
First, get your programming language. Let say it's PHP to make it simple.
Second, you get the PostGresql connector. Here is the link for PHP but you can get for all popular language.
Third, you open a connection and you do the command you want. You want to create a database so you call the create database YourDatabaseName command.

Resources