批量导入数据触发器处理

1.不需要关联

insert into table(fd,fd1) select (ft,ft2) form inserted

2、而我在实际应用中,还存在与storeFile关联的storeFileItem数据表,
要用插入storeFile时自动生成的主键FileID,插入storeFileItem中作为外键关联

CREATE TRIGGER TriInsertStoreFile ON tblStore
FOR INSERT
AS

DECLARE @storeID int
DECLARE @FILEID int

SET @storeID=0
SET @FILEID=0

DECLARE stores_cursor CURSOR FOR
SELECT storeID FROM Inserted

OPEN stores_cursor

FETCH NEXT FROM stores_cursor
INTO @storeID

WHILE @@FETCH_STATUS = 0
BEGIN
  Insert into storeFile(storeID) (SELECT storeID FROM INSERTED)
  SET @FILEID=@@identity
  Insert into storeFileItem(fileID) values(@FILEID)

  FETCH NEXT FROM stores_cursor   INTO @storeID
END

CLOSE stores_cursor
DEALLOCATE stores_cursor

你可能感兴趣的:(批量导入数据触发器处理)