【MS-SQL】判斷空值”或’NULL’ 產生預設值GUID (uniqueidentifier)

情境: 判斷空值”或’NULL’ 產生預設值GUID (uniqueidentifier)

SQL語法:

--判斷空值''或'NULL' 產生預設值GUID (uniqueidentifier)
create table #t (
GUID uniqueidentifier,
GUID_Random uniqueidentifier
);
insert into #t (GUID, GUID_Random) values (
CASE WHEN '' = '' THEN '00000000-0000-0000-0000-000000000000' WHEN '' = 'NULL' THEN '00000000-0000-0000-0000-000000000000' ELSE '' END,
CASE WHEN '' = '' THEN NEWID() WHEN '' = 'NULL' THEN NEWID() ELSE '' END
);
insert into #t (GUID, GUID_Random) values (
CASE WHEN 'NULL' = '' THEN '00000000-0000-0000-0000-000000000000' WHEN 'NULL' = 'NULL' THEN '00000000-0000-0000-0000-000000000000' ELSE 'NULL' END,
CASE WHEN 'NULL' = '' THEN NEWID() WHEN 'NULL' = 'NULL' THEN NEWID() ELSE 'NULL' END
);

select GUID as '使用預設值', GUID_Random as 'Server 端自動產生' from #t
drop table #t

輸出結果:

[MSSQL] 判斷空值''或'NULL' 產生預設值GUID (uniqueidentifier)

留言

發佈留言

Top