Vous êtes sur la page 1sur 7

http://readvitamin.

com/teradata/

What are SET operators?


SET operators are used to manipulate the results sets of two or more queries by combining the results of each individual query into a single results set. In Teradata we have ANSI standard SET operators: INTERSECT, UNION, and EXCEPT, as well as the Teradata extensions to ANSI as MINUS. One good thing to note about these operators is that the separate SELECT statements run in parallel, prior to being combined. Additionally, they all run as a single transaction. Here is the way the query is written. SELECT one_statement UNION [ALL] SELECT another_statement MINUS [ALL] SELECT one_more_statement INTERSECT [ALL] SELECT last_statement; Rules for SET Operations. 1. Both the SELECT statements must have the same number of columns with the same attributes. 2. 3. 4. a. b. c. Casting and formatting has to be done in the 1st SELECT statement. Note that there is only one semi colon used at the end of the last SELECT statement. While using multiple SET Operators, follow the order of precedence. INTERSECT operation is performed first Then it is the turn of the UNION operator(s) Then EXCEPT and at the last MINUS operation is performed.

5. By default the duplicate rows are eliminated from the answer sets but if at all you want you can get the same by the use of another operator ALL.

How to Create a View in Teradata SQL?


As you might know a View in SQL is nothing but a SELECT statement, sometimes more than one too, that is stored in the Data Dictionary and is executed when you want to use it. It does not have any physical memory for data; the data that you see in a View comes from tables that view is accessing. So why do we create a View, most of the time for reasons of security, to limit the direct access or to have restricted access to the data by user. This helps in unwanted modification of business sensitive data. Also Views are created for various uses of data analysis and mining. But following caution should be taken while creating a View;  No Indexes should be created on Views,

 If you specify a TOP n or TOP m PERCENT option, then only you can specify an ORDER BY clause otherwise NO.  When referencing multiple tables make use of indexes for faster access.

Here is the syntax: CREATE VIEW <view-name> [( <column alias name1>,... ) ] AS LOCK [TABLE | VIEW | ROW ] FOR | IN [READ | WRITE | ACCESS ] MODE NOWAIT SELECT [DISTINCT | ALL ] TOP <n|m> PERCENT [WITH TIES] <column-name> [AS <alias-name> ] [ , <column-name> [AS <alias-name> ] ] FROM <table-name> <JOIN CONDITION | SUB QUERY> [ WHERE <search condition> GROUP BY <grouping specification> [ WITH CHECK OPTION ] HAVING <condition >]

ORDER BY <expression> [ASE | DESC];

Use the clause WITH CHECK OPTION for updatable Views which helps in restricting the rows that can be updated in the table by an INSERT or UPDATE statement to satisfy the condition in WHERE clause. In other words, use this clause if you want to insert rows that satisfies the WHERE clause, present in the View definition. If you do not specify this clause, rows would be inserted ignoring the WHERE clause but base table checks and constraints remain enforced.

How To Create a Temporary/Session Variable in Teradata.


There is no direct answer to this question as this concept is present and used only in Stored Procedures and Macros in Teradata. But there are ways to replicate this functionality in the command prompt. As said before create a macro and pass a variable. Remember you can not use dynamic SQLs in a Macro. Also you can create a volatile (temporary) table which lives until the session is active, so here is the code on how to create a volatile table. Suppose you want to hold todays date in a temp variable/table here is the way to do it. CREATE VOLATILE TABLE today_date AS (SELECT DATE) WITH DATA ON COMMIT PRESERVE ROWS; In case you do not want to see the results after you run the query, then use DELETE in place of PRESERVE which deletes the row as soon as the query finishes. This is helpful if you are doing any transactional query. If you are wondering how to create macro, here is the way. CREATE MACRO macro_name (<param1> datatype, <param2> datatype) AS ([INSERT;][UPDATE;][DELETE;][SELECT;] ); For example; CREATE MACRO my_macro( InVal INTEGER) ( Update mytable set mycolumn = mycolumn *5/100 WHERE myothercolumn = :InVal; SELECT mycolumn, myothercolumn From myothertable WHERE myothercolumn = :InVal); How to execute/run this Marco, here is how; EXECUTE (EXEC) <macro_name> [parameter value list] For Example; EXEC my_macro ( 4545);EXEC my_macro ( InVal = 4545); If you have more than one parameters and using name for value assignment, the sequence of the parameter in the EXEC does NOT matter.

What is equivalent to Oracles Dual table in Teradata? Good question, actually there is no such table present in Teradata. But the same functionality can be derived by the following code. Before that just to remind that the Dual table in Oracle is used for intermediate calculations, like selecting a date and calculating something. In Teradata it is done without the use of any such table. In Oracle if you are using SELECT SYSDATE FROM DUAL In Teradata the same can be achieved by the following shorter command; SELECT DATE Isnt it COOL! How to Change your Password in Teradata This command is pretty similar to the command in Oracle. MODIFY USER userid AS PASSWORD = yournewpassword

