integer[] type in Azure SQL Server



在PostgresSQL中,我使用数组类型来存储id、uuid等,如:

CREATE TABLE IF NOT EXISTS eventing.notifications (event_type integer NOT NULL, transport_type integer, user_id uuid, user_ids uuid[]);

CREATE TABLE IF NOT EXISTS public.sources (name character varying(255), timestamp without time zone, read_only_organization_ids integer[], organization_id uuid);

Azure中是否有使用Microsoft SQL server的等效程序?

这会模糊SQL Server和nosql之间的界限,但您可以通过将数组编码为json数组并将其存储在varchar(max(列中来实现这一点。

然后,要从其他存储用户id的表中创建json数组,可以使用for json子句。

要从varchar列中获得原始数组,可以使用openjson函数进行交叉应用:

declare @notifications table (user_ids varchar(max))
declare @user_ids varchar(max)
;with cte_jsonUser(jsonIds) as
(
select id
from (values(1), (2)) as tbluser(id)
for json auto
)
insert into @notifications(user_ids)
select replace(replace(jsonIds,'{"id":',''),'}','')
from cte_jsonUser
select user_ids from @notifications
-- user_ids
-- [1,2]
select i.user_ids
from @notifications as n
cross apply openjson(n.user_ids)
with (user_ids int '$') as i
-- user_ids
-- 1
-- 2

最新更新