Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Execute an SQL script file from C++

945 views
Skip to first unread message

Mike Tissington

unread,
May 23, 2001, 12:55:55 AM5/23/01
to
What the best way of executing an .sql file from C++ (I also need to
automate some bcp too)

Thanks.

--
Mike Tissington, MCSD, MCDBA
Shimmering Water
http://oaklodge.com
-

Peter

unread,
May 23, 2001, 3:07:16 AM5/23/01
to
Mike,

You can make a connection to the SQL Server and simply execute the content
in the .SQL file.

1. Read the T-SQL statements
2. Execute it over DAO or ADO.

Do you encounter any errors when doing this?


Peter

Randy Charles Morin

unread,
May 23, 2001, 5:30:28 AM5/23/01
to
ShellExecuteEx or CreateProcess.
--
Randy Charles Morin
Author of Programming Windows Services
http://www.kbcafe.com/randycharlesmorin

"Mike Tissington" <mi...@oaklodge.com> wrote in message
news:#5R1nS04AHA.964@tkmsftngp03...

Mike Tissington

unread,
May 23, 2001, 10:05:30 AM5/23/01
to
I was hoping for a method that would automatically take the contents of the
.sql file since it is quite large.

--
Mike Tissington, MCSD, MCDBA
Shimmering Water
http://oaklodge.com
-

"Peter" <pwno...@microsoft.com> wrote in message
news:jr0GEc1...@cppssbbsa01.microsoft.com...

Mike Tissington

unread,
May 23, 2001, 10:58:52 AM5/23/01
to
When trying to do this I get many errors.

The sql containes a lot of T-SQL for deleteing and creating TABLES/SPROCs
etc ...
If I execute the sql with Query Analyizer then it works.

--
Mike Tissington, MCSD, MCDBA
Shimmering Water
http://oaklodge.com
-

"Mike Tissington" <mi...@oaklodge.com> wrote in message
news:uj1stF54AHA.1256@tkmsftngp03...

Josh Heyse

unread,
May 23, 2001, 2:18:04 PM5/23/01
to
I am also trying to figure out a way to do this too. I would like to have the
sql script be stored in my program as a string, a very large string. I haven't
found anything that takes the same syntax as Query Analyzer. ADO give me a bunch
of errors for things like comments and go statments. If you find an answer
please post it here. I will do the same.

-Josh Heyse
Morse Data Corp

Thanks.

.

Josh Heyse

unread,
May 23, 2001, 2:52:25 PM5/23/01
to
I found the answer to our problem. I opend up good old command prompt went to
the sql\tools directory and typed in "isql /?". I got a ton of parameters. I
just set the ones I needed and hit enter. One of the parameters is an input file.
My command line looked like this

C:\PROGRA~1\MICROS~4\80\TOOLS\BINN>isql -U user -P password -S server -d master -
i C:\sql.sql -o C:\output.txt

It ran great. Now all I have to do is use CreateProcess to replicate this
command line and go from there.

I Hope this has helped.

Josh Heyse
Morse Data Corp.

Thanks.

..

.

Gert E.R. Drapers

unread,
May 23, 2001, 3:22:51 PM5/23/01
to
Because GO is not a SQL statement but a batch separator, that is used by
tools as Query Analyzer, isql, osql to parse the script file/input and chunk
it in batches.

That is what SQL Query Analyzer is doing, so you need to do the parsing your
self, read up to the next GO and submit that for execution, handle the
results and read the next batch, never include the GO statements into what
you send to the server.

-GertD

"Josh Heyse" <jo...@morsedata.com> wrote in message
news:079b01c0e3b4$b58b2f30$9ee62ecf@tkmsftngxa05...

Peter

unread,
May 23, 2001, 10:05:03 PM5/23/01
to
Mike,

You may have a try to use osql utility.

The osql utility is a Microsoft® Win32® command prompt utility for ad hoc,
interactive execution of Transact-SQL statements and scripts. To use osql,
users must understand Transact-SQL.

The osql utility is typically used in these ways:

Users interactively enter Transact-SQL statements in a manner similar to
working on the command prompt. The results are displayed in the command
prompt window.

Users submit an osql job either specifying a single Transact-SQL statement
to execute or pointing the utility to a text file that contains
Transact-SQL statements to execute. The output is usually directed to a
text file, but it also can be displayed in the command prompt window.

The osql utility uses the ODBC database application programming interface
(API). It is a replacement for the isql command prompt utility based on the
DB-Library API. Both utilities are provided with Microsoft SQL Server. The
DB-Library API remains at a SQL Server 6.5 level; therefore, applications
that depend on DB-Library, such as isql, do not support some SQL Server
2000 features. For example, isql cannot access columns defined with the
ntext data type and truncates any char, varchar, nchar, or nvarchar columns
longer than 255 bytes. It also cannot retrieve results as XML documents.
Except for these limitations in isql, both osql and isql support the same
features.


HTH

Peter

"Adam Cogan"

unread,
May 26, 2001, 8:37:42 PM5/26/01
to
Another option is to use a utility. We use SSW SQL Script Wizard - This is a
utility to run .sql files - it is at www.ssw.com.au/SQLScriptWizard

Adam


"Mike Tissington" <mi...@oaklodge.com> wrote in message

news:#5R1nS04AHA.964@tkmsftngp03...

Peter

unread,
May 28, 2001, 9:09:16 PM5/28/01
to
Hi Mike,

Since this seems to be a quite complex problem, I suggest you submit a new
for Microsoft PSS. You can reach PSS at
http://support.microsoft.com/support/webresponse.asp

A support professioanl will follow up with you soon after you have created
a case for the problem.


Peter

0 new messages