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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:43:51+00:00 2026-05-30T23:43:51+00:00

Ok so I am new here :) I am relatively new to SQL, and

  • 0

Ok so I am new here 🙂 I am relatively new to SQL, and I am trying to insert data into multiple tables. I have both inserts to work however I want it so if one fails neither are committed.
The tables look like this:

Student –
StudentID – int PK,
StudentName – Varchar,
etc …

Class –
ClassID – int PK,
ClassName – varchar,
etc…

StudentClass –
StudentID,
ClassID,

What I am trying to do is create a new Student whom can belong to multiple classes. So I have created the Student class table to break up the many-many relationship. I have a stored procedure to insert a new student and return the newest StudentID and then I use this StudentID, in a new stored procedure, and a table value parameter to insert multiple rows into StudentClass table. These are the stored procedures:

Create A Student:

@FirstName varchar(20) = '', 
@LastName varchar(20) = '',
@PredictedGrade char(1) = '',
@ActionPlan bit = 0,
@StudentActive bit = 1,
@StudentID int out

INSERT INTO Student (FirstName, LastName, PredictedGrade, ActionPlan, StudentActive)
VALUES (@FirstName, @LastName, @PredictedGrade, @ActionPlan, @StudentActive)
SET @StudentID = SCOPE_IDENTITY()

Add Multiple Rows To StudentClass Table:

(@StudentClassCollection As InsertStudentClass READONLY)

INSERT INTO StudentClass(StudentID, ClassID)
SELECT StudentID, ClassID FROM @StudentClassCollection

So both of these work however I don’t know how to make it so if one fails the other will not execute and changes will not be committed? So effectively I need to perform both actions one after the other in the same stored procedure? I think! As I said I am new so if I have done anything wrong please let me know I will correct it 🙂

  • 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-30T23:43:52+00:00Added an answer on May 30, 2026 at 11:43 pm

    try like the below

    using (SqlConnection  connection= new SqlConnection(connectionstring))
       {
         connection.Open();
         SqlTransaction transaction = connection.BeginTransaction();
         try
         {
            SqlCommand command = new SqlCommand("proc1",connection);
            //execute the above command
            command.CommandText="proc2";
            //execute command again for proc2
            transaction.Commit();                   
         }
         catch
         {
           //Roll back the transaction.
           transaction.Rollback();
         }  
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to MySQL and PHP and I have been trying to
I'm relatively new to both scala and jodatime, but have been pretty impressed with
So I am a first time user here, and still relatively new to SQL.
I am relatively new to db design, so any help here would be appreciated.
am new here. i have a slight problem; PLease look at the following code
I'm new here to stackoverflow, so bear with me. I have a book that
I'm new here and I hope anyonte can help me. I have WCF Service
I'm new around here and i have a little problems with a C# application.
Quite new to maven here so let me explain first what I am trying
Someone here asked: Linq-To-Sql enables calling SPs. If this SP executes an update/delete/insert, do

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.