Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server - stored procedure - ignore exception

I have inside a stored procedure:

INSERT INTO SitesCategories(SiteID, CategoryID) VALUES(@SITEID, @TempCId);

This insert could throw exceptions because I have this constraint on the table:

  ALTER TABLE dbo.SitesCategories
    ADD CONSTRAINT UniqueSiteCategPair
    UNIQUE (SiteId,CategoryID);

I made this constraint so that I would not have to check when inserting the uniques of the pairs (@SITEID, @TempCId). But I do not want that an SQl exception is thrown when this gets executed inside the stored procedure. How can I "catch" the exception inside the stored procedure and continue operations inside the procedure ?

like image 874
Ghita Avatar asked Jan 19 '23 09:01

Ghita


1 Answers

            BEGIN TRY
                INSERT INTO SitesCategories(SiteID, CategoryID) VALUES(@SITEID, @TempCId);
            END TRY
            BEGIN CATCH
            END CATCH;
like image 152
Anja Avatar answered Jan 24 '23 16:01

Anja