Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:05:18+00:00 2026-06-03T14:05:18+00:00

I have a try catch in my physical C# code that just skips this

  • 0

I have a try catch in my physical C# code that just skips this insert process when a error accrues and continues the loop to fill the database. However this is bad coding practice.

So I would like to add an IF statement to the stored procedure below that will just skip if a primary key is already there. My primary key is @id.

How can I go about this?

CREATE PROCEDURE InsertProc
    (
    @id int, 
    @to nvarchar(100), 
    @from nvarchar(100), 
    @subject nvarchar(100), 
    @date datetime
    )
AS
    INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
    VALUES (@id, @to, @from, @subject, @date)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:05:19+00:00Added an answer on June 3, 2026 at 2:05 pm
    CREATE PROCEDURE InsertProc
        (
        @id int, 
        @to nvarchar(100), 
        @from nvarchar(100), 
        @subject nvarchar(100), 
        @date datetime
        )
        AS
        IF NOT EXISTS (SELECT NULL FROM Emails_Log
                        WHERE Email_ID = @ID)
        BEGIN
            INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
                VALUES (@id, @to, @from, @subject, @date)
        END
    

    If you actually want to update record if already exists and insert if not, the pattern is as follows:

    CREATE PROCEDURE InsertProc
        (
           @id int, 
           @to nvarchar(100), 
           @from nvarchar(100), 
           @subject nvarchar(100), 
           @date datetime
        )
        AS
           UPDATE Emails_Log
              SET e_To = @to, 
                  e_From = @from, 
                  e_Subject = @subject, 
                  e_Date = @date
            WHERE Email_ID = @ID
           -- If there was no update it means that @ID does not exist,
           -- So we proceede with insert
           IF @@ROWCOUNT = 0
           BEGIN
              INSERT INTO Emails_Log (Email_ID, e_To, e_From, e_Subject, e_Date) 
                   VALUES (@id, @to, @from, @subject, @date)
           END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
I have a try-catch block that iterates over a set of records like this:
I have a try catch and finally block like this. Client client = new
I have a Try Catch in my code. I would like to know if
I have never used Try-catch in my code before, but now I need to
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
We have following code: try { // some code throwing MyException } catch (MyException
I have a try catch block to handle an error I am getting with
Using c# code within a web application, I have a try catch statement in
Today, in my C++ multi-platform code, I have a try-catch around every function. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.