# MERGE Example - Named Queries

**URL:** <https://forum.inductiveautomation.com/t/merge-example-named-queries/114784>\
**Category:** Ignition\
**Created:** [March 30, 2026, 8:04pm UTC](https://forum.inductiveautomation.com/t/merge-example-named-queries/114784 "2026-03-30T20:04:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecujak](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@ecujak](https://forum.inductiveautomation.com/u/ecujak)\
**Post date:** [March 30, 2026, 8:04pm UTC](https://forum.inductiveautomation.com/t/merge-example-named-queries/114784/1 "2026-03-30T20:04:52Z")

</div>

SQL Server DB

Would someone please give an example of a Named Query using a MERGE statement.

Please show the syntax with multiple On comparisons and multiple columns being updated.

How to use the Ignition Named Query :Parameters

Thank you.

This syntax is not working

> MERGE INTO dbo.Users AS target
> 
> USING (VALUES (:parm1, :parm2)) AS source (UserId, UserName)
> 
> ON target.UserId = source.UserId
> 
> WHEN MATCHED THEN
> 
> ```python
> UPDATE SET UserName = source.UserName
> 
> ```
> 
> WHEN NOT MATCHED THEN
> 
> ```python
> INSERT (UserId, UserName)
> 
> VALUES (source.UserId, source.UserName);
> 
> ```

```python

```

---

<div class="post-metadata">

**Author:** ![Steve\_Laubach](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steve_laubach/32/60900_2.png) [@Steve\_Laubach](https://forum.inductiveautomation.com/u/Steve_Laubach)\
**Post date:** [March 30, 2026, 8:33pm UTC](https://forum.inductiveautomation.com/t/merge-example-named-queries/114784/2 "2026-03-30T20:33:05Z")

</div>

I haven’t used a merge before but I would typically handle this kind of functionality with a stored procedure.

Stored procedure would take the 2 parameters so your named query would resemble this  
EXEC dbo.spName :param1 :param2

Then the stored procedure would resemble this (rough sample)  
The text formatting tool got mad about some of the code but it’s a basic create procedure statement with parameters of:  
@UserID INT,  
@UserName VARCHAR(128)

```python
IF NOT EXISTS (SELECT TOP 1 COUNT(*) FROM tblUsers WHERE UserId = @UserId)
	INSERT INTO tblUsers (UserId, UserName)
	VALUES (@UserId, @UserName)
ELSE
	UPDATE tblUsers
		SET UserName = @UserName
WHERE UserID = @UserId

```

Edit:  
I forgot a where clause on my update. I wrote this in a query window without having any tables and stuff so I didn’t test it. I would recommend testing yours on a test system since it’s using an update statement.

---

<div class="post-metadata">

**Author:** ![Lynn\_Martineau](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Lynn\_Martineau](https://forum.inductiveautomation.com/u/Lynn_Martineau)\
**Post date:** [March 30, 2026, 8:55pm UTC](https://forum.inductiveautomation.com/t/merge-example-named-queries/114784/3 "2026-03-30T20:55:50Z")

</div>

I used MERGE to INSERT or UPDATE in named queries at a prior company and they worked great.  
I don’t have access to them to provide as a sample.

Validate the query with your database tool (looks like MSSMS for you, SQL Developer, PGAdmin, etc.) or even in the Designer Tools–\>Database Query Browser, before you dump it into the Named Query.

Offhand, it looks like the syntax with

> [@ecujak](#):
>
> USING (VALUES (:parm1, :parm2)) AS source (UserId, UserName)
> 
> ON target.UserId = source.UserId

is wrong.

---

<div class="post-metadata">

**Author:** ![ecujak](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@ecujak](https://forum.inductiveautomation.com/u/ecujak)\
**Post date:** [March 31, 2026, 3:45pm UTC](https://forum.inductiveautomation.com/t/merge-example-named-queries/114784/4 "2026-03-31T15:45:50Z")

</div>

You were correct, my syntax was invalid. The block below works with the Ignition Named Query with Parameters. Thank you Lynn.

```python
MERGE INTO dbo.table WITH (HOLDLOCK) AS target
USING (Select 
            :Val1 as Val1
           ,:Val2 as Val2
           ,:Val3 as Val3 
           ,:Val4 as Val4
           ,:Val5 as Val5 
           ) as source         
           
ON ( target.Val1= source.Val1
    AND target.Val2 = source.Val2
    AND target.Val3 = source.Val3)

WHEN MATCHED THEN
        UPDATE SET 
                    target.Val4= source.Val4,
                    target.Val5 = source.Val5,
                    
WHEN NOT MATCHED THEN                    
    INSERT
            (Val1
           ,Val2
           ,Val3
           ,Val4
           ,Val5
           )
     VALUES
           (:Val1
           ,:Val2
           ,:Val3
           ,:Val4
           ,:Val5
          );

```
