शुरू करेंमुफ़्त में शुरू करें

Stacked diagnostics प्राप्त करना

Stacked diagnostics PostgreSQL के आंतरिक error message और exception विवरण ला सकता है. आइए patients टेबल पर फिर लौटें और एक ऐसा A1C जोड़ने की कोशिश करें जो testing limit से ऊपर हो. इससे check constraint exception उठेगा जिसे हम कैप्चर कर सकते हैं. Exception handler में हम stacked diagnostics का उपयोग करके अपनी error recording को और समृद्ध कर सकते हैं.

यह अभ्यास पाठ्यक्रम का हिस्सा है

PostgreSQL में Transactions और Error Handling

पाठ्यक्रम देखें

अभ्यास निर्देश

  • DECLARE में दो वैरिएबल exc_message और exc_detail को text के रूप में घोषित करें.
  • Diagnostics stack प्राप्त करें और exc_message को MESSAGE_TEXT तथा exc_detail को PG_EXCEPTION_DETAIL से सेट करें.
  • exc_message और exc_detail को errors टेबल के msg और detail फ़ील्ड में insert करें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

DO $$ 
-- Declare our text variables: exc_message and exc_detail 
___
   ___ text;
   exc_detail ___;
BEGIN 
    INSERT INTO patients (a1c, glucose, fasting) 
    values (20, 89, TRUE);
EXCEPTION 
WHEN others THEN
    -- Get the exception message and detail via stacked diagnostics
	___ ___ ___ 
    	exc_message = ___,
        exc_detail = ___;
    -- Record the exception message and detail in the errors table
    INSERT INTO errors (___, detail) VALUES (exc_message, ___);
END;
$$ language 'plpgsql';

-- Select all the errors recorded
SELECT * FROM errors;
कोड संपादित करें और चलाएँ