• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

RAISERROR and SQL 2000 - Errors and Omissions

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
gawilson2000
Asked:
gawilson2000
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
mdouganCommented:
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
 
Anthony PerkinsCommented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
gawilson2000Author Commented:
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
 
Anthony PerkinsCommented:
>> Aren't VB programmers out there using RAISERROR?<<
Sorry we all use the "cumbersome" Command object.

<unsubscribing>

Anthony
0
 
gawilson2000Author Commented:
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
 
gawilson2000Author Commented:
Is it true that everyone uses the Command object to return RAISERRORs?
0
 
mdouganCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
gawilson2000Author Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now