下面我使用OUTPUT参数输出更新语句:
UPDATE set Value = src.Value, DeletedDate = iif (@Deleted =1, getutcdate(), NULL), ModifiedBy = SUSER_SNAME(), ModifiedDate = getutcdate()
WHEN NOT MATCHED THEN
INSERT (ConfigKey, Value, AddedBy, ModifiedBy)
VALUES (@ConfigKey, @Value, SUSER_SNAME(), SUSER_SNAME())
OUTPUT 'Core.Config', 'ConfigID', inserted.ConfigID,
iif($action = 'Update', Core.updXMLFragment('Value', inserted.Value, deleted.Value)+
Core.updXMLFragment('ModifiedBy', inserted.ModifiedBy, deleted.ModifiedBy) +
Core.updXMLFragment('DeletedDate', inserted.DeletedDate, deleted.DeletedDate)
, Core.insXMLFragment('ConfigID') +
Core.addnlXMLFragment ('ModifiedBy', inserted.ModifiedBy))
上面的代码在底部生成更新语句,但我在更新语句中遇到的问题是'DeletedDate = 0'
。我需要将其更改为DeletedDate=NULL
并且条件为'DeletedDate IS NULL'。怎么办呢?
BEGIN TRAN
update Core.Config
set Value = 'Insert',ModifiedBy = 'HARROGATE\Name' ,DeletedDate = '0'
where ConfigID = '1' and Value = 'Update' and ModifiedBy = 'HARROGATE\Name' and DeletedDate = '0'
if @@ROWCOUNT <> 1
BEGIN;
THROW 99999, 'Error - Single Row not updated, rollback terminated', 1;
END;
更新:包含updxmlfragment
BEGIN
if (@InsertedValue = '')
BEGIN
set @InsertedValue = ' '
END
if(@DeletedValue = '')
BEGIN
set @DeletedValue = ' '
END
if(@FieldName IS NULL)
--if(@InsertedValue IS NULL and @DeletedValue IS NULL or @FieldName IS NULL) -- pre v2.1 logic
BEGIN
-- This "errors" the function and ensures the script doesn't get any further than the update
return convert(int, 'NULL Params passed for XML Fragment Generation')
END
-- Validate the date has the correct "time" element associated - i.e HH:MM:SS:MSS
if (@FieldName like '%date%')
BEGIN
if((TRY_PARSE(@InsertedValue as datetime) IS NOT NULL) or (TRY_PARSE(@DeletedValue as datetime) IS NOT NULL))
BEGIN
if((len(replace(@DeletedValue, ':', '')) +3 <> len(@deletedvalue)) OR (len(replace(@InsertedValue, ':', '')) +3 <> len(@InsertedValue)))
BEGIN
-- This "errors" the function and ensures the script doesn't get any further than the update
return convert(datetime, 'Invalid Date Params passed for XML Fragment Generation')
END
END
END
return
'<Update><FieldName>'+@FieldName+'</FieldName>'+iif(@DeletedValue is null, '<OldValue />', '<OldValue><![CDATA['+@DeletedValue+']]></OldValue>')
+iif(@InsertedValue is null, '<NewValue />', '<NewValue><![CDATA['+@InsertedValue+']]></NewValue>') + '</Update>
'
答案 0 :(得分:2)
为什么不使用NULLIF()?
如果两个指定的表达式相等,则返回null值。
因此,您必须仅使用NULLIF(DeletedDate, 0)
代替DeletedDate
。
根据我的理解,你必须像这样使用它:
UPDATE
SET Value = src.Value
, DeletedDate = IIF(@Deleted = 1, getutcdate(), NULL)
, ModifiedBy = SUSER_SNAME()
, ModifiedDate = getutcdate() WHEN NOT MATCHED THEN
INSERT (ConfigKey, Value, AddedBy, ModifiedBy)
VALUES (@ConfigKey, @Value, SUSER_SNAME(), SUSER_SNAME())
OUTPUT 'Core.Config'
, 'ConfigID'
, inserted.ConfigID
, IIF($ACTION = 'Update', Core.updXMLFragment('Value', inserted.Value, deleted.Value) + Core.updXMLFragment('ModifiedBy', inserted.ModifiedBy, deleted.ModifiedBy) + Core.updXMLFragment('DeletedDate', NULLIF(inserted.DeletedDate, 0), NULLIF(deleted.DeletedDate, 0))
, Core.insXMLFragment('ConfigID') + Core.addnlXMLFragment('ModifiedBy', inserted.ModifiedBy))
UPDATE Core.Config
SET Value = 'Insert'
, ModifiedBy = 'HARROGATE\Name'
, DeletedDate = NULL
WHERE ConfigID = '1'
AND Value = 'Update'
AND ModifiedBy = 'HARROGATE\Name'
AND NULLIF(DeletedDate, 0) IS NULL;