-1

我试图让这个查询在选择的所有结果上运行。
但问题是 select fromLOG_ITEM201303只返回一个结果。
所以这意味着INSERT INTO每次我运行这个脚本时,该子句只影响一行。
但我想要做的是获得所有结果(总共 600 多个)并逐行插入所有结果。

DECLARE @charkey int, @charname varchar(16), @item int

SELECT @charkey = CHAR_KEY, @charname = NAME, @item = ITEMNUM
FROM LOG_ITEM201303
where 
(   
    ITEMNUM = 14317
OR  ITEMNUM = 14318
OR  ITEMNUM = 15478
OR  ITEMNUM = 15479
OR  ITEMNUM = 14301
OR  ITEMNUM = 14302
OR  ITEMNUM = 15476
OR  ITEMNUM = 15477
OR  ITEMNUM = 15018
OR  ITEMNUM = 15019
OR  ITEMNUM = 15020
OR  ITEMNUM = 15021
OR  ITEMNUM = 15022
OR  ITEMNUM = 15023
OR  ITEMNUM = 15024
OR  ITEMNUM = 15025
OR  ITEMNUM = 14437
OR  ITEMNUM = 14438
OR  ITEMNUM = 15656
OR  ITEMNUM = 15657
OR  ITEMNUM = 15658
OR  ITEMNUM = 15659
OR  ITEMNUM = 15660
OR  ITEMNUM = 15661
OR  ITEMNUM = 15662
OR  ITEMNUM = 15663
) AND (KIND = 133) AND (Convert(varchar, OCCUR_TIME,111) < '2013/03/22')

SELECT @charkey, @charname, @item

INSERT INTO game.dbo.Mail_Item_Table
(ItemNumber, ItemInfo, ReceiveDate)
VALUES
(@item, (SELECT CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), @item)))), NULL)

INSERT INTO game.dbo.Mail_Message_Table
(Message)
VALUES
('Automated Message from the ADMIN.')

INSERT INTO game.dbo.Mail_List_Table
(ReceiverCharKey, MailListIndex, MailItemIndex, MailMessageIndex, Sender, Receiver, SendDate)
VALUES 
(@charkey,
(SELECT TOP 1   MailListIndex+1 as last_entry
 FROM           game.dbo.Mail_List_Table
 WHERE          sender = 'SEND0R'
 ORDER BY MailListIndex DESC),
(SELECT TOP 1   MailItemIndex AS last_entry
 FROM           game.dbo.Mail_Item_Table
 ORDER BY       MailItemIndex DESC),
(SELECT TOP 1   MailMessageIndex AS last_entry
 FROM           game.dbo.Mail_Message_Table
 ORDER BY       MailMessageIndex DESC),
 'SEND0R', '@charname', getdate())
4

1 回答 1

2

主要问题是您选择单个变量 - 仅适用于单行 - 因为每个变量只能保存一个值。另外:对于一般的 SQL,您应该避免“逐行”(也称为RBAR - row-by-agonizing-row)方法 - 这只是一个糟糕的设计,并且会扼杀您可能从集合中获得的任何性能基于的方法。尽可能避免它!

相反,您需要做的是直接将数据插入到目标表中SELECT - 不使用介于两者之间的任何变量......或者如果您需要使用相同的数据进行多次插入(就像您在此处的情况一样) ,您需要将初始结果存储SELECT表变量中(而不是每行的单个变量)

所以尝试这样的事情:

-- define a table variable to hold the data
DECLARE @TemporaryData TABLE (charkey int, charname varchar(16), item int)

-- initial SELECT - insert data into table variable    
INSERT INTO @TemporaryData(charkey, charname, item)
   SELECT CHAR_KEY, NAME, ITEMNUM
   FROM LOG_ITEM201303
   WHERE ( ..... ) 

-- insert data from the table variable into the first table    
INSERT INTO game.dbo.Mail_Item_Table(ItemNumber, ItemInfo, ReceiveDate)
   SELECT 
       item, 
       CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), item)))), 
       NULL
   FROM @TemporaryData

-- unrelated to the table variable ...    
INSERT INTO game.dbo.Mail_Message_Table(Message)
VALUES('Automated Message from the ADMIN.')

-- insert data from table variable into yet another table...    
INSERT INTO game.dbo.Mail_List_Table(ReceiverCharKey, MailListIndex, MailItemIndex, MailMessageIndex, Sender, Receiver, SendDate)
  SELECT
      charkey,
     (SELECT TOP 1 MailListIndex+1 as last_entry
      FROM game.dbo.Mail_List_Table
      WHERE sender = 'SEND0R'
      ORDER BY MailListIndex DESC),
     (SELECT TOP 1 MailItemIndex AS last_entry
      FROM game.dbo.Mail_Item_Table
      ORDER BY MailItemIndex DESC),
     (SELECT TOP 1 MailMessageIndex AS last_entry
      FROM game.dbo.Mail_Message_Table
      ORDER BY MailMessageIndex DESC),
     'SEND0R', 
     charname, 
     getdate()
   FROM @TemporaryData    
于 2013-03-24T17:05:34.407 回答