Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:42:48+00:00 2026-05-25T06:42:48+00:00

We have an SQL script in a .sql file. It has a couple of

  • 0

We have an SQL script in a .sql file. It has a couple of parameters defined like this

DECLARE @device_directory NVARCHAR(250) = '$(CONFIG_DBPATH)'+'$(CONFIG_DBNAME)'

We then execute this script in a .bat script:

SET CONFIG_DBNAME=NewConfigBase
SET CONFIG_DBPATH=C:\BT_Server\
sqlcmd -I -S .\SQLEXPRESS8  -i SetUpConfigDb.sql

I’ve been assigned the task of implementing this functionality as a part of our configuration tool, i.e. creating the physical db file and attaching it to the database. Later, we will also include the creation of database tables, stored procedures, ect, which are currently defined in a separate .sql script.

Ideally, I would like the configuration tool to not rely on sqlcmd, but rather execute the scripts in-process (e.g. by use of SqlCommand). This would allow me to use standard error handling (exceptions) rather than parsing sqlcmd’s output.

Now I’m a bit unsure how to tackle this. I want to be able to re-use our existing sql scripts so that we can still use our .bat scripts without maintaining two different sql queries performing the same task.

One of the scripts contains GO on several places. It is my understanding that SqlCommand does not allow this. I am also unsure that I can use SqlParameter to set the value of $(CONFIG_DBPATH) etc (in fact I am almost certain that I can not).

So my question is, what is the recommended way to tackle this scenario? How do I execute .sql commands with GO in them, and how do I specify variables so that the scripts can be used both from .bat files and from managed code? I suppose one option is to ‘wash’ the script when it’s executed from code, to remove GO etc, and modify how variables are handled. But are there any better options?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:42:49+00:00Added an answer on May 25, 2026 at 6:42 am

    If you want to execute sql scripts via sqlcommand programmatically, then you need to open the file and read it line by line. As you pass each line, append it to the current batch string builder and when you hit a line that after trimming is GO, get string builder ToString and execute that batch. Clear the current batch and keep going.

    edit: second part of question
    As far as the parameters go, you can either replace them as part of parsing and executing or you could pull duties like creating the DBs out to C# code. As a preference, I’ve always kept the sql scripts runnable by ssms and sqlcmd by developers even though they happen to be run by C# code as part of the deployment. So that means, I wouldn’t make up any special tokens that only your code understands. You could leave as is in your question, replace them and above that in the script, have the variables declared and commented out with — so developers can easily work in ssms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL data that looks like this: events id name capacity 1 Cooking
I have an SQL script than runs a couple of DBBC commands. Is there
I have a windows batch file that does this: for %%s in (*.sql) do
I have a SQL script that has four columns and about 100 records. I
I have a SQL script that inserts data (via INSERT statements currently numbering in
We have an sql build script, with oracle 10.2. At the end of the
I have a migration that runs an SQL script to create a new Postgres
anyone have script or procedures to install SQL Server 2008 Express, set up the
I would like to ask couple of questions regarding SQL Server Locking mechanism If
Hello I have a very large SQL script and while trying to execute it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.