开发者

SQL Try Catch the exact errors caused by the recent variables

开发者 https://www.devze.com 2023-01-23 04:38 出处:网络
Query: BEGIN TRY SELECT @AccountNumber, @AccountSuffix, @Sedat, @Dedo, @Payalo, @Artisto FROM SWORDBROS WHERE AMAZING =\'HAPPENS\'

Query:

   BEGIN TRY
     SELECT @AccountNumber,
            @AccountSuffix,
            @Sedat,
            @Dedo,
            @Payalo,
            @Artisto
   FROM SWORDBROS
    WHERE AMAZING ='HAPPENS'

    END TRY
    EGIN CATCH
       Print @Sedat
    END CATCH

How can I get the @Sedat, is it possible? SQL 2005开发者_运维百科 , it will be in an SP


Like this, no?

BEGIN TRY
    SELECT @AccountNumber,
        @AccountSuffix,
        @Sedat,
        @Dedo,
        @Payalo,
        @Artisto
    FROM SWORDBROS
    WHERE AMAZING ='HAPPENS'

END TRY
BEGIN CATCH
   --error handling only
END CATCH

--There is no finally block like .net
Print @Sedat


IN a proc when I want to trap the exact values that caused an erorr, this is what I do. I declare a table variable (very important must be a table variable not a temp table) that has the fields I want to have information on. I populate the table variable with records as I go. In a multitep proc, I would add one record for each step if I wanted to see the who process or only a record if I hit an error (which I would populate in this case in the catch block typically). Then in The catch block I would rollback the transaction and then I would insert the contents of the table varaible into a permanent exception processing table. You could also just do a select of this table if you wanted, but if I'm going to this much trouble it usually is for an automated process where I need to be able to research the problem at a later time, not see the problem when it hits becasue I'm not running it on my mchine or where I could see a select or print statement. By using the table varaible which stay in scope even after the rollback, my information is still available for me to log in my exception logging table. But it important that you do the logging to any permananent table after the rollback or the process will rollback with everything else.


which database are you using? also, which programming language is this?

usually there would be an INTO clause and some local variables declared.

your query should also have a FROM clause at a minimum


It is not clear if you are expecting the returned values to be placed into the @ variables or whether you are trying to dynamically specify which columns you want selected. In a Sql Server stored procedure you usually return a result set, not a bunch of individual variables. The syntax you have will not work if you want column values returned since what you have will dynamically specify which columns are wanted based on the column names passed into the stored procedure. And this will not work since the stored procedure must know which columns you are going after when it is analyzed as it is stored. Now the except clause will be trigged if there is a problem reading from the database (communication down, disk error, etc.) in which case none of the column values will be known.

Use the Sql Query Analyzer tool (under the "Tools" menu in SqlManager after you have selected a database) to define your stored procedure and test it. If you installed the documentation when you installed SqlManager go to Start>Programs>Microsoft Sql Server>Books Online and open the "Transact-SQL Reference" node for documentation on what can be done.

0

精彩评论

暂无评论...
验证码 换一张
取 消