Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Pass Insert SQL Statement to MS-SQL DB via BAT script

Pass Insert SQL Statement to MS-SQL DB via BAT script

Scheduled Pinned Locked Moved Database
databasetoolsquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mil_an
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • M mil_an

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        M Offline
        M Offline
        mil_an
        wrote on last edited by
        #3

        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!

        A 1 Reply Last reply
        0
        • M mil_an

          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!

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • A Ashfield

            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

            M Offline
            M Offline
            mil_an
            wrote on last edited by
            #5

            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>>
            GO

            Now, 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups