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 7052059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:20:18+00:00 2026-05-28T03:20:18+00:00

I have a question about constraints in SQL, to be exact in transact-sql. I

  • 0

I have a question about constraints in SQL, to be exact in transact-sql. I have a database for trips. I created a procedure for adding new trip participant. I am using ms-sql server so foreign key and primary key constraints were added when I created tables. Now in my procedure for adding new participant for a trip I have

insert VoyageThemes(VoyageId,ThemeId) values (@voyageId,@themeId)

now, in VoyageThemes table both VoyageId and ThemeId are primary keys and foreign keys, so when I try to add new values that doesen’t correspond to already existing values in database constraint raises it’s head.

My question is, can I somehow check if constraint ‘said’ that I can’t add values to table, so I can stop the procedure or I need to manually check in my database if VoyageId and ThemeId exists.

I need to know if those values exists because of this line of code:

update Voyages
set Voyages.Price=Voyages.Price+@costOfTheme*@numOfParticipants

I am updating the price of a trip, so this line of code can only excecute only if there is a corresponding VoyageId and ThemeId

  • 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-05-28T03:20:18+00:00Added an answer on May 28, 2026 at 3:20 am

    I guess you can use a try/catch?:

    ...
    BEGIN TRY  
        insert VoyageThemes(VoyageId,ThemeId) values (@voyageId,@themeId)     
        -- If we are here, then the insert succeeded, proceed with the update
        update Voyages
        set Voyages.Price=Voyages.Price+@costOfTheme*@numOfParticipants
        ...
    END TRY 
    BEGIN CATCH  
        -- insert failed, check error
        SELECT @error_number = ERROR_NUMBER(),
               @error_severity = ERROR_SEVERITY(),
               @error_state = ERROR_STATE()  
        IF @error_number = 547
        -- constraint violation   
            BEGIN     
                PRINT '...'   
            END  
        ELSE 
            -- propagate error 
            BEGIN
                RAISERROR(@error_number, @error_severity, @error_state) WITH LOG   
            END 
    END CATCH
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of questions about Keys, Indexes and Constraints in SQL, SQL
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I am new to LINQ and EF and I have a quick question about
I have a very simple question about transactions. (In sql server 2000, but I
I have a one more question about T-SQL I´d like to DROP a PRIMARY
I have a very basic question about SQL server and Visual Studio 2010. I'm
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex

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.