简体   繁体   English

在SQL Server 2008中的多个表中插入数据

[英]Insert data within multiple tables in SQL Server 2008

I have 3 different tables in my database as below; 我的数据库中有3个不同的表,如下所示;

Table 1:
ResourceID |  ResourceTitle|  ResourceCategory

Table 2:
DocumentID|  DocName  |  DocSize

Table 3:
ResourceID  |  DocumentID

Now I want to add some values into the above tables (using a C# .ASP.NET) respectively. 现在,我想分别在上述表中添加一些值(使用C#.ASP.NET)。

  • The first table I have one row to be added into the Resource table(Table 1). 第一个表我有一行要添加到Resource表(表1)中。
  • The second table I have multiple rows to be added into the document table(Table 2). 第二个表我有多行要添加到文档表(表2)中。
  • The third table needs to keep all the ids from the first two tables as a foreign key in ResourceDocument Table(Table 3). 第三个表需要将前两个表中的所有ID保留为ResourceDocument表(表3)中的外键。

All of the processes need to be done in one single transaction, so there would be a loop for the second table data in my asp.net c# class. 所有过程都需要在一个事务中完成,因此在asp.net c#类中的第二个表数据将存在一个循环。

the problem that I have is finding the right way to handle the transaction to do this job, also I need to make sure while these processes are running it doesn't let the other users to modify these data. 我遇到的问题是找到正确的方法来处理事务以完成此工作,我还需要确保在这些进程运行时不会让其他用户修改这些数据。

Your help would be appreciated. 您的帮助将不胜感激。

try this 尝试这个

DECLARE @ResourceID int
 DECLARE @DocumentID int
 Insert into Table 1(ResourceTitle,  ResourceCategory)value("values","values")          

 SELECT @ResourceID=SCOPE_IDENTITY()
 Insert into Table 2( DocName , DocSize) value("values","values")
 SELECT @DocumentID =SCOPE_IDENTITY()
 Insert into Table 3( ResourceID  ,  DocumentID) values(@ResourceID ,@DocumentID)

You can use transaction scope as you can see this link 您可以使用事务范围,因为您可以看到此链接

http://www.dotnetjalps.com/2010/05/using-transactions-with-linq-to-sql.html http://www.dotnetjalps.com/2010/05/using-transactions-with-linq-to-sql.html

You can use table valued parameters in stored procedures. 您可以在存储过程中使用表值参数。

This is a good LINK how to create & use TVP in stored procedures. 这是如何在存储过程中创建和使用TVP的很好的LINK

Then you need to pass TVP from your page/form. 然后,您需要通过页面/表单传递TVP

You can have a look HERE to get an idea for that. 您可以在这里了解一下。

Example for creating table type : 创建表类型的示例:

CREATE TYPE yourDocumentTableType AS TABLE 
(
    docID INT NOT NULL IDENTITY(1, 1),
    docName VARCHAR(50),
    docSize INT 
)
GO

Your stored procedure should be like this 您的存储过程应如下所示

CREATE PROCEDURE yourInsertOperation 
    -- Add the parameters for the stored procedure here
    @Param1 INT, 
    @Param2 VARCHAR(20),
    --......your parameter list goes here
    -- here you would pass the records for your document table
    @YourTableValuedParam dbo.yourDocumentTableType READONLY 
AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        -- declare temp table type & assign the TVP to it
        DECLARE @TempDocuments dbo.yourDocumentTableType

        INSERT INTO @TempDocuments (docName, docSize)
        SELECT docName, docSize FROM @YourTableValuedParam

        DECLARE @ResourceID BIGINT            

        --Marking the start of a transaction
        BEGIN TRANSACTION

        --Inserting records into resource table
        INSERT INTO resourceTable(column1, column2) VALUES (@param1, @param2)

        --selecting resourceID to be inserted into document table & resource-document table
        SELECT @ResourceID = SCOPE_IDENTITY()

        WHILE EXISTS(SELECT * FROM @TempDocuments)
            BEGIN                        
                DECLARE @DocumentID BIGINT

                --Inserting records into document table from the table valued parameter
                INSERT INTO documentTable(docName, docSize, resourceID)
                SELECT TOP 1 docName, docSize, @ResourceID FROM @TempDocuments

                SELECT @DocumentID = SCOPE_IDENTITY()
                INSERT INTO resouceDocumentTable(resourceID, docID) VALUES (@ResourceID, @DocumentID)

                DELETE TOP (1) FROM @TempDocuments
            END

    --Checking for any error in the whole transaction (all 3 table insert operation)
    IF @@ERROR > 0
        --rollback if there was any error
        ROLLBACK TRANSACTION
    ELSE
        --commit whole transaction (all 3 table insert operation)
        COMMIT TRANSACTION

END
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM