ogr2ogr Batchfile from TSQL Stored Procedure - sql-server

I have a stored procedure with the following sql
EXECUTE xp_CMDShell
'\\gissrv\data\BroadSpectrumSQLTreeExtract\ogr2ogr_reproject.bat'
which is meant to run the batch file containing
pushd \\gissrv\data\BroadSpectrumSQLTreeExtract\ogr2ogr
ogr2ogr -f "MSSQLSpatial"
"MSSQL:server=gissrv;database=Infrastructure;trusted_connection=yes"
"MSSQL:server=gissrv;database=Infrastructure;trusted_connection=yes" -sql
"SELECT * FROM [Infrastructure].[dbo].[BS_Trees_Line_Shire_Inv]" -t_srs
"EPSG:28355" -lco "GEOM_TYPE=geometry" -lco "GEOM_NAME=GEOMETRY_SPA" -nln
"BS_Trees_Line_Shire_Inv_reprojected2"
popd \\gissrv\data\BroadSpectrumSQLTreeExtract\ogr2ogr
The pushd creates a temporary Z drive to access the ogr2ogr.exe, then reprojects SQL data into a new table. It works when I run it, but fails with from SQL. I get the following.
ERROR 1: Can't load requested DLL:
Z:\BroadSpectrumSQLTreeExtract\ogr2ogr\gdalplugins\ogr_MSSQLSpatial.dll
126: The specified module could not be found.
I have given the folder SQL Service permissions (full control). I also tried giving permissions in the batch file. xp_CMDShell has also been configured on the server. Can you run such a script from SQL Server?

Related

SQL Server ogr2ogr batch unable to access ogr_MSSQLSpatial.dll

I have an ogr2ogr batch file that reprojects SQL data into a new SQL Server table.
It works fine when I run the bat file manually but it fails if I run the bat file via a SQL Server stored procedure. I have given the gdal folders SQL Service permissions and xp_CommandShell is also enabled. I'm using
EXECUTE xp_CMDShell 'blah'
in the T-SQL script.
For some reason the ogr_MSSQLSpatial.dll causes it to fail.
ERROR 1: Can't load requested DLL: Z:\BroadSpectrumSQLTreeExtract\ogr2ogr\gdalplugins\ogr_MSSQLSpatial.dll
If I remove this dll the script runs via SQL but it means I need to add extra commands that the dll must take care of, such as setting source coordinate system. I haven't managed to get it working 100%. The furthest I got to was producing the reprojected table but the geometry field is empty.
The DLL does contain SQL commands to the system tables. Could this be a SQL Server security issue stopping it from working?
I again had this problem with another ogr2ogr bat while executing with SQL. If I put the bat in the same folder as the dll's it works fine.

Executing set of SQL queries using batch file?

I am using a SQL Server database. I have these SQL queries:
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Is it possible to run these scripts using a batch file? The database is a remote database.
Thanks!
Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.
Contents of C:\Temp\ClearTables.sql
Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;
Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).
sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql
For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.
You will execute all this by opening a CMD command window.
Using a Batch File.
If you want to save it in a batch file and double-click to run it, do it as follows.
Create, and save the ClearTables.bat like so.
echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:
Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.
Check out SQLCMD command line tool that comes with SQL Server. http://technet.microsoft.com/en-us/library/ms162773.aspx
Use the SQLCMD utility.
http://technet.microsoft.com/en-us/library/ms162773.aspx
There is a connect statement that allows you to swing from database server A to server B in the same batch.
:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]]
Connects to an instance of SQL Server. Also closes the current connection.
On the other hand, if you are familiar with PowerShell, you can programmatic do the same.
http://technet.microsoft.com/en-us/library/cc281954(v=sql.105).aspx
Different ways:
Using SQL Server Agent (If local instance)
schedule a job in sql server agent with a new step having type as "T-SQL" then run the job.
Using SQLCMD
To use SQLCMD refer http://technet.microsoft.com/en-us/library/ms162773.aspx
Using SQLPS
To use SQLPS refer http://technet.microsoft.com/en-us/library/cc280450.aspx

Batch file to run an MS Access query and

I have an Access DB which has query to retrieve data from teradata. I want to create a batchfile which will execute that particular query.
You can create a macro to run the query and then exit Access, then use a command line switch to automatically run this from a batch file.
Example:
"C:\Program Files\Microsoft Office\Office11\msaccess.exe" "C:\Program Files\Microsoft Office\Office11\samples\northwind.mdb" /X mcrRunQueryAndQuit
More detail on Access command line switches: http://support.microsoft.com/kb/209207
(Should work for Access 2000 - 2010)
VBA for Microsoft Access is your key.
Video tutorial: http://www.youtube.com/watch?v=G9pqmd2PSYk

Create batch file to copy contents of one SQL Server database to another

I need to create a batch file (script) to copy entire content of one SQL Server database to another. Source database is local for the machine. What command line tools and commands therein I could use? Destination database can be dropped entirely.
Also, I need to run a pair of SQL queries afterwards, also from batch file.
Thanks in advance!
you can read about Run Transact-SQL Script Files Using sqlcmd from here:
http://msdn.microsoft.com/en-us/library/ms170572.aspx
first u need to create your sql queries then u can use this command to run it via batch file
sqlcmd -S %computername%\%SName% -U %UName% -P %Pwd% -i SQL_DB.sql >> _Deploy.txt 2>&1
this command gets computer name and sqlserver instance name and user name and password and run SQL_DB.sql file near batch file that contains our queries and save the result in a text file named _deploy.txt

How to Launch External SQL from an MS Access Link

I am building an Access database that functions as a reference library. I want to use links in the Access database to execute SQL queries in a different database. Presently when I click the Access hyperlink it tries to run SQL ServerManagemenrt Studio but then errors with 'The operation could not be completed'. I also tried using the Access hyperlink to open a folder containing the SQL queries so one can double click the SQL to run it. The folder opens but the same error message occurs when I try to run SQL from the folder. Clearly something is happening due to the SQL or folder containing the SQL being opened by MS Access. Can any advise what to do?
Since SQL Server Management Studio is openning, are these .sql text files you're trying to execute?
You can execute the t-sql script contained in the file by running this command line:
SQLCMD -S SQL_SERVER_NAME -d DATABASE_NAME -E -I -i "C:\QueryFile.sql" >> ResultBatch.txt
The results get sent to the file ResultBatch.txt which would be in the same folder as the sql file.
Create a batch file in the folder with all the scripts and this will execute all of them:
for %%X in (*.SQL) do SQLCMD -S SERVER_NAME-d DATABASE_NAME -E -I -i "%%X" >> ResultBatch.txt

Resources