Triggers in Teradata SQL


First lets start with the question, what are Triggers? Triggers are database operations which are caused by data modification events performed in column(s) of a table. In simple words, Triggers are event driven operations. The events are nothing but data modification phenomenon such as insert, update, delete etc. It can consist of a single SQL statement or a block of SQL statements. Triggers are used to reduce manual intervention for tasks which are reparative in nature. Like auto update, data integrity etc. Triggers are of two types; Row level triggers Row level triggers executes once for each row of the table that is being modified by the triggering event. Statement level triggers Statement triggers on the other hand, executes only once upon the execution of the entire triggering statement. Again the above triggers can be executed BEFORE any event occurs or AFTER. In the BEFORE triggers, the triggering action (action needs to be done like inserting a row) is only initiated but is not completed. Control then flows to the triggered action (check on the row in the same table). After the triggered action is complete, control returns to the triggering event, which then (if all went well) completes its job (inserts the row into the table). In case of AFTER triggers, first, control flows to the trigger event (update a row) upon completion only the control passes to the triggered action (insert a new row regarding the update). To add to the list there are also a couple of more types of triggers; INSTEAD OF and CASCADING. Former one is used perform something when the original trigger could not be completed. For example, on a particular UPDATE, you want to INSERT into a table but could not due to some error, then log this message to another table using INSTEAD OF trigger. On the other hand CASCADING triggers are triggers fired by another trigger. TriggerB is fired by TriggerA on some update performed. Syntax for creating a trigger; CREATE[REPLACE] TRIGGER <trigger_name> [ENABLED|DISABLED] BEFORE|AFTER INSERT|DELETE|UPDATE OF <column name> ON <table name> REFERENCING OLD[row|table] AS <before processed row> NEW[row|table] AS <after processed row> FOR EACH ROW | STATEMENT WHEN (search_condition SQL statement) (<triggered action to be performed >;);

Note the two semi-colons, first one is for the triggered action statement and the 2nd one is for the entire CREATE TRIGGER statement.

How to Create a Stored Procedure in Teradata?


If there is anything lacking in SQL to make it a complete programming language, Stored Procedures fulfills the same. Stored Procedures are written in SQL and consist of a set of control and condition handling statements. These features provide a server-based procedural interface to the Teradata Database for application programmers. What does this mean? It is simple, when you use Teradata from any client software such as SQL Assistant, then Teradata becomes the server and the SQL Assistant becomes the client. But when you call a Stored Procedure from SQL Assistant, at this time the Stored Procedure itself becomes the server to the SQL Assistant and client to Teradata DB. Thus it acts as an interface in between SQL Assistant and Teradata DB. Things to NOTE while creating a Stored Procedure.

1. Stored Procedure is a database Object and DDL is used to create it. 2. The set of statements that forms the primary task of a Stored Procedure contains 3. 4. 5. 6. 7. 8.
within, what is called a Stored Procedure body. Stored Procedure body can be either a single state a compound statement. Single statement procedure can contain only one looping mechanism or SQL statement, excluding Cursors. Where as compound statements can have multiple statements but it has to be within the BEGIN and END block. It is not a good practice to use DDL statements within a Stored Procedure. It is stored in USER DATABASE space. Can have parameters like IN, OUT and INOUT.

Example: CREATE PROCEDURE MyFirstProc (IN emp_number INTEGER, IN dept_number INTEGER, OUT dept_name CHAR(10), INOUT errstr VARCHAR(30)) BEGIN INSERT INTO Employee (EmpNo, DeptNo )

VALUES (emp_number, dept_number); SELECT DeptName INTO dept_name FROM Department WHERE DeptNo = dept_number; END; To execute/call the above Stored Procedure: CALL MyFirstProc (495, 211);

Transaction Management
The principle objective of a transaction is to facilitate data completeness when a sequence of events happens. Although it is not necessary that a sequence of events has to take place. A single event can also be considered as a transaction. A single event could be any data manipulation events like INSERTING, UPDATING or DELETING a row or set of rows. Transaction is a logical unit of work. When we say a logical unit, it means the unit of work has a logic involved in it and this unit of work has to be performed in its completeness or not at all. A transaction cannot be completed partially. For example, lets say in an organization there is a change in the employees salary. This revised salary has to be applied to all the employees or none of the employees. Similarly when a bank pays interest on Savings Account, it has to pay interest on all the Savings Account that is present in the books or NONE.

Vous aimerez peut-être aussi