Solved

RAISERROR and SQL 2000 - Errors and Omissions

Posted on 2002-04-21
11
453 Views
Last Modified: 2012-06-22
I have an issue that I have seen posted all around newsgroups but I haven't yet come accross an adequate answer.

I am using VB6 (SP5), SQL Server 2000 and the latest SQl OLE DB driver (MSDAC 2.7).

In summary, if I use RAISERROR as the first line in a Stored Procedure then VB will respond to it, however if it occurs after a SELECT (or just about any other) statement then it becomes invisible to VB.  The errors are reported correctly when the procedure is executed within Query Analyser.

I have tried changing the RAISERROR priority from 16 to 10 and I have tried the OLE ODBC driver - both with no success.  I have been told that the SQL Server installation contains all of the latest patches.

I have included below a sample Stored Procedure followed by the VB code I am using.  Please do not 'guess' at this one - only respond if you have come accross this issue before.

CREATE PROCEDURE [dbo].[uspRaiseTheError] AS

SET NOCOUNT ON

SELECT * FROM sysobjects

RAISERROR ('Talk to me',16,1)


SET NOCOUNT OFF
GO


The VB:

Private Sub Command1_Click()
    On Error GoTo ErrHandler

    Dim c As Connection
   
    Set c = New Connection
   
    c.CursorLocation = adUseClient
    c.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=EDRMSX;Data Source=EDRMS"
   
    'This stored procedure reports on objects not installed correctly by raising errors
    c.Execute "EXEC dbo.uspRaiseTheError"
   
    c.Close
   
    Set c = Nothing
   
    Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation, "Error"

End Sub
0
Comment
Question by:gawilson2000
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6959203
Check out this:

Private Sub Command1_Click()
   On Error GoTo ErrHandler

   Dim c As Connection
   dim r as Recordset
   
   Set c = New Connection
   
   c.CursorLocation = adUseClient
   c.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=EDRMSX;Data
Source=EDRMS"
   
   'This stored procedure reports on objects not installed correctly by raising errors
   set r = c.Execute ("EXEC dbo.uspRaiseTheError")
   while not(r is nothing)  
     set r = r.NextRecordset
   wend

   c.Close
   
   Set c = Nothing
   
   Exit Sub

ErrHandler:
 MsgBox Err.Description, vbExclamation, "Error"

End Sub

CHeers
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6960646
I think one problem might be that you're not issuing a return statement after the RAISERROR statement:

    IF  @@error <> 0
    BEGIN
        SET @sql_error = @@error
        RAISERROR('Error updating table', 16, 1)
        ROLLBACK TRANSACTION
        RETURN @sql_error
    END

A couple of things to try.  Try putting the RETURN statement in the proc, Try using a command object's Execute method instead of the connection object's Execute method - setting the CommandType to adStoredProc instead of adCommandText and removing the Exec from the CommandText so that it just reads:

oCMD.CommandText = "dbo.uspRaiseTheError"

Of course this is all just a guess ;)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6960690
Did you see the following article?
HOWTO: Retrieve Values in SQL Server Stored Procedures with ADO (Q194792)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q194792

Anthony
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:gawilson2000
ID: 6961528
I'm disappointed that no one has come accross this before.

Angelll's solution might work - but if it did, it would only work for this example of the problem.  Not very helpful.

mdougan is only guessing - when it comes down to it.  I tried adding a RETURN statement but with no success.  I'm not going to the trouble of changing my method of access when no reason to do so has been supplied.

acperkins I think is on the right track.  This issue should be documented somewhere - someone would have come accross it before.  However the article supplied doesn't explain what key element enables the reporting of errors or why errors aren't being reported in the standard fashion.  Do I have to replace some simple code for the cumbersome Command Object and adding one parameter at a time?  I have not yet tried the code supplied because even if it was correct it would take me hours to decifer it into something that relates to my issue.  I will have to come back to this if no one can give me something simpler and more to the point.

I have supplied a simple sample of code.  Can't someone install it on their system an find a solution?  And a generic solution for a variety of stored procedures with a variety of RAISERROR calls being generated for many different reasons?

Aren't VB programmers out there using RAISERROR?

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6961569
>> Aren't VB programmers out there using RAISERROR?<<
Sorry we all use the "cumbersome" Command object.

<unsubscribing>

Anthony
0
 
LVL 5

Author Comment

by:gawilson2000
ID: 6961678
I have been searching the Microsoft KB and have come accross:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q254304
which supplied a glimmer of hope but it hasn't worked either.
0
 
LVL 5

Author Comment

by:gawilson2000
ID: 6961680
Is it true that everyone uses the Command object to return RAISERRORs?
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6961861
gawilson2000,

I'm only guessing that this will solve your problem, because this is what I use and I've never experienced the problems that you are currently experiencing.  It's really arrogant of you to dismiss other's suggestion without trying them.  If you can't be bothered to try suggestions that work for the people who made them, and might work for you as well, then I guess you deserve to rot with your problem.  But, then, I'm only guessing ;)

Unsubscribing...
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 6962063
Hi,

  first of all, my suggestion DOES work. And I don't understand what is "bad" on this suggestion, as you could place the necessary code into a procedure, and hide this from the normal application code:

Public Sub ExecuteSQL (c as ADODB.Connection, sql as string)
dim r as ADODB.Recordset
set r = c.execute(sql)
while not (r is nothing)
  set r = r.nextrecordset
wend
End Sub

If there is an error of whatever kind (runtime or RAISERROR), the calling code will get it, and doesn't "see" that you need to have this small workaround...

Anyway, I agree with mdougan's point.

CHeers
0
 
LVL 5

Author Comment

by:gawilson2000
ID: 6992839
Thanks for your suggestion and better attitude than some.

I've been described as arrogant but at least I can take constructive criticism.  You also have taken criticism well.

I'm sorry I doubted your solution so much - it just didn't look like it would work and looked like the guess of a junior.

I waded through the suggestions given and found that even the comprehensive technical article that was supplied to me boiled down to a similar solution to that which you provided.

I've graded your answer as B because like the others, you never stated that your suggestion is tried and true for you and attacks the issue directly (or why it works) - until late.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6993358
Don't mind about the grade, i am glad to help.

Of course, I don't state for each suggestion if i tried them out, as most of the time, i write out of memory, as most of the time i don't have the tools on the workstation i am currently on. Anyway, for most q, i don't have the time to verify each and every line of code...
Furthermore, if a questioner asks for more information about the suggestion, i can always provide more information if needed. again, it's a time of question.

Hope you get the best of EE
CHeers
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question