150 likes | 305 Views
Stored Procedures & Operations Navigator. Week 10. SPL. SPL - Declarations. SPL supports all data types available in the Create Table statement SPL does not support User Defined Types SPL does not support Arrays Declare QtyToXfr Int Default 0;
E N D
SPL - Declarations • SPL supports all data types available in the Create Table statement • SPL does not support User Defined Types • SPL does not support Arrays Declare QtyToXfr Int Default 0; Creates a variable called QtyToXfr as an integer and assigns 0 as the default value
SPL - Assignment Set XfrQtyActual = 0; • Test a local variable or parameter If QtyToXfr is not Null then…
SPL – Data definition and Manipulation Statement • Use SQL data definition and manipulation statements similar to embedded SQL. • You don’t need to use Exec SQL and End-Exec. • You don’t need to prefix variables with an ‘:’.
SPL – Conditional (Selection) Statements • If statements If XfrPartId = 123 then set QtyToXfr = 1; ElseIf XfrToRqs > 100 then set QtyToXfr = 100; Else set QtyToXfr = XfrQtyRqs; End if; • The above statement has 3 possible conditions • If the part number = 123 • If the XfrToRqs > 100 • otherwise
SPL – Conditional (Selection) Statements • Case Statements Case PartId When 123 then Set QtrToXfr = 1; When 234 then Set QtrToXfr = 10; Else Set QtyToXfr = XfrQtyRqs; End Case; • This statement has 3 conditions • If PartId = 123 • If PartId = 234 • otherwise
SPL – Conditional (Selection) Statements • Case Statements Case When XfrPartId = 123 then Set QtyToXfr = 1; When XfrPartId = 234 then Set QtyToXfr = 100; Else Set QtyToXfr = XfrQtyRqs; End Case; • This condition has 3 conditions • If XfrPartId = 123 • If XfrpartId = 234 • Otherwise
SPL – Loop Control Structures(Iteration) • 4 loop structures • Loop • infinite loop (use the Leave statement to exit) • While • Test is at the beginning of the loop • Repeat • Test is at the end of the loop • For • Iterate over a set of rows
SPL – Loop Control Structures(Iteration) • Loop Read: Loop If EOF = ‘Y’ then Leave Read; End If; End Loop Read;
SPL – Loop Control Structures(Iteration) • While While (EOF = ‘N’) Do End While;
SPL – Loop Control Structures(Iteration) • Repeat Repeat Until (EOF = ‘Y’) End Repeat;
SPL – Loop Control Structures(Iteration) • For For InvRow as InvCursor cursor for Select Qty From Inventory Where PartId = CurPartId Do End For; • Builds a cursor full of data, reads a record from the cursor, process it, and then reads the next record. This loop continues until all the records in the cursor are processed.
Write a Stored Procedure that: • Reads through ACTRANSPF and updates records as follows: • Trans Type BD – change to 500 • Trans Type FD – increase by 5% • Trans Type LS – change to 40