Showing posts with label statements. Show all posts
Showing posts with label statements. Show all posts

Monday, March 19, 2012

Advanced SQL generations options

Advanced SQL generations options:

generate INSERT, UPDATE, and DELETE statements is all greyed out?

in my sql data source control.?

I have made a brand new instance with sql server management express...have I missed something?

Does the table have a Primary key defined?|||

Sorry about being slow to get back

NO,

|||

Thankyou very much Mr Wellens.....

I just put in a primary key into table and all working fine.

Does that mean you cant insert into any table without it having a primary key.

many many thanks

|||

>>Does that mean you cant insert into any table without it having a primary key.

Well YOU can, but ASP.Net wants a primary key.

If there was no primary key, the ASP.Net code could get very confused about what row needs to be updated or deleted.

Sunday, March 11, 2012

Advance Update Statement Sql Server

I am trying to speed up my update statements by removing inner select
statements.

Example:
update orders set shipname = (select contactName from
customers where customerid = orders.customerID)

I read some articles which said that I should be able to use an inner
join on the update statement like the following:

update orders set shipname = (select contactName from customers where
customerid = orders.customerID)

But every time that I run this statement I get the follwing error:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'inner'.

Any Help will be greatly appreciated.

Thank you.I think you posted the wrong UPDATE statement. Both those statements
are identical and valid syntax.

However, if performance is your concern then why not take SHIPNAME out
of the Orders table. It looks like it's redundant there.

--
David Portas
SQL Server MVP
--|||See "Changing Data Using the FROM Clause" and also example C under
UPDATE in Books Online (although it would be better to rewrite it with
INNER JOIN).

Simon|||Both statements were the same ?! I think I know what you meant to say
...

As long as the scalar query expression returns zero or one row, you
will be fine. If you use the proprietary FROM syntax, you will get an
unpredictable result from a multi-row result set.

The real cost of an update is in the physical disk access, not the
code.|||HeadScratcher (mayur@.servicemg.com) writes:
> I am trying to speed up my update statements by removing inner select
> statements.
> Example:
> update orders set shipname = (select contactName from
> customers where customerid = orders.customerID)
> I read some articles which said that I should be able to use an inner
> join on the update statement like the following:
> update orders set shipname = (select contactName from customers where
> customerid = orders.customerID)
> But every time that I run this statement I get the follwing error:
> Server: Msg 156, Level 15, State 1, Line 1
> Incorrect syntax near the keyword 'inner'.

Apparently there was some glitch in the editing. Anyway, this is what
you want:

UPDATE Orders
SET ShipName = c.ContactName
FROM Orders o
JOIN Customers c ON c.CustomerID = O.CustomerID

I suspect the problem is that you left out the FROM clause.

I left out INNER here, because this is implied.

I should add your original syntax is in alignment with ANSI standards,
whereas the syntax with FROM JOIN is proprietary to MS SQL Server and
Sybase (and possibly Informix). If you need portability, stick to the
original syntax. As long as you work with SQL Server only, do as you
please. Personally, I find the FROM/JOIN syntax very pleasant, as it
builds on the same paradigm as a regular SELECT statement. It is also
more effecient, if you need to update more than one column.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||David Portas (REMOVE_BEFORE_REPLYING_dportas@.acm.org) writes:
> However, if performance is your concern then why not take SHIPNAME out
> of the Orders table. It looks like it's redundant there.

Nah, I would not recommend people to drop columns from their
Northwind databases. :-)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||>> I find the FROM/JOIN syntax very pleasant, as it builds on the same paradigm as a regular SELECT statement.<<

Arrrgh! The **consistent meaning in the Standard SQL model** of a
FROM clause is that a temporary working table is constructed, used and
dropped at the end of the statement. You would be updating a temporary
working table, not the base table.

The Sybase, Informix and MS SQL Server syntax might look the same, but
the semantics are all slightly different when you get to a 1:m
relationship. Moving the code is deadly -- it moves over to the next
platform and runs differently. With the ANSI syntax, the vendors have
to follow the same rules. This is a good thing.

