我想将一些旧列中的数据复制到同一个表中的一些新创建的列中。有些列为text
,有些列为varchar
(旧)到not null
(新)。我遇到的困难是旧列中有一些空数据,而新列是BEGIN TRY
BEGIN TRANSACTION
update tblProject
set
NewBitColumn1 = OldBitColumn1,
NewBitColumn2 = OldBitColumn2,
NewBitColumn3 = OldBitColumn3,
NewVarCharColumn1 = OldTextColumn1,
NewVarCharColumn2 = OldTextColumn2,
NewVarCharColumn3 = OldTextColumn3,
COMMIT TRAN -- Transaction Success
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE()
DECLARE @ErrorSeverity INT = ERROR_SEVERITY()
DECLARE @ErrorState INT = ERROR_STATE()
IF @@TRANCOUNT > 0
ROLLBACK TRAN --RollBack in case of Error
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH
。我怎样才能使旧数据为空,然后新数据得到一个非空值,0或者''依赖。
<a href="url_to_apk" target="_blank">
Download APK
</a>
答案 0 :(得分:2)
使用coalesce()
:
update tblProject
set
NewBitColumn1 = coalesce(OldBitColumn1, 0),
NewBitColumn2 = coalesce(OldBitColumn2, 0),
NewBitColumn3 = coalesce(OldBitColumn3, 0),
NewVarCharColumn1 = coalesce(OldTextColumn1, ''),
NewVarCharColumn2 = coalesce(OldTextColumn2, ''),
NewVarCharColumn3 = coalesce(OldTextColumn3, '');