0

我正在寻找有关如何处理来自通过 System.Data.SqlClient.SqlBulkCopy 发送的 C# 客户端应用程序的传入数据的建议。最初的意图是维护所有通过应用程序发送到我们的 sql server 中的数据,这些数据将被“按摩”。为了保证安全,数据被发送到只写表,其中 AFTER INSERT 触发器将复制到另一个表并删除传入的表。

简单示例:

ALTER TRIGGER [dbo].[FromWeb_To_MyTable] ON [dbo].[_FromWeb_MyTable]
AFTER INSERT
AS 
BEGIN
   SET NOCOUNT ON;

   INSERT INTO [dbo].[MyTable]([MyTableID],[MemberID],[LocationID],[ODBCID],[InsertDateTime])
   SELECT i.[MyTableID],i.[MemberID],i.[LocationID],i.[ODBCID],i.[InsertDateTime]
   FROM Inserted i;

   DELETE FROM _FromWeb_MyTable
   WHERE [MyTableID] IN (SELECT i.[MyTableID] FROM Inserted i);
END

现在我将使用一些不同的方法,需要删除“转到”表中的所有内容。我最大的表将是大约 350,000 条记录。我打算像这样删除并重新创建所述表:(此方法运行良好,请参阅下面的问题)

ALTER TRIGGER [dbo].[FromWeb_To_MyTable] ON [dbo].[_FromWeb_MyTable]
AFTER INSERT
AS 
BEGIN
   SET NOCOUNT ON;

   IF EXISTS(SELECT [name] FROM sys.TABLES WHERE [name] = 'MyTable') DROP TABLE [dbo].[MyTable];
   CREATE TABLE [dbo].[MyTable] (
      [MyTableID] [int] NOT NULL,
      [MemberID] [varchar](6) NOT NULL,
      [LocationID] [varchar](50) NOT NULL,
      [ODBCID] [varchar](50) NOT NULL,
      [InsertDateTime] [datetime] NOT NULL
   ) on [PRIMARY];

   INSERT INTO [dbo].[MyTable] ( [MyTableID], [MemberID],[LocationID],[ODBCID],[InsertDateTime] )
   SELECT i.[MyTableID],i.[MemberID],i.[LocationID],i.[ODBCID],i.[InsertDateTime]
   FROM Inserted i;

   DELETE FROM _FromWeb_MyTable
   WHERE [MyTableID] IN (SELECT [MyTableID] FROM Inserted);
END

有人看到这种方法有什么问题吗?有什么不同的建议吗?有人可以解释何时以及如何索引新创建的表吗?

4

1 回答 1

1

由于您重用表而不是删除并重新创建它,因此只需使用TRUNCATE.

ALTER TRIGGER dbo.FromWeb_To_MyTable ON dbo._FromWeb_MyTable
AFTER INSERT
AS 
BEGIN
   SET NOCOUNT ON;

   TRUNCATE TABLE dbo.MyTable;

   INSERT INTO dbo.MyTable (MyTableID, MemberID,LocationID,ODBCID,InsertDateTime)
   SELECT i.MyTableID,i.MemberID,i.LocationID,i.ODBCID,i.InsertDateTime
   FROM Inserted i;

   DELETE FROM _FromWeb_MyTable
   WHERE MyTableID IN (SELECT MyTableID FROM Inserted);
END
于 2013-06-08T23:33:52.600 回答