Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Update one record & OUTPUT two records

$
0
0
For a staging table

Table TimeRange
id binary(16),
startTime datetime,
endTime datetime,
isValid tinyint

There are two validation rules: starttime cannot be null, endTime cannot be null (assume that we cannot set the columns as NOT NULL).

We would like to OUTPUT an error record for each validation error for every record in the TimeRange table.

Would there be a single statement that could do this ? (ie. would UPDATE invalid record AND would OUTPUT two validation error records for a record that has startTime = NULL AND endTime = NULL)

Something like:


UPDATE TimeRange
SET isValid = 0
OUTPUT inserted.id,
CASE WHEN inserted.startTime is NULL THEN inserted.startTime
WHEN inserted.endTime is NULL THEN inserted.endTime
END -- Needs to handle the case where both startTime and endTime are invalid
INTO @InvalidRecords
FROM (a SELECT stmt that is a table with a record for each validation error)


MERGE does not have the functionality needed (inserting multiple records for every invalid record).

Have not had success using a UNION ALL, as there is an error updating derived tables.

Have not had success creating a JOIN statement.

Thx!

Viewing all articles
Browse latest Browse all 13329

Trending Articles