Run SQL comand with String from SQL Script

G

Guest

Using SQL 2005 I generated a SQL Script to create a DB Table and columns. in
VB.NET i use the StreamReader to read this file to a string, and I want to
execute the script in my application, so the app can create an identical
table at runtime.

Obvously if i simply run the SQL Script from the file, it works, and if I
copy the StreamReader String while debugging into a SQL query, it also works
(creates the table) fine. However, when the program steps to the
SqlCmd.ExecuteNonQuery( ), I get a SQL Exception, "Syntax error near....".
The sql script (and command string) are very long, (Many tables, many
properties... many "Syntax error near"). I've tried to Replace single
(apostrophe) ' with '' and even single (quote) " with "", but the same
result. How come if I paste the string directly from the app to the query
analizer it works, but errors out in the app? Any suggestions?

Thanks
MATT
 
G

Guest

In the SQL Script, after each EXEC line, it is followed by a 'GO' command.
When i remove the go, it seems to work much better. Can Anyone explain?
 
J

Jim Hughes

GO is not a TRANSACT SQL keyword

It is a batch separator used by Query Analyzer (and its siblings).

Split your script on the GO keyword and execute each segment by itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top