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
Related
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?
I'm trying to run an SSIS job daily using the SQL Server Agent. In my job step I have:
Type - Operating System (CmdExec)
Run as: Sql Server Agent Service Account
Command: "C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe"/F"C:\Users\Administrator\Documents\SourceToTarget\SourceToTarget\TestSqlServerAgent\TestSqlServerAgent\GoogleGeocode.dtsx"
When I run this I get an error saying: "At least one of the DTS, SQL, ISServer or File options must be specified. Process Exit Code 6"
Does anyone know how to fix this?
The best way to chose your command is to open the package, go to commandline tab and copy the whole line. Then put this line in front of DTExec command. You do that you need to make sure your connectionstrings a properly set up. Fire the command like this and you'll be fine.
If your command line truly is
"C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe"/F"C:\Users\Administrator\Documents\SourceToTarget\SourceToTarget\TestSqlServerAgent\TestSqlServerAgent\GoogleGeocode.dtsx"
Then the problem is that you need a space between the arguments
"C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\DTExec.exe" /F "C:\Users\Administrator\Documents\SourceToTarget\SourceToTarget\TestSqlServerAgent\TestSqlServerAgent\GoogleGeocode.dtsx"
Unfortunately, testing that on my machine doesn't yield the error message you've encountered.
In fact, searching for DTEXEC error code yielded this fine answer from William Salzman which reinforces my guess that the supplied arguments for your agent job have been simplified/obfuscated for posting.
So I have a lot of table create scripts that I need to run on a fresh database. They are all in one folder, and theirs over 250 of them.
Is there a good way to go about doing this using SSMS?
Option 1: USE BATCH FILE
Using Batch file[to run all the scripts in one click] but that cmd implicitly will use sqlcmd.
Option 2: MERGE MULTIPLE SQL FILES INTO A SINGLE SQL FILE AND THEN EXECUTE IN SSMS IN ONE SHOT
Create a simple batch file under the folder(wherein your 250 table creation scripts reside) as follows
type *.sql > allinone.sql
Note: Open txt document and then copy paste the above content and then save it with extension .bat(Ex:merge.bat)
Just double clicking that batch file will give you the single sql file which you can run in SSMS in single go.
We have several SQL scripts which are generated from an Entity Model. They need to be run in a specific order. Additionally there are several filling scripts which insert test data into the database.
Currently I need to open each script in Visual Studio and execute them in the correct order by clicking Execute (ctrl shift E).
Is there a way to create a script like Master.sql that contains includes like this:
BEGIN TRANSACTION
ImportOrInclude myDB1.sql
ImportOrInclude myDB2.sql
...
COMMIT
This is only required to run from Visual Studio and will not be part of the application itself.
How can this be done?
EDIT 1
I've found that there is something called SQLCMD Scripts which can import SQL files:
http://msdn.microsoft.com/en-us/library/aa833281%28v=vs.80%29.aspx
But my question is then how to get the directory path of the current solution into the :r command.
EDIT 2
So I figured out how to do it, not perfectly, but it works. The downside is that the $(SolutionDir) is not loaded from the Visual Studio variables, so you need to set it up manually. This code is meant to be run in Visual Studio:
-- turn on in menu: Data -> Transact SQL editor -> SQL CMD mode
-- set this to path where the .sln file is.
:setvar SolutionDir C:\_work\projectname\
:!! echo $(SolutionDir)Maa.EntityModel.All\DbWEntityModel.edmx.sql
:r $(SolutionDir)Maa.EntityModel.All\DbWEntityModel.edmx.sql
go
:!! echo $(SolutionDir)Maa.EntityModel.All\DblQEntityModel.edmx.sql
:r $(SolutionDir)Maa.EntityModel.All\DbQEntityModel.edmx.sql
go
Use sqlcmd utility.
Extract you might find interesting:
-i input_file[,input_file2...]
Identifies the file that contains a batch of SQL statements or stored procedures. Multiple files may be specified that will be read and processed in order. Do not use any spaces between file names. sqlcmd will first check to see whether all the specified files exist. If one or more files do not exist, sqlcmd will exit.
Example:
sqlcmd -dDataBaseName -E -Stcp:ServerName\instancename -imaster.sql
Master.Sql:
:r file1.sql
:r file2.sql
Use the Business Intelligence Development Studio that comes with SQL Server to do this. Create a new SSIS Package. Inside this package create several Execute SQL Tasks in the Control Flow for each file you have and set SQLSourceType to FileConnection and choose your file.
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