Sunday, February 19, 2012

ADO Recorset / Set object = Nothing Issue?

I am having an issue of update statements not being
commited (lost). I am using ado 2.7 with vb6.
Here's an example that I am concerned about:
adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
adoRS.AddNew
adoRS.field("field1").value = "abc"
adoRS.update
set adoRS = Nothing
Since I am seting the ADORS object to nothing, is it
possible it's not finished executing and rolls the
transaction back?
Jason Roozeethis is technically the wrong group for this post, but here goes...
You need to make sure your cursor type supports the updates.
If I were you, I would not use rs.Update to post changes back to server.
You should consider using Command objects.
if you want more specifics, post back.
Greg Jackson
PDX, Oregon|||Your life will be a lot easier and your code will run faster if you
use SQL to update/insert/delete data. You can execute it in ADO from
either a Connection or a Command object. You also eliminate that extra
and unnecessary round trip to the database to create the recordset.
cmd.Execute "INSERT INTO MyTable (Field1) " & _
"VALUES ('" & stringvariable & "')"
-- Mary
MCW Technologies
http://www.mcwtech.com
On Fri, 6 Feb 2004 14:31:42 -0800, "Jason Roozee" <jason@.camcoinc.net>
wrote:
>I am having an issue of update statements not being
>commited (lost). I am using ado 2.7 with vb6.
>Here's an example that I am concerned about:
>adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
>adoRS.AddNew
>adoRS.field("field1").value = "abc"
>adoRS.update
>set adoRS = Nothing
>Since I am seting the ADORS object to nothing, is it
>possible it's not finished executing and rolls the
>transaction back?
>Jason Roozee|||Don't use a recordset to affect data. Recordsets are for *retrieving* data.
http://www.aspfaq.com/2191
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Jason Roozee" <jason@.camcoinc.net> wrote in message
news:b4bb01c3ed00$fe683150$a101280a@.phx.gbl...
> I am having an issue of update statements not being
> commited (lost). I am using ado 2.7 with vb6.
> Here's an example that I am concerned about:
> adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
> adoRS.AddNew
> adoRS.field("field1").value = "abc"
> adoRS.update
> set adoRS = Nothing
> Since I am seting the ADORS object to nothing, is it
> possible it's not finished executing and rolls the
> transaction back?
> Jason Roozee

ADO Recorset / Set object = Nothing Issue?

I am having an issue of update statements not being
commited (lost). I am using ado 2.7 with vb6.
Here's an example that I am concerned about:
adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
adoRS.AddNew
adoRS.field("field1").value = "abc"
adoRS.update
set adoRS = Nothing
Since I am seting the ADORS object to nothing, is it
possible it's not finished executing and rolls the
transaction back?
Jason Roozeethis is technically the wrong group for this post, but here goes...
You need to make sure your cursor type supports the updates.
If I were you, I would not use rs.Update to post changes back to server.
You should consider using Command objects.
if you want more specifics, post back.
Greg Jackson
PDX, Oregon|||Your life will be a lot easier and your code will run faster if you
use SQL to update/insert/delete data. You can execute it in ADO from
either a Connection or a Command object. You also eliminate that extra
and unnecessary round trip to the database to create the recordset.
cmd.Execute "INSERT INTO MyTable (Field1) " & _
"VALUES ('" & stringvariable & "')"
-- Mary
MCW Technologies
http://www.mcwtech.com
On Fri, 6 Feb 2004 14:31:42 -0800, "Jason Roozee" <jason@.camcoinc.net>
wrote:

>I am having an issue of update statements not being
>commited (lost). I am using ado 2.7 with vb6.
>Here's an example that I am concerned about :
>adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
>adoRS.AddNew
>adoRS.field("field1").value = "abc"
>adoRS.update
>set adoRS = Nothing
>Since I am seting the ADORS object to nothing, is it
>possible it's not finished executing and rolls the
>transaction back?
>Jason Roozee|||Don't use a recordset to affect data. Recordsets are for *retrieving* data.
http://www.aspfaq.com/2191
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Jason Roozee" <jason@.camcoinc.net> wrote in message
news:b4bb01c3ed00$fe683150$a101280a@.phx.gbl...
> I am having an issue of update statements not being
> commited (lost). I am using ado 2.7 with vb6.
> Here's an example that I am concerned about :
> adoRS.Open "SELECT TOP 1 Field1 FROM MyTable"
> adoRS.AddNew
> adoRS.field("field1").value = "abc"
> adoRS.update
> set adoRS = Nothing
> Since I am seting the ADORS object to nothing, is it
> possible it's not finished executing and rolls the
> transaction back?
> Jason Roozee

ADO puts EXEC in front of my SQL statements?

Hi there,
I'm tracking down a syntax error and have an odd problem.
I'm using the SQL Trace tool to view SQL arriving at the server.
Now, there's two problems...
SQL Server keeps on putting EXEC in front of my statements -
eg:
ENABLE TRIGGER ddlDatabaseTriggerLog ON DATABASE
is changed to
exec ENABLE TRIGGER ddlDatabaseTriggerLog ON DATABASE
And the second problem is that any cursor/dataset is created like:
declare @.p1 int
set @.p1=2
exec sp_prepexec @.p1 output,NULL,N'<my statement>'
select @.p1
Does anyone have an idea why?
--
With regards,
Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.comBecause you are embedding dynamic SQL. Best practice is to put your
code into stored procs and call the procs from ADO.
David Portas
SQL Server MVP
--|||Hello David,

> Because you are embedding dynamic SQL. Best practice is to put your
> code into stored procs and call the procs from ADO.
In this case, not an option :-)
Funny, ad-hoc queries in SQL Server Management Studio or in the
Query Analyzer don't "suffer" from this problem.
Why is that?
With regards,
Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com|||Query Analyzer uses ODBC, not ADO, that's why :)
Jacco Schalkwijk
SQL Server MVP
"Martijn Tonies" <m.tonies@.upscene-removethis.nospam.com> wrote in message
news:%23C1BYjZyFHA.3320@.TK2MSFTNGP14.phx.gbl...
> Hello David,
>
> In this case, not an option :-)
> Funny, ad-hoc queries in SQL Server Management Studio or in the
> Query Analyzer don't "suffer" from this problem.
> Why is that?
>
> --
> With regards,
> Martijn Tonies
> Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
> Server
> Upscene Productions
> http://www.upscene.com
> Database development questions? Check the forum!
> http://www.databasedevelopmentforum.com
>|||
> Query Analyzer uses ODBC, not ADO, that's why :)
Hmm and I thought the ODBC driver wasn't updated to support SQL 2000.
Ah well...
Anyway, how can I get rid of this EXEC stuff then?
With regards,
Martijn Tonies

