在存储过程中插入的数据序列不正确,无法使用临时表

时间:2015-07-29 08:44:12

标签: sql-server sql-server-2008 stored-procedures temp-tables

我在下面的存储过程中使用临时表,这会产生很多问题。

以下是该场景:此存储过程在一天内运行一次,然后选择并处理当天的条目。

[dbo].[TriggerEmailContent]是一个永久表,其中数据通过其他进程输入。但是每天都会从这张表中删除数据。

因此,此表中的id列始终以1开头。

[dbo].[Contacts][dbo].[TriggerEmail]也是永久性表格。

我正在使用@contact@trigger表,它们是两个表变量。

在此存储过程中操作的数据最终输入到[dbo].[MergeTable]表中。

电子邮件服务器通过TriggerEmailId从此表格顺序中选择此数据并向客户发送电子邮件。

我面临的问题是:在将数据输入[dbo].[MergeTable]时,临时表@trigger会以某种方式获取错误的数据序列,因为在[dbo].[MergeTable]中为该实例输入的数据被存储为错误。

问题仅出在临时表中,所有永久表都有正确的数据。

我想要什么:如何重组sp所以我可以乘坐@trigger桌子? 如何确保[dbo].[MergeTable]中输入的数据是来自[dbo].[TriggerEmailContent]表格的正确数据。

数据流:
让我们举一个例子数据。

@trigger临时表
1 100
2 200
3 300

TriggerEmail表
1 AAA
2 BBB
3 CCC

合并表将是@trigger和&之间的联接。 TriggerEmail。
所以它看起来像这样,
1 100 AAA
2 200 BBB
3 300 CCC

但我的问题是,有时候只有一些数据以这种方式存储 1 100 BBB
2 200 AAA
3 300 CCC

存储过程:

ALTER PROCEDURE [dbo].[DatabaseA]
      @currentDate as datetime
AS
BEGIN
      DECLARE @contact TABLE (Id int IDENTITY(1,1),  ContactId int)
      DECLARE @trigger TABLE (Id int IDENTITY(1,1),  TriggerEmailId int)

      -- Create the Contact Lines for ServerA
      INSERT INTO [dbo].[Contacts] ([EMailLogin], [FirstName], [LastName],[SendText], [SendHTML], [bounceCount], [verified],
             [Title], [CompanyName], [Address1], [Address2], [Address3], [Postcode], [Phone], [Country], [Region], [Department],
             [LanguageCode], [Aemail], [DaysToExpiry], [SubjectLine], [xxxxPhone], [xxxxAddress], [xxxxCompany])
      OUTPUT inserted.ID INTO @contact
         SELECT 
             [EMailLogin], [FirstName], [LastName], 1, 1, 0, 0,
             [Title], [CompanyName], [Address1], [Address2], [Address3], 
             [Postcode], [Phone], [Country], [Region], 
             [Department], [LanguageCode], [Aemail], [DaysToExpiry],
             [SubjectLine], [xxxxPhone], [xxxxAddress], [xxxxCompany]
         FROM 
             [dbo].[TriggerEmailContent]
         ORDER BY 
             Id

    -- Create TriggerEmail lines
    INSERT INTO [dbo].[TriggerEmail] ([MergeId], [ContactId], [sendDate],[sendStatus], [isActive])
    OUTPUT inserted.ID INTO @trigger
       SELECT 
           te.[MergeId], t.[ContactId], @currentDate, 0, 1
       FROM 
           [dbo].[TriggerEmailContent] as te, @contact as t
       WHERE 
           te.Id = t.Id
       ORDER BY 
           t.Id

-- Add All Mergefield
      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 2, te.[MergeField02],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] te
      join @trigger t on te.Id = t.Id
      join TriggerEmail ste on ste.id = t.TriggerEmailId

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 2, te.[MergeField02],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 3, te.[MergeField03],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 4, te.[MergeField04],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 5, te.[MergeField05],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 6, te.[MergeField06],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 7, te.[MergeField07],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 8, te.[MergeField08],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 9, te.[MergeField09],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      

      INSERT INTO [dbo].[MergeTable] ([MergeId],[MergeFieldNumber],[MergeFieldContent],[TriggerEmailId])
     SELECT te.[MergeId], 10, te.[MergeField10],  t.[TriggerEmailId]
      FROM [dbo].[TriggerEmailContent] as te, @trigger as t
      WHERE te.Id = t.Id      
END

1 个答案:

答案 0 :(得分:1)

不确定这是否是实际问题,但至少这对我来说是个错误:

您正在将行插入“联系人”,并将新ID存储到@contact:

  INSERT INTO [dbo].[Contacts] ...
  OUTPUT inserted.ID INTO @contact
  SELECT [EMailLogin], [FirstName], ....
  FROM [dbo].[TriggerEmailContent]
  ORDER BY Id

之后,您使用来自Contacts的{​​{1}}表中的ID加入@contacts,如果缺少任何ID,则不一样:

TriggerEmailContent

如果ID总是相同且顺序相同,则根本不需要整个@contact表。

稍后你用@trigger的ID做同样的事情。

如果你需要某种排序表变量,你必须将新表中的原始id和id存储到那里并将其与表连接,而不是新的id。

<强>更新

我最终看到的是与联接 INSERT INTO [dbo].[TriggerEmail] ... OUTPUT inserted.ID INTO @trigger SELECT te.[MergeId], t.[ContactId], @currentDate, 0, 1 FROM [dbo].[TriggerEmailContent] as te, @contact as t WHERE te.Id = t.Id ORDER BY t.Id 中的实际表格类似的ID不匹配问题

如果我理解正确的话,我会做什么来解决这个问题,就是将TriggerEmailContent中的id插入Contacts,TriggerEmail和MergeTable。然后,它应该用于在实际从表中获取数据时加入数据。对于数据的排序(如果确实需要),您可以使用Contacts(或其他表中的一个)中的id字段,并使用来自该源的数据对数据进行排序。这样我就不会看到对表变量的任何需求,对我来说看起来就像是在那里尝试在插入中创建的所有表中获得相同的匹配ID号。

顺便说一句,看起来你在10个插页中进行了一次无法操作,所以据我所知,你可以用一个插件替换它们使用unpivot