MIS 385/MBA 664 Systems Implementation with DBMS/ Database ManagementProcedures, triggers & macros in TeradataTriggers in TeradataOne sample triggerGeneral syntax for triggersCode for delete_customer triggerShow date/time with SELECT & EXTRACTThe procedure from the bookEmbedded and Dynamic SQLMIS 385/MBA 664Systems Implementation with DBMS/Database ManagementDave [email protected] (email)http://www.davesalisbury.com/ (web site)Procedures, triggers & macros in TeradataStored ProceduresProgram modules that execute on demandWritten in SQLControl and condition handling statementsMore powerful than macros; more sophisticated procedural logic and error handlingTriggers Routines that execute in response to a database evente.g. INSERT, UPDATE, or DELETEMacrosSeries of SQL statementsExecutable with a single commandTriggers in TeradataTriggers are procedures associated with a tableFire upon meeting of trigger conditionUsed in database to react to specific situationsenforcing data integrity rulesderiving specific values Two general typesRow triggersStatement triggersSimple example of using a trigger might be the autonum datatype in Access (when calling a stored procedure to increment the value by 1)One sample triggerThis sample will store the following fields in a table (old_customer_t) on the occurrence of the event (deletion of a customer (NOTE: need to create the table first).Customer_IDCustomer_NameCustomer_CityCustomer_StateGeneral syntax for triggersCREATE[REPLACE] <trigger_name>ENABLED[DISABLED] BEFORE|AFTERINSERT|DELETE OF <column_name>ON <table_name>REFERENCING OLD [row|table] AS <before processed row>NEW [row|table] AS <after processed row>FOR EACH ROW|STATEMENTWHEN (search_condition SQL statement)<triggered action to be performed>;Code for delete_customer triggerCREATE TRIGGER delete_customer AFTER DELETE ON CUSTOMER_TREFERENCING OLD as OFOR EACH ROWINSERT INTO ARCHIVE_CUSTOMER_T (O.Customer_ID, O.Customer_Name, O.Customer_City, O.Customer_State);Show date/time with SELECT & EXTRACTTo see current SYSTEM DATESELECT DATE; - gives output as “11/11/2008″EXTRACTSELECT EXTRACT (MONTH FROM date); - “11”SELECT EXTRACT (YEAR FROM date); - “2008”SELECT EXTRACT (DAY FROM date); - “11”For HOUR MINUTE and SECONDS extractions, use TIME instead. SELECT TIME; - gives output as “18:25:01”SELECT EXTRACT (MINUTE FROM time); - “18”SELECT EXTRACT (HOUR FROM time); - “25”SELECT EXTRACT (SECOND FROM time); - “01”All of the above can be implemented using macrosThe procedure from the bookFirst alter the product_t tableThen create the procedureThen call the procedureEmbedded and Dynamic SQLEmbedded SQLIncluding hard-coded SQL statements in a program written in another language such as C or JavaDynamic SQLAbility for an application program to generate SQL code on the fly, as the application is
View Full Document