> --
> Jacco Schalkwijk
> SQL Server MVP
>
> "Martijn Tonies" <m.tonies@.upscene-removethis.nospam.com> wrote in message
> news:%23C1BYjZyFHA.3320@.TK2MSFTNGP14.phx.gbl...
SQL
>|||Just to make sure that we are talking about the same thing: you are
using ADO 2.x, not ADO.NET, right ? And you are using SQL Server 2000
or something else ?
Razvan|||> Just to make sure that we are talking about the same thing: you are
> using ADO 2.x, not ADO.NET, right ? And you are using SQL Server 2000
> or something else ?
No ADO.NET, just plain old ADO.
SQL 2000 and 2005, for that matter.
Haven't tried SQL 7 on this particular thingy yet...
With regards,
Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com|||I'm not an ADO expert, but IIRC there is a method you can call on the
connection (or command) that just sends the SQL to the server with adding
the EXEC stuff. Which one that is you'll have to look up in the ADO
documentation or ask on an ado newsgroup. It might be the CommandType. You
can try setting the CommandType to adCmdText.
Jacco Schalkwijk
SQL Server MVP
"Martijn Tonies" <m.tonies@.upscene-removethis.nospam.com> wrote in message
news:OnYpjrayFHA.460@.TK2MSFTNGP15.phx.gbl...
>
> Hmm and I thought the ODBC driver wasn't updated to support SQL 2000.
> Ah well...
> Anyway, how can I get rid of this EXEC stuff then?
>
> --
> With regards,
> Martijn Tonies
>
> SQL
>|||
> I'm not an ADO expert, but IIRC there is a method you can call on the
> connection (or command) that just sends the SQL to the server with adding
> the EXEC stuff. Which one that is you'll have to look up in the ADO
> documentation or ask on an ado newsgroup. It might be the CommandType. You
> can try setting the CommandType to adCmdText.
It's already adCmdText :-/
I'll have a look at the docs then ... couldn't find it before ...
With regards,
Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com|||Hello, Martijn
In SQL Server 2000, "ENABLE TRIGGER ddlDatabaseTriggerLog ON DATABASE"
is not a valid statement. Try a "ALTER TABLE YourTable ENABLE TRIGGER
SomeNormalTrigger" and it should work. If ADO doesn't recognize your
CommandText as being a valid SQL statement, it tries to add an "exec "
or a "select * from " guessing that it could be a procedure name or a
table/view name.
In SQL Server 2005, that statement should work, but it seems that there
is a bug in the "Microsoft OLEDB Provider for SQL Server" (or it is not
updated for SQL Server 2005). Try using the "SQL Native Client" OLEDB
provider (i.e. "Provider=SQLNCLI.1", instead of "Provider=SQLOLEDB.1"
in the connection string). It worked on my system (using SQL Server
2005 September CTP).
Razvan

Thursday, February 16, 2012

ADO Error Collection in VB With SQL Prints

