关于SQL插入语句的问题!

时间:2011-05-30 08:26:30

标签: c# asp.net sql

我有两张桌子:

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

Topics
******
TopicID
Theme
Topics
Date

我需要插入两个语句并在它们之间进行连接! 这是第一个声明:

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

我需要为主题表提供另一个声明:

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

问题是我在TopicsID(Threads表)和TopicsID之间有连接 (主题表)。两者都是增量整数,所以我如何插入相同的TopicID 他们两个都得到了相同的价值?

4 个答案:

答案 0 :(得分:2)

如果您使用MS SQL服务器,则可以使用@@ Identity获取自动增量值。

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

然后,以ExecuteScalar运行此命令并获取您的值

答案 1 :(得分:1)

您可以使用Transaction维护TransactionScope并使用SCOPE_IDENTITY()从第一个查询中获取插入的ID。

 // 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
}

答案 2 :(得分:0)

如果您正在寻找可靠的东西,您需要使用交易

请参阅Managing Transactions in SQL Server Stored Procedures以获取相关信息。

另外,请查看Controlling Transactions (Database Engine)SQL Server Transaction Isolation Models

您还需要使用@@Identity最后插入的标识值。

答案 3 :(得分:0)

您的代码示例与提供的其他信息不相关。如果没有代码,你的帖子似乎足够一致,所以我倾向于认为那些片段只是错误的。

无论如何,你的想法似乎很清楚。在SQL Server 2005+中,您可以使用类似这样的INSERT语句来解决您的问题:

string insertCommand = 
    "INSERT INTO Topics (Theme, Topics, Date) " +
      "OUTPUT 'CONVERT(uniqueidentifier, '" + giveMeGuidID() +
      "'), INSERTED.TopicID, @dateTime, @questionTitle, @subTopic " +
      "INTO Threads (UserID, TopicID, Date, ThreadTitle, ThreadParagraph) " +
    "VALUES (@Theme, @Topics, @Date)";

虽然这是一个单一语句,但它会在不同的表中执行两次插入。 'main'插入正在进入Topics表。 {secondary}中的'secondary'由Threads子句定义。基本上,OUTPUT子句允许您引用要插入的数据,并将它们作为行集返回给客户端,或者(与OUTPUT...INTO结合使用时)将它们引导到现有表中,就像您可以看到的那样它在这里完成。