在 Azure SQL 服务器中输入 integer[]
integer[] type in Azure SQL Server
在 Postgres 中SQL 我使用数组类型来存储 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 服务器的等效项?
这模糊了 SQL Server 和 nosql 之间的界限,但是您可以通过将数组编码为 json 数组并将其存储在 varchar(max) 列中来实现。
然后要从其他一些 table 存储用户 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
在 Postgres 中SQL 我使用数组类型来存储 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 服务器的等效项?
这模糊了 SQL Server 和 nosql 之间的界限,但是您可以通过将数组编码为 json 数组并将其存储在 varchar(max) 列中来实现。
然后要从其他一些 table 存储用户 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