12.07.2015 Views

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

Solution Guide for Migrating Oracle on UNIX to SQL Server - Willy .Net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

204 Developing: Applicati<strong>on</strong>s — <str<strong>on</strong>g>Migrating</str<strong>on</strong>g> <str<strong>on</strong>g>Oracle</str<strong>on</strong>g> <strong>SQL</strong> and PL/<strong>SQL</strong>1. Declare Transact-<strong>SQL</strong> variables <strong>to</strong> c<strong>on</strong>tain the data returned by the cursor. Declare<strong>on</strong>e variable <str<strong>on</strong>g>for</str<strong>on</strong>g> each result set column. Declare the variables <strong>to</strong> be large enough<strong>to</strong> hold the values returned by the column.2. Associate a Transact-<strong>SQL</strong> cursor with a SELECT statement using the DECLARECURSOR statement. The DECLARE CURSOR statement also defines thecharacteristics of the cursor, such as the cursor name and whether the cursor isread-<strong>on</strong>ly or <str<strong>on</strong>g>for</str<strong>on</strong>g>ward-<strong>on</strong>ly.3. Use the OPEN statement <strong>to</strong> execute the SELECT statement and populate thecursor.4. Use the FETCH INTO statement <strong>to</strong> fetch individual rows and have the data <str<strong>on</strong>g>for</str<strong>on</strong>g>each column moved in<strong>to</strong> a specified variable. Other Transact-<strong>SQL</strong> statements canthen reference those variables <strong>to</strong> access the fetched data values. Transact-<strong>SQL</strong>cursors do not support fetching blocks of rows.5. When you are finished with the cursor, use the CLOSE statement. Closing a cursorfrees some resources, such as the cursor's result set and its locks <strong>on</strong> the currentrow, but the cursor structure is still available <str<strong>on</strong>g>for</str<strong>on</strong>g> processing if you reissue an OPENstatement. Because the cursor is still present, you cannot reuse the cursor name atthis point. The DEALLOCATE statement completely frees all resources allocated <strong>to</strong>the cursor, including the cursor name. After a cursor is de-allocated, you mustissue a DECLARE statement <strong>to</strong> rebuild the cursor.Th e cursor syntax differs between <str<strong>on</strong>g>Oracle</str<strong>on</strong>g> and <strong>SQL</strong> <strong>Server</strong>:● <str<strong>on</strong>g>Oracle</str<strong>on</strong>g>●DECLARECURSOR IS < SELECT STATEMENT>BEGINFOR IN CURSOR_NAMELOOP-- Business LogicEND LOOP;END;<strong>SQL</strong> <strong>Server</strong>DECLARE DECLARE CURSOR FOR BEGINOPEN FETCH NEXT FROM INTO WHILE @@FETCH_STATUS = 0BEGIN-- business logic goes hereFETCH NEXT FROM INTO ENDCLOSE DEALLOCATE END<str<strong>on</strong>g>Oracle</str<strong>on</strong>g> allows the entire cursor record <strong>to</strong> be transferred in<strong>to</strong> a user-defined data structuredefined using ROWTYPE. In <strong>SQL</strong> <strong>Server</strong>, the record values need <strong>to</strong> be assigned in<strong>to</strong> anindividual variable. The following example uses ROWTYPE <strong>to</strong> process the SHIPPERStable record byrecord.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!