Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A question about SQL insert statement!

Tags:

c#

sql

asp.net

I have two tables:

Threads
*******
ThreadID
UserID
TopicsID
Date
ThreadTitle
ThreadParagraph
ThreadClosed

Topics
******
TopicID
Theme
Topics
Date

I need to insert two statements and connect between them! This the first statment:

string insertCommand = 
    "INSERT INTO Users (UserID,TopicID,Date,ThreadTitle,ThreadParagraph) " +
     "VALUES ('CONVERT(uniqueidentifier, '" + giveMeGuidID() + 
     "),TopicID,dateTime,questionTitle,subTopic)";

and i need to have another statement for the Topics Table:

string insertCommand = 
    "INSERT INTO Topics (UserID,TopicID,Date,ThreadTitle,ThreadParagraph) " +
    "VALUES ('Theme, Topics, Date')";

The problem is that i have a connection between TopicsID (Threads table) and TopicsID (Topics table). Both are incremental ints, so how do i insert the same TopicID to both of them so they get the same value?

like image 660
WithFlyingColors Avatar asked May 30 '11 08:05

WithFlyingColors


People also ask

When SQL insert statement is used?

The INSERT INTO statement is used to insert new records in a table.

What type of statement is insert in SQL?

If we want to create a data, we're going to use the SQL keyword, “Insert”. The general format is the INSERT INTO SQL statement followed by a table name, then the list of columns, and then the values that you want to use the SQL insert statement to add data into those columns.

Which query is used to insert record?

Introduction on SQL Insert Query. In SQL, the 'Insert' command is used to add new records into the table in a database. An 'Insert' statement can be used to insert single row records or multiple rows of records, depending on the requirement.

What are the constraints with insert statement in SQL?

The values specified (or implied) by the INSERT statement must satisfy all the applicable constraints (such as primary keys, CHECK constraints, and NOT NULL constraints). If a syntax error occurs or if any constraints are violated, the new row is not added to the table and an error returned instead.


2 Answers

If you use MS SQL server, you can get the autoincrement value with @@Identity.

string insertCommand = 
    "INSERT INTO Users (UserID,TopicID,Date,ThreadTitle,ThreadParagraph) " +
    "VALUES ('CONVERT(uniqueidentifier, '" + giveMeGuidID() +
    "),TopicID,dateTime,questionTitle,subTopic); SELECT @@Identity";

Then, run this command as ExecuteScalar and get your value

like image 123
vityanya Avatar answered Oct 23 '22 08:10

vityanya


You can maintain the Transaction by using TransactionScope and using SCOPE_IDENTITY() to get the inserted Id from the First Query.

 // Create the TransactionScope
using (TransactionScope oTranScope = new TransactionScope())
{
   Int32 TopicID;
    // Open a connection 
    using (SqlConnection oCn1 = new SqlConnection(this.sCn1))
    {
        SqlCommand oCmd1 = new SqlCommand("INSERT INTO Users (UserID,TopicID,Date,ThreadTitle,ThreadParagraph) " +
"VALUES ('CONVERT(uniqueidentifier, '" + giveMeGuidID() +
"),TopicID,dateTime,questionTitle,subTopic); SELECT SCOPE_IDENTITY()";, oCn1);

        oCmd1.Parameters.Add ... Better to use parameter to save SQL Injection Attack

        oCn1.Open();
        // At this point, the connection is in the transaction scope, 
        // which is a lightweight transaction.
        TopicID = Convert.ToInt32 oCmd1.ExecuteScaler()); // as you want to get Id
        oCn1.Close();
    }
    // Open a connection 
    using (SqlConnection oCn2 = new SqlConnection(this.sCn2))
    {
        SqlCommand oCmd2 = new SqlCommand("SQLQuery", oCn2);
        //use return TopicID from last inserted query
        oCn2.Open();
        // The connection is enlisted in the transaction scope, 
        // which is now promoted to a distributed transaction
        // controlled by MSDTC
        oCmd2.ExecuteNonQuery();
        oCn2.Close();
    }
    // Tell the transaction scope to commit when ready 
    oTranScope.Consistent = true;
    // The following bracket completes and disposes the transaction
}
like image 29
Muhammad Akhtar Avatar answered Oct 23 '22 08:10

Muhammad Akhtar