Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A MERGE statement must be terminated with a semicolon #318

Closed
skostrewa opened this issue May 14, 2024 · 9 comments
Closed

A MERGE statement must be terminated with a semicolon #318

skostrewa opened this issue May 14, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@skostrewa
Copy link

skostrewa commented May 14, 2024

The Merge-Statmentent written in my .sql-file can not be executed due to a missing semicolon, even though the semicolon is specified just like the examples all over the internet require. The query also runs fine in ssms if you alter the part where the values are grabbed from SQLPage.

index.sql:

select
    'form'                       as component,
    'Edit user'                 as title,
    'insert_user.sql' || COALESCE('?id=' || $id, '') as action;

select
    'firstname'                   as name,
    (select firstname from "Ressource" where Ressource_ID = cast($id as int)) as value;

select
    'lastname'                  as name,
    (select lastname from "Ressource" where Ressource_ID = cast($id as int)) as value;

insert_user.sql:

MERGE Ressource as target
USING  (select CAST($id AS INT) as id, :firstname as new_firstname, :lastname as new_lastname) as source
ON (target.Ressource_ID = source.id)
WHEN MATCHED
    THEN UPDATE SET
        target.Vorname = source.new_firstname,
        target.Nachname = source.new_lastname
WHEN NOT MATCHED
    THEN INSERT (RessourceTyp_ID, Vorname, Nachname)
    VALUES (1, source.new_firstname, source.new_lastname)
;

There is an error-message being displayed when you execute the presented code

error returned from database: Eine MERGE-Anweisung muss durch ein Semikolon (;) abgeschlossen werden. (translates to "A MERGE statement must be terminated with a semicolon (;)")

i expect the database-table to be updated or inserted according to the data submitted by the form

  • OS: Windows 11
  • Database: MSSQL
  • SQLPage Version: 0.20.4
@skostrewa skostrewa added the bug Something isn't working label May 14, 2024
@lovasoa
Copy link
Collaborator

lovasoa commented May 14, 2024

Hello and welcome to SQLPage!
Thank you for reporting this, I'll look into it. In the meantime you should be able to work around this by putting the merge into a stored procedure and just calling it from SQLPage.

@skostrewa
Copy link
Author

Hi,
Thank you for you fast reply! Unfortunately i am failing to call any sp from SQLPage and i am not able to find any information about that topic. Can you elaborate on how to call a sp from SQLPage?

Furthermore i noticed that the link to your blog on the sqlpage-website is not working as intended:
E.g.: im currently browsing https://sql.ophir.dev/examples/tabs.sql and select blog from the menu. The url will be https://sql.ophir.dev/examples/blog.sql instead of https://sql.ophir.dev/blog.sql and you will get a 404 not found.
I thought i just mention it here instead of opening a new issue.

@skostrewa
Copy link
Author

I'm now temporarily got it running using a upsert statement. I am still looking forward to your answer 😄.

@lovasoa
Copy link
Collaborator

lovasoa commented May 15, 2024

E.g.: im currently browsing https://sql.ophir.dev/examples/tabs.sql and select blog from the menu. The url will be https://sql.ophir.dev/examples/blog.sql instead of https://sql.ophir.dev/blog.sql and you will get a 404 not found.

It sounds like you have an old version of the page in cache ? Can you hard-refresh the page (with ctrl-shift-R) ?

@skostrewa
Copy link
Author

I am glad i did not open an issue for that. Hard refreshing the page fixed this issue. Thanks!

@lovasoa
Copy link
Collaborator

lovasoa commented May 15, 2024

I'll change SQLPage's behavior to include trailing semicolons in what we send to the database in the next release. Currently, the easiest is probably to do an update followed by an insert with a condition.

SELECT 'text' as component, @@VERSION as contents;

--drop table Ressource;
--create table Ressource (Ressource_ID INTEGER PRIMARY KEY, firstname varchar(50), lastname varchar(50));

-- In the next sqlpage, you will be able to run:
-- merge into Ressource as target
-- using (select $firstname as firstname, $lastname as lastname) as source
-- on target.Ressource_ID = $id
-- when matched then
--     update set firstname = source.firstname, lastname = source.lastname
-- when not matched then
--     insert (Ressource_ID, firstname, lastname) values ($id, source.firstname, source.lastname);

-- Currently, we do it manually. Update and then insert if necessary.
update Ressource set firstname = $firstname, lastname = $lastname where Ressource_ID = $id;
insert into Ressource (Ressource_ID, firstname, lastname) select $id, $firstname, $lastname where @@ROWCOUNT = 0;

select 'list' as component;
select CONCAT(firstname, ' ', lastname) as title, Ressource_ID as description
from Ressource;

image

@skostrewa
Copy link
Author

Thank you very much!

@lovasoa
Copy link
Collaborator

lovasoa commented May 15, 2024

Let's keep this opened until the semicolon fix is pushed

@lovasoa lovasoa reopened this May 15, 2024
@lovasoa
Copy link
Collaborator

lovasoa commented May 19, 2024

0.21 is now live, with the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants