Pass Insert SQL Statement to MS-SQL DB via BAT script
-
Hi, I am trying to pass an UPDATE / INSERT statement into an MS-SQL DB via a BAT script. Basically, I want to amend / add entries in the DB via a BAT script. 1. Can this be done? 2. If it can be done, how would I connect to the DB? What would the syntax be? Any assistance on this will be much appreciated. Thanks, M
-
Hi, I am trying to pass an UPDATE / INSERT statement into an MS-SQL DB via a BAT script. Basically, I want to amend / add entries in the DB via a BAT script. 1. Can this be done? 2. If it can be done, how would I connect to the DB? What would the syntax be? Any assistance on this will be much appreciated. Thanks, M
You can use sqlcmd with the -q command argument. More details here: sqlcmd Utility[^]. There is also a sqlcmd tutorial[^].
Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual
-
You can use sqlcmd with the -q command argument. More details here: sqlcmd Utility[^]. There is also a sqlcmd tutorial[^].
Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual
-
Thanks for your response Colin, much appreciated. However, it looks like SQLCMD is only available in MS-SQL 2005 onwards. I am using MS-SQL 2000 though, and I think the OSQL command is the alternative. Any experience with this? Thanks!
-
mil_an wrote:
I think the OSQL command is the alternative
Correct. The server & connection details can go on the command line and you can read a file of commands to execute. A quick google should reveal plenty of examples.
Bob Ashfield Consultants Ltd
Thanks. This is what I have so far and it seems to work when run on the server where the DB is located:
OSQL -s <<SERVER_NAME>>\<<DB_NAME>> -U <<USERNAME>> -P <<PASSWORD>> -i <<PATH_TO_SQL_COMMAND_FILE>>
The above makes the connection to the DB and runs the SQL statements held in a text file (PATH_TO_SQL_COMMAND_FILE), which looks a little like this:
UPDATE <<TABLE_NAME>>
SET <<Column>> = '<<VALUE>>'
WHERE <<PREDICTAES>>
GONow, I am facing the following issue. I am unable to run this command from a different server that does not host the DB. I get the below error:
[Shared Memory]SQL Server does not exist or access denied. [Shared Memory]ConnectionOpen (Connect()).
I am using the host name of the DB server including DNS for server_name. Any ideas?modified on Wednesday, December 10, 2008 9:15 AM