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
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 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
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 do I launch multiple sqlcmd windows from a batch file that all point to the same database? For example, when I run the .bat file I want it to spawn N number of windows based on a parameter that I pass into it (ex. 5). Each of these 5 windows should open on my desktop and all connect to the same database. That's what I want to do first. Once I have that working, I then want to have each of those 5 windows to run a distinct .sql script that performs inserts, queries, updates, deletes, calling stored procedures...essentially emulating a production environment to help us in debugging efforts (under a user load). I want to see the output of each .sql commend flying by in the sqlcmd window while it is being executed.
I found:
http://hammerora.sourceforge.net/
which is a GUI tool that is focused on TPC-C load testing, but it is not exactly what I want. I bring it up because it is a similar concept that I want to do only driven by batch files on a smaller scale (ex. 20 concurrent users max).
I created a system like this back in the late 90's for Oracle scalability testing but I've been out of the database business since then and can't remember how to do it and how different it would need to be to support SQL Server. So I know it is possible in Oracle, but just not sure about SQL Server given the command line tool and scripting capabilities.
Does anyone have any information about what it would take to make this work?
Ex. Create a launch3users.bat file that looks like:
sqlcmd -d MichaelTest -run this 1.sql file
Pause
sqlcmd -d MichaelTest -run this 2.sql file
Pause
sqlcmd -d MichaelTest -run this 3.sql file
Pause
where each of those would spawn a sqlcmd window and run the proper .sql script which could do DML operations or called stored procedures.
Thanks,
Michael
You simply add "start" to the beginning of commands.
start sqlcmd -d MichaelTest -i 1.sql
start sqlcmd -d MichaelTest -i 2.sql
start sqlcmd -d MichaelTest -i 3.sql
I have a database table on a development server that is now fully populated after I set it running with an import routine for a CSV file containing 1.4 million rows.
I ran the Database Publishing Wizard on the table, and now I have a 286MB SQL script on my local machine. The problem is, I can't figure out how to run it. If I load it into SQL Server Management Studio Express I get an alert window that says "The operation could not be completed".
Any ideas on how I can get this SQL script to run?
use the sqlcmd tool to execute the file..
sqlcmd -S myServer\instanceName -i C:\myScript.sql
In case your have an unexplained "script error" for large sql files (> 100MB) which includes several INSERT, just replace "INSERT INTO" by "GO INSERT INTO" in your file, which will reduce size of transaction.
This tool (Big SQL Script File Runner) on CodePlex will run any size script file with log and GUI.
Adding to Gulzar Nazim's answer:
If you still get a failure, try specifying the codepage of your SQL file using option -f:
sqlcmd -S myServer\instanceName -d databaseName -i C:\myScript.sql -f 65001
I was trying to import a .dump file from SQLite (UTF-8 by default), and sqlcmd kept throwing an error after encountering the first special character. -f 65001 fixed it for me.
Why not just use DTS to import the CSV file directly?
Yes we could do that, I tried with BCP(Bulk Copy Program) approach in order to avoid OutOfMemory issue.
Note : Tried in SQLServer 2014
In BCP, first we need to export the Source DataBase data to bcp file(in local directory folder) and then need to import that bcp file to Source DataBase
Below are the cake walk steps:
Note:
a) Make sure empty table is present in Destination DataBase
b) Make sure Temp folder is present in C drive
1) Create a bat file named as Export_Data.bat with below command
bcp.exe [Source_DataBase_Name].[dbo].[TableName] OUT "C:\Temp\TableName.bcp" -S "Computer Name" -U "SQL Server UserName" -P "SQL Server Password" -n -q
pause
2) Run that bat file, as a result of that a bcp file will get generated in Temp folder
3) Then Create a another bat file named as Import_Data.bat with below command
bcp.exe [Destination_DataBase_Name].[dbo].[TableName] IN "C:\Temp\TableName.bcp" -S "Computer Name" -U "SQL Server UserName" -P "SQL Server Password" -n -q
Pause
And here we go!!
Running something that large inside a single transaction is not a good idea. Therefore, I'd recommend breaking up the file into smaller, more manageable chunks.
Another option is to look at some of the other ways to import CSV data directly.