This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-01
Channels
- # babashka (30)
- # beginners (80)
- # calva (21)
- # chlorine-clover (2)
- # cider (22)
- # clj-kondo (65)
- # cljfx (2)
- # cljs-dev (3)
- # cljsrn (3)
- # clojure (37)
- # clojure-europe (27)
- # clojure-italy (3)
- # clojure-nl (2)
- # clojure-taiwan (1)
- # clojure-uk (21)
- # clojurescript (13)
- # conjure (23)
- # core-typed (1)
- # cursive (16)
- # datahike (1)
- # datomic (6)
- # docs (2)
- # emacs (5)
- # figwheel-main (1)
- # fulcro (12)
- # helix (11)
- # jobs (2)
- # jobs-discuss (2)
- # leiningen (1)
- # lsp (34)
- # luminus (1)
- # malli (19)
- # microservices (1)
- # nrepl (1)
- # off-topic (25)
- # pathom (6)
- # polylith (47)
- # practicalli (5)
- # re-frame (8)
- # reagent (1)
- # reitit (1)
- # releases (2)
- # remote-jobs (2)
- # reveal (1)
- # sci (1)
- # shadow-cljs (11)
- # sql (8)
- # tools-deps (9)
- # xtdb (16)
What would the SQL look like for that update @orlandomr27?
(you can always do (jdbc/execute-one! ds ["arbitrary SQL for updating DB" ...])
which is why I’m asking)
Given a collection of :code
/`:status` pairs that could have arbitrary values, I don’t think you could construct a single SQL statement that updated all the matching rows.
You can if your database supports something like OPENJSON (MS SQL, postgres, sqlite with extensions):
-- Test table
declare @Person as table(
id int primary key,
email nvarchar(max)
);
insert into @Person(id, email)
values
(1, '[email protected]')
,(2, '[email protected]')
,(3, '[email protected]')
-- This would be passed in as a SQL parameter
declare @emailUpdates nvarchar(max) = N'[
{"id": 1, "email": "[email protected]"},
{"id": 2, "email": "[email protected]"}
]';
-- The single SQL statement:
update a
set email = b.email
from @Person a
join openjson(@emailUpdates, '$') with (
id int,
email nvarchar(max)
) b on a.id = b.id
So perhaps instead of “single statement” you mean “single transaction”?
Exactly, I meant a single transaction. I have not yet written a SQL query for that
Are you using next.jdbc
? If so: https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.2.659/doc/getting-started#datasources-connections--transactions
Will take a look. Thanks
You can if your database supports something like OPENJSON (MS SQL, postgres, sqlite with extensions):
-- Test table
declare @Person as table(
id int primary key,
email nvarchar(max)
);
insert into @Person(id, email)
values
(1, '[email protected]')
,(2, '[email protected]')
,(3, '[email protected]')
-- This would be passed in as a SQL parameter
declare @emailUpdates nvarchar(max) = N'[
{"id": 1, "email": "[email protected]"},
{"id": 2, "email": "[email protected]"}
]';
-- The single SQL statement:
update a
set email = b.email
from @Person a
join openjson(@emailUpdates, '$') with (
id int,
email nvarchar(max)
) b on a.id = b.id