Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This isn't an answer.</strong> I just want to show that <code>IF NOT EXISTS(...) INSERT</code> method isn't safe. You have to execute first <code>Session #1</code> and then <code>Session #2</code>. After <code>v #2</code> you will see that without an <code>UNIQUE</code> index you could get duplicate pairs <code>(SoftwareName,SoftwareSystemType)</code>. Delay from session #1 is used to give you enough time to execute the second script (session #2). You could reduce this delay.</p> <p><strong>Session #1</strong> (SSMS > New Query > F5 (Execute))</p> <pre><code>CREATE DATABASE DemoEXISTS; GO USE DemoEXISTS; GO CREATE TABLE dbo.Software( SoftwareID INT PRIMARY KEY, SoftwareName NCHAR(400) NOT NULL, SoftwareSystemType NVARCHAR(50) NOT NULL ); GO INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (1,'Dynamics AX 2009','ERP'); INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (2,'Dynamics NAV 2009','SCM'); INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (3,'Dynamics CRM 2011','CRM'); INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (4,'Dynamics CRM 2013','CRM'); INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (5,'Dynamics CRM 2015','CRM'); GO /* CREATE UNIQUE INDEX IUN_Software_SoftwareName_SoftareSystemType ON dbo.Software(SoftwareName,SoftwareSystemType); GO */ -- Session #1 BEGIN TRANSACTION; UPDATE dbo.Software SET SoftwareName='Dynamics CRM', SoftwareSystemType='CRM' WHERE SoftwareID=5; WAITFOR DELAY '00:00:15' -- 15 seconds delay; you have less than 15 seconds to switch SSMS window to session #2 UPDATE dbo.Software SET SoftwareName='Dynamics AX', SoftwareSystemType='ERP' WHERE SoftwareID=1; COMMIT --ROLLBACK PRINT 'Session #1 results:'; SELECT * FROM dbo.Software; </code></pre> <p><strong>Session #2</strong> (SSMS > New Query > F5 (Execute))</p> <pre><code>USE DemoEXISTS; GO -- Session #2 DECLARE @SoftwareName NVARCHAR(100), @SoftwareSystemType NVARCHAR(50); SELECT @SoftwareName=N'Dynamics AX', @SoftwareSystemType=N'ERP'; PRINT 'Session #2 results:'; IF NOT EXISTS(SELECT * FROM dbo.Software s WHERE s.SoftwareName=@SoftwareName AND s.SoftwareSystemType=@SoftwareSystemType) BEGIN PRINT 'Session #2: INSERT'; INSERT INTO dbo.Software(SoftwareID,SoftwareName,SoftwareSystemType) VALUES (6,@SoftwareName,@SoftwareSystemType); END PRINT 'Session #2: FINISH'; SELECT * FROM dbo.Software; </code></pre> <p>Results:</p> <pre><code>Session #1 results: SoftwareID SoftwareName SoftwareSystemType ----------- ----------------- ------------------ 1 Dynamics AX ERP 2 Dynamics NAV 2009 SCM 3 Dynamics CRM 2011 CRM 4 Dynamics CRM 2013 CRM 5 Dynamics CRM CRM Session #2 results: Session #2: INSERT Session #2: FINISH SoftwareID SoftwareName SoftwareSystemType ----------- ----------------- ------------------ 1 Dynamics AX ERP &lt;-- duplicate (row updated by session #1) 2 Dynamics NAV 2009 SCM 3 Dynamics CRM 2011 CRM 4 Dynamics CRM 2013 CRM 5 Dynamics CRM CRM 6 Dynamics AX ERP &lt;-- duplicate (row inserted by session #2) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload