I had posted other questions relating to this problem, but haven't had any responses to directly address the issue of multiple row data imports from XLS. I'm an infrequent user of SQL or DBs in general, so my background/experience is limited in regard to writing these queries. If there is an easier or more direct approach to reach my goal, I'm certainly open to them. I don't mean to overpost or anything, but this site seems to be the most helpful (thank you everyone who has replied to my other post).
From some posts I have looked at, I understand that I have a working set-based query/trigger (since multiple rows do get imported). Ultimately I only need to import data into the parent table, and the child table can be populated with static values and or values from the parent table, but the PK/FK relationship needs to be maintained. And this is what I seem to have the most trouble with when more than 1 row of data is imported from XLS.
I have set up a trigger to insert values into a child table when a insert is executed on the parent table. The query executes correctly however I am unable to have the FK match the PK when multiple rows of data are inserted. The FK always has the ID of the last row inserted in the parent table. I have tried several approaches from other forum posts 开发者_运维问答(here and on other sites) but always get errors.
Here is my updatePgVer Trigger code:
ALTER TRIGGER [updatePgVer]
ON [prototype].[dbo].[PageVersion]
FOR INSERT AS
BEGIN
SET NOCOUNT ON;
-- Insert into PageHistory
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
@@IDENTITY
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
END
And the query used to insert into the parent table:
INSERT INTO [prototype].[dbo].[PageVersion] ([Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu])
SELECT [Number], [PageId], [Properties], [StateId], [Language], [SearchText], [PageMetaDescription], [PageMetaKeyWords], [PageTypeId], [Name], [Title], [Owner], [Admin], [ShowInMenu]
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\test_import.xls', 'SELECT * FROM [Query$]');
The only other idea I have would to be to create some sort of loop that goes through each row and imports 1 at a time, so that the @@IDENTITY will always match. However, examples I have looked at seem hard to apply to my import.
The value for column VersionID, which appears to be the column with IDENTITY, is in the inserted table. You can reference it in your trigger like this
INSERT
INTO [prototype].[dbo].[PageHistory] ([VersionID], [Date], [Action], [Who], [StateId], [Owner])
SELECT
[VersionID],
, GETDATE()
, 'created'
, 'xls_user'
, [StateID]
, 'xls_user'
FROM inserted
If you want to see what data is available from inserted during the INSERT, temporarily put this in your trigger:
SELECT *
FROM inserted
精彩评论