In Visual Basic using ADO, I am trying to get print
statements from SQL into my VB project. How does query
analyzer get the print statements from the server? I am
able to display the first TWO print statements using the
[recordset].NextRecordset command, but I get an error if
I try to go past the second recordset. Please help if
you can.
[CODE]
Dim objerr As ADODB.Error
On Error GoTo err_check
Do
For Each objerr In gcn.Errors
GetSQLPrints = GetSQLPrints & vbCrLf &
objerr.Description
rs.NextRecordset
Next
Loop
[/CODE]
Only Prints two... I don't know why. Please help.It looks like you're executing a NextRecordset during your Errors collection
iteration. I suggest you complete the iteration before NextRecordset since
multiple messages can be returned along with a recordset.
Below is a VBScript example that shows one method to process multiple
recordsets with messages. In VB, another method is to handle the ADO
Connection InfoMessage event and process the messages in your event handler.
SqlScript = _
"PRINT 'test message 1'" & vbCrLf & _
"PRINT 'test message 2'" & vbCrLf & _
"SELECT 3" & vbCrLf & _
"PRINT 'test message 4'" & vbCrLf & _
"SELECT 5" & vbCrLf & _
"PRINT 'test message 6'" & vbCrLf
Set MyRecordset = MyConnection.Execute(SqlScript)
Message = ""
Do While Not MyRecordset Is Nothing
RecordsetNumber = RecordsetNumber + 1
Message = Message & "Recordset " & _
RecordsetNumber & ":" & VbCrLf
If MyConnection.Errors.Count > 0 Then
For Each SqlError In MyConnection.Errors
Message = Message & vbTab & "Message: " & _
SqlError.Description & vbCrLf
Next
Else
Message = Message & vbTab & _
"No messages." & vbCrLf
End If
If MyRecordset.State = adStateOpen Then
Message = Message & vbTab & _
"Rowset returned." & vbCrLf
Else
Message = Message & vbTab & _
"No rowset returned." & vbCrLf
End If
Message = Message & vbCrLf
Set MyRecordset = MyRecordset.NextRecordset
Loop
Message = Message & "Recordset is Nothing."
MyConnection.Close
MsgBox Message
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike B" <BGates@.Microsoft.com> wrote in message
news:034801c3c671$449addf0$a101280a@.phx.gbl...
> In Visual Basic using ADO, I am trying to get print
> statements from SQL into my VB project. How does query
> analyzer get the print statements from the server? I am
> able to display the first TWO print statements using the
> [recordset].NextRecordset command, but I get an error if
> I try to go past the second recordset. Please help if
> you can.
> [CODE]
> Dim objerr As ADODB.Error
> On Error GoTo err_check
> Do
> For Each objerr In gcn.Errors
> GetSQLPrints = GetSQLPrints & vbCrLf &
> objerr.Description
> rs.NextRecordset
> Next
> Loop
> [/CODE]
> Only Prints two... I don't know why. Please help.|||For our connection, we are using SQLOLEDB...
When we run you code example, getting an error saying:
"Current Provider does not support returning multiple
recordsets from a single execution"
If we take the do-while loop out, we get a message box
saying:
Recordset 1:
Message: text message 1
No recordset returned
Recordset is nothing
We did add the following Dim's:
Dim SqlScript as string
dim Myrecordset as new adodb.recordset
dim recordsetnumber as long
dim sqlerror as error
dim message as string
>--Original Message--
>It looks like you're executing a NextRecordset during
your Errors collection
>iteration. I suggest you complete the iteration before
NextRecordset since
>multiple messages can be returned along with a recordset.
>Below is a VBScript example that shows one method to
process multiple
>recordsets with messages. In VB, another method is to
handle the ADO
>Connection InfoMessage event and process the messages in
your event handler.
>SqlScript = _
> "PRINT 'test message 1'" & vbCrLf & _
> "PRINT 'test message 2'" & vbCrLf & _
> "SELECT 3" & vbCrLf & _
> "PRINT 'test message 4'" & vbCrLf & _
> "SELECT 5" & vbCrLf & _
> "PRINT 'test message 6'" & vbCrLf
>Set MyRecordset = MyConnection.Execute(SqlScript)
>Message = ""
>Do While Not MyRecordset Is Nothing
> RecordsetNumber = RecordsetNumber + 1
> Message = Message & "Recordset " & _
> RecordsetNumber & ":" & VbCrLf
> If MyConnection.Errors.Count > 0 Then
> For Each SqlError In MyConnection.Errors
> Message = Message & vbTab & "Message: " & _
> SqlError.Description & vbCrLf
> Next
> Else
> Message = Message & vbTab & _
> "No messages." & vbCrLf
> End If
> If MyRecordset.State = adStateOpen Then
> Message = Message & vbTab & _
> "Rowset returned." & vbCrLf
> Else
> Message = Message & vbTab & _
> "No rowset returned." & vbCrLf
> End If
> Message = Message & vbCrLf
> Set MyRecordset = MyRecordset.NextRecordset
>Loop
>Message = Message & "Recordset is Nothing."
>MyConnection.Close
>MsgBox Message
>
>--
>Hope this helps.
>Dan Guzman
>SQL Server MVP
>
>"Mike B" <BGates@.Microsoft.com> wrote in message
>news:034801c3c671$449addf0$a101280a@.phx.gbl...
>> In Visual Basic using ADO, I am trying to get print
>> statements from SQL into my VB project. How does query
>> analyzer get the print statements from the server? I am
>> able to display the first TWO print statements using
the
>> [recordset].NextRecordset command, but I get an error
if
>> I try to go past the second recordset. Please help if
>> you can.
>> [CODE]
>> Dim objerr As ADODB.Error
>> On Error GoTo err_check
>> Do
>> For Each objerr In gcn.Errors
>> GetSQLPrints = GetSQLPrints & vbCrLf &
objerr.Description
>> rs.NextRecordset
>> Next
>> Loop
>> [/CODE]
>> Only Prints two... I don't know why. Please help.
>
>.
>|||It looks like there are some differences in behavior between VB and
VBScript. On my system, the VB code below uses Recordset.Open instead of
Connection.Execute and returns the same result as the original VBScript I
posted.
Dim SqlScript As String
Dim MyConnection As New ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim recordsetnumber As Long
Dim sqlerror As Error
Dim message As String
Dim ConnectionString As String
Dim lastError As Integer
ConnectionString = _
"Provider=SQLOLEDB" & _
";Data Source=MyServer" & _
";Integrated Security=SSPI"
MyConnection.Open ConnectionString
SqlScript = _
"SET NOCOUNT ON" & vbCrLf & _
"PRINT 'test message 1'" & vbCrLf & _
"PRINT 'test message 2'" & vbCrLf & _
"SELECT 3" & vbCrLf & _
"PRINT 'test message 4'" & vbCrLf & _
"SELECT 5" & vbCrLf & _
"PRINT 'test message 6'" & vbCrLf
MyRecordset.Open SqlScript, MyConnection
message = ""
Do While Not MyRecordset.ActiveCommand Is Nothing
recordsetnumber = recordsetnumber + 1
message = message & "Recordset " & _
recordsetnumber & ":" & vbCrLf
If MyConnection.Errors.Count > 0 Then
For Each sqlerror In MyConnection.Errors
message = message & vbTab & "Message: " & _
sqlerror.Description & vbCrLf
Next
Else
message = message & vbTab & _
"No messages." & vbCrLf
End If
If MyRecordset.State = adStateOpen Then
message = message & vbTab & _
"Rowset returned." & vbCrLf
Else
message = message & vbTab & _
"No rowset returned." & vbCrLf
End If
message = message & vbCrLf
On Error Resume Next
Set MyRecordset = MyRecordset.NextRecordset
Loop
message = message & "Recordset is Nothing."
MyConnection.Close
MsgBox message
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike B/Steve Z" <szlamany@.antarescomputing.com> wrote in message
news:10e801c3c70e$22406160$a301280a@.phx.gbl...
> For our connection, we are using SQLOLEDB...
> When we run you code example, getting an error saying:
> "Current Provider does not support returning multiple
> recordsets from a single execution"
> If we take the do-while loop out, we get a message box
> saying:
> Recordset 1:
> Message: text message 1
> No recordset returned
> Recordset is nothing
> We did add the following Dim's:
> Dim SqlScript as string
> dim Myrecordset as new adodb.recordset
> dim recordsetnumber as long
> dim sqlerror as error
> dim message as string
>
> >--Original Message--
> >It looks like you're executing a NextRecordset during
> your Errors collection
> >iteration. I suggest you complete the iteration before
> NextRecordset since
> >multiple messages can be returned along with a recordset.
> >
> >Below is a VBScript example that shows one method to
> process multiple
> >recordsets with messages. In VB, another method is to
> handle the ADO
> >Connection InfoMessage event and process the messages in
> your event handler.
> >
> >SqlScript = _
> > "PRINT 'test message 1'" & vbCrLf & _
> > "PRINT 'test message 2'" & vbCrLf & _
> > "SELECT 3" & vbCrLf & _
> > "PRINT 'test message 4'" & vbCrLf & _
> > "SELECT 5" & vbCrLf & _
> > "PRINT 'test message 6'" & vbCrLf
> >
> >Set MyRecordset = MyConnection.Execute(SqlScript)
> >
> >Message = ""
> >Do While Not MyRecordset Is Nothing
> > RecordsetNumber = RecordsetNumber + 1
> > Message = Message & "Recordset " & _
> > RecordsetNumber & ":" & VbCrLf
> > If MyConnection.Errors.Count > 0 Then
> > For Each SqlError In MyConnection.Errors
> > Message = Message & vbTab & "Message: " & _
> > SqlError.Description & vbCrLf
> > Next
> > Else
> > Message = Message & vbTab & _
> > "No messages." & vbCrLf
> > End If
> > If MyRecordset.State = adStateOpen Then
> > Message = Message & vbTab & _
> > "Rowset returned." & vbCrLf
> > Else
> > Message = Message & vbTab & _
> > "No rowset returned." & vbCrLf
> > End If
> > Message = Message & vbCrLf
> > Set MyRecordset = MyRecordset.NextRecordset
> >Loop
> >Message = Message & "Recordset is Nothing."
> >MyConnection.Close
> >MsgBox Message
> >
> >
> >--
> >Hope this helps.
> >
> >Dan Guzman
> >SQL Server MVP
> >
> >
> >"Mike B" <BGates@.Microsoft.com> wrote in message
> >news:034801c3c671$449addf0$a101280a@.phx.gbl...
> >> In Visual Basic using ADO, I am trying to get print
> >> statements from SQL into my VB project. How does query
> >> analyzer get the print statements from the server? I am
> >> able to display the first TWO print statements using
> the
> >> [recordset].NextRecordset command, but I get an error
> if
> >> I try to go past the second recordset. Please help if
> >> you can.
> >>
> >> [CODE]
> >> Dim objerr As ADODB.Error
> >> On Error GoTo err_check
> >> Do
> >> For Each objerr In gcn.Errors
> >> GetSQLPrints = GetSQLPrints & vbCrLf &
> >>
> objerr.Description
> >> rs.NextRecordset
> >> Next
> >> Loop
> >> [/CODE]
> >> Only Prints two... I don't know why. Please help.
> >
> >
> >.
> >|||Thank you - your example worked great in VB as well.
Not sure where our problem was - went in circles for a
while...
>--Original Message--
>It looks like there are some differences in behavior
between VB and
>VBScript. On my system, the VB code below uses
Recordset.Open instead of
>Connection.Execute and returns the same result as the
original VBScript I
>posted.
>Dim SqlScript As String
>Dim MyConnection As New ADODB.Connection
>Dim MyRecordset As New ADODB.Recordset
>Dim recordsetnumber As Long
>Dim sqlerror As Error
>Dim message As String
>Dim ConnectionString As String
>Dim lastError As Integer
>ConnectionString = _
> "Provider=SQLOLEDB" & _
> ";Data Source=MyServer" & _
> ";Integrated Security=SSPI"
>MyConnection.Open ConnectionString
>SqlScript = _
> "SET NOCOUNT ON" & vbCrLf & _
> "PRINT 'test message 1'" & vbCrLf & _
> "PRINT 'test message 2'" & vbCrLf & _
> "SELECT 3" & vbCrLf & _
> "PRINT 'test message 4'" & vbCrLf & _
> "SELECT 5" & vbCrLf & _
> "PRINT 'test message 6'" & vbCrLf
>MyRecordset.Open SqlScript, MyConnection
>message = ""
>Do While Not MyRecordset.ActiveCommand Is Nothing
> recordsetnumber = recordsetnumber + 1
> message = message & "Recordset " & _
> recordsetnumber & ":" & vbCrLf
> If MyConnection.Errors.Count > 0 Then
> For Each sqlerror In MyConnection.Errors
> message = message & vbTab & "Message: " & _
> sqlerror.Description & vbCrLf
> Next
> Else
> message = message & vbTab & _
> "No messages." & vbCrLf
> End If
> If MyRecordset.State = adStateOpen Then
> message = message & vbTab & _
> "Rowset returned." & vbCrLf
> Else
> message = message & vbTab & _
> "No rowset returned." & vbCrLf
> End If
> message = message & vbCrLf
> On Error Resume Next
> Set MyRecordset = MyRecordset.NextRecordset
>Loop
>message = message & "Recordset is Nothing."
>MyConnection.Close
>MsgBox message
>
>--
>Hope this helps.
>Dan Guzman
>SQL Server MVP
>
>"Mike B/Steve Z" <szlamany@.antarescomputing.com> wrote
in message
>news:10e801c3c70e$22406160$a301280a@.phx.gbl...
>> For our connection, we are using SQLOLEDB...
>> When we run you code example, getting an error saying:
>> "Current Provider does not support returning multiple
>> recordsets from a single execution"
>> If we take the do-while loop out, we get a message box
>> saying:
>> Recordset 1:
>> Message: text message 1
>> No recordset returned
>> Recordset is nothing
>> We did add the following Dim's:
>> Dim SqlScript as string
>> dim Myrecordset as new adodb.recordset
>> dim recordsetnumber as long
>> dim sqlerror as error
>> dim message as string
>>
>> >--Original Message--
>> >It looks like you're executing a NextRecordset during
>> your Errors collection
>> >iteration. I suggest you complete the iteration
before
>> NextRecordset since
>> >multiple messages can be returned along with a
recordset.
>> >
>> >Below is a VBScript example that shows one method to
>> process multiple
>> >recordsets with messages. In VB, another method is to
>> handle the ADO
>> >Connection InfoMessage event and process the messages
in
>> your event handler.
>> >
>> >SqlScript = _
>> > "PRINT 'test message 1'" & vbCrLf & _
>> > "PRINT 'test message 2'" & vbCrLf & _
>> > "SELECT 3" & vbCrLf & _
>> > "PRINT 'test message 4'" & vbCrLf & _
>> > "SELECT 5" & vbCrLf & _
>> > "PRINT 'test message 6'" & vbCrLf
>> >
>> >Set MyRecordset = MyConnection.Execute(SqlScript)
>> >
>> >Message = ""
>> >Do While Not MyRecordset Is Nothing
>> > RecordsetNumber = RecordsetNumber + 1
>> > Message = Message & "Recordset " & _
>> > RecordsetNumber & ":" & VbCrLf
>> > If MyConnection.Errors.Count > 0 Then
>> > For Each SqlError In MyConnection.Errors
>> > Message = Message & vbTab & "Message: " &
_
>> > SqlError.Description & vbCrLf
>> > Next
>> > Else
>> > Message = Message & vbTab & _
>> > "No messages." & vbCrLf
>> > End If
>> > If MyRecordset.State = adStateOpen Then
>> > Message = Message & vbTab & _
>> > "Rowset returned." & vbCrLf
>> > Else
>> > Message = Message & vbTab & _
>> > "No rowset returned." & vbCrLf
>> > End If
>> > Message = Message & vbCrLf
>> > Set MyRecordset = MyRecordset.NextRecordset
>> >Loop
>> >Message = Message & "Recordset is Nothing."
>> >MyConnection.Close
>> >MsgBox Message
>> >
>> >
>> >--
>> >Hope this helps.
>> >
>> >Dan Guzman
>> >SQL Server MVP
>> >
>> >
>> >"Mike B" <BGates@.Microsoft.com> wrote in message
>> >news:034801c3c671$449addf0$a101280a@.phx.gbl...
>> >> In Visual Basic using ADO, I am trying to get print
>> >> statements from SQL into my VB project. How does
query
>> >> analyzer get the print statements from the server?
I am
>> >> able to display the first TWO print statements using
>> the
>> >> [recordset].NextRecordset command, but I get an
error
>> if
>> >> I try to go past the second recordset. Please help
if
>> >> you can.
>> >>
>> >> [CODE]
>> >> Dim objerr As ADODB.Error
>> >> On Error GoTo err_check
>> >> Do
>> >> For Each objerr In gcn.Errors
>> >> GetSQLPrints = GetSQLPrints & vbCrLf &
>> >>
>> objerr.Description
>> >> rs.NextRecordset
>> >> Next
>> >> Loop
>> >> [/CODE]
>> >> Only Prints two... I don't know why. Please help.
>> >
>> >
>> >.
>> >
>
>.
>