Sunteți pe pagina 1din 2

Macro can have multiple SQL statements within it, if a macro contains DDL,it mu st be the last statement in the

macro.The reason for this is based on the transactional nature of a macro. Since DDL locks one or more rows withi n the DD and this could prevent user access to the DD, it is desirable to releas e these locks as soon as possible. Therefore, a macro?s DDL transaction needs t o finish quickly. Hence, you can only have one DDL statement within a macro. ------------------------------------------------------------------------Create a macro to generate a DOB list for department 321: CREATE MACRO DOB_Details AS (SELECT first_name ,last_name ,DOB FROM TERADATA.employees WHERE dept_numbr =321 ORDER BY DOB asc;); ------------------------------------------------------------------------EXECUTE a Macro To execute a macro, call it along with the exec command. EXEC DOB_Details; last_name Ram Laxman first_name Kumar Sinha DOB 75/02/22 79/04/06

------------------------------------------------------------------------DROP a Macro To drop a macro, use following command . DROP MACRO DOB_Details; ------------------------------------------------------------------------REPLACE a Macro If we need to modify an existing macro , instead of dropping and re-creating it We can use replace macro command as follows REPLACE MACRO DOB_Details AS (SELECT first_name,last_name ,DOB FROM TERADATA.employees WHERE dept_numbr = 321 ORDER BY DOB, first_name;); ------------------------------------------------------------------------Parameterized Macros Parametrized macros allow usage of variables . we can pass values to these vari ables. Advantage of using parametrized macros is , Values can be passed to thes e variables at run-time. Example CREATE MACRO dept_list (dept INTEGER) AS ( SELECT last_name FROM TERADATA.employees WHERE dept_numbr = :dept; );

To Execute the macro EXEC dept_list (321); ------------------------------------------------------------------------Macros may have more than one parameter CREATE MACRO emp_verify (dept_numbr INTEGER ,salary DEC(18,0)) AS ( SELECT emp_numbr from TERADATA.employees WHERE dept_numbr = :dept AND salary< :sal;) ; To Execute this macro EXEC emp_check (301, 50000); ------------------------------------------------------------------------What is diffeerence between macro and procedure? SP: It does not return rows to the user. It has to use cursors to fetch multiple rows It used Inout/Out to send values to user It Contains comprehensive SPL It is stored in DATABASE or USER PERM A stored procedure also provides output/Input capabilities Macros: It returns set of rows to the user. It is stored in DBC SPOOL space A macro that allows only input values which gud in term of performance? 1. If you have set of sql queries to be run and large number of output statemen ts then go for macro 2. If you have some logic to be implemented and based on some conditions the qu eries should execute then go for procedures.

S-ar putea să vă placă și