|
| 1 | +--- |
| 2 | +title: "DB22SS0239: ROWID column not accessible (Error)" |
| 3 | +description: "Describes the reason why SQL Server Migration Assistant (SSMA) for DB2 requires a ROWID column to be defined." |
| 4 | +author: "nahk-ivanov" |
| 5 | +ms.prod: sql |
| 6 | +ms.technology: ssma |
| 7 | +ms.devlang: "sql" |
| 8 | +ms.topic: "article" |
| 9 | +ms.date: "10/15/2020" |
| 10 | +ms.author: "alexiva" |
| 11 | +--- |
| 12 | + |
| 13 | +# DB22SS0239: `ROWID` column not accessible (Error) |
| 14 | + |
| 15 | +This article describes the reason why SQL Server Migration Assistant (SSMA) for DB2 requires a `ROWID` column to be defined for the table with triggers. |
| 16 | + |
| 17 | +## Background |
| 18 | + |
| 19 | +In DB2 you can create a trigger that will run `FOR EACH ROW`, rather than for the entire set of rows that is changing. In SQL Server triggers always execute for the entire set of modified rows. If row-level DB2 trigger accesses both - `OLD` and `NEW` correlation names, then SSMA needs a way to match rows from both row sets in order to identify what the value was for a given row before and after the update. To emulate such "for each row" functionality, SSMA adds special `ROWID` column to uniquely identify each modified row and uses it to establish a relationship between `inserted` and `deleted` row sets. |
| 20 | + |
| 21 | +This behavior is controlled by the **Generate ROWID column** project setting, which can be found under **Tools** - **Project Settings** - **General** - **Conversion** - **ROWID generation**. If the setting is **Off**, then `ROWID` column will not be added to the table and SSMA will not be able to convert such triggers. |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +Consider the following DB2 trigger, that executes for each row updated in `TRIG_TEST` table: |
| 26 | + |
| 27 | +```sql |
| 28 | +CREATE OR REPLACE TRIGGER TSCHM.TRIG_TEST_AU |
| 29 | +AFTER UPDATE OF DATA ON TSCHM.TRIG_TEST |
| 30 | +REFERENCING OLD AS O NEW AS N |
| 31 | +FOR EACH ROW |
| 32 | +MODE DB2SQL |
| 33 | +BEGIN ATOMIC |
| 34 | + IF (N.DATA = 'ABC') THEN |
| 35 | + INSERT INTO TSCHM.TRIG_TEST(DATA) VALUES ('-' || O.DATA); |
| 36 | + END IF; |
| 37 | +END |
| 38 | +``` |
| 39 | + |
| 40 | +When you try to convert this trigger in SSMA, if the **Generate ROWID column** project setting is **Off**, then SSMA will generate the following error message: |
| 41 | + |
| 42 | +> DB22SS0239: ROWID column not accessible |
| 43 | +
|
| 44 | +## Possible remedies |
| 45 | + |
| 46 | +Change the **Generate ROWID column** project setting to **On**, this will allow SSMA to produce the following T-SQL within the SQL Server trigger when converting the above example: |
| 47 | + |
| 48 | +1) Run a cursor over `inserted` row set, selecting `ROWID` and `DATA` columns into `@new$0` and `@new$DATA` variables: |
| 49 | + |
| 50 | + ```sql |
| 51 | + DECLARE |
| 52 | + ForEachInsertedRowTriggerCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR |
| 53 | + SELECT ROWID, DATA |
| 54 | + FROM inserted |
| 55 | + |
| 56 | + OPEN ForEachInsertedRowTriggerCursor |
| 57 | + |
| 58 | + FETCH ForEachInsertedRowTriggerCursor |
| 59 | + INTO @new$0, @new$DATA |
| 60 | + ``` |
| 61 | + |
| 62 | +2) Select matching row from `deleted` row set into `@old$0` and `@old$DATA` variables, based on the inserted `ROWID` (stored in `@new$0` variable): |
| 63 | + |
| 64 | + ```sql |
| 65 | + SELECT @old$0 = ROWID, @old$DATA = DATA |
| 66 | + FROM deleted |
| 67 | + WHERE ROWID = @new$0 |
| 68 | + ``` |
| 69 | + |
| 70 | +3) Perform trigger actions using `@old$DATA`/`@new$DATA` variables: |
| 71 | + |
| 72 | + ```sql |
| 73 | + IF (@new$DATA = 'ABC') |
| 74 | + INSERT SSMAADMIN.TRIG_TEST(DATA) |
| 75 | + VALUES (('-' + ISNULL(@old$DATA, ''))) |
| 76 | + ``` |
| 77 | + |
| 78 | +## Related conversion messages |
| 79 | + |
| 80 | +* [DB22SS0028: ROWID column generated](db22ss0028.md) |
| 81 | +* DB22SS0267: ROWID column |
| 82 | +* DB22SS0404: ROWID column can not be converted |
0 commit comments