Ho apportato alcune modifiche al mio database e ho bisogno di migrare i vecchi dati alle nuove tabelle. Per questo, ho bisogno di riempire una tabella (ReportOptions) prendendo i dati dalla tabella originale (Practice), e riempire una seconda tabella intermedia (PracticeReportOption).E 'possibile per la clausola Output SQL restituire una colonna non inserita?
ReportOption (ReportOptionId int PK, field1, field2...)
Practice (PracticeId int PK, field1, field2...)
PracticeReportOption (PracticeReportOptionId int PK, PracticeId int FK, ReportOptionId int FK, field1, field2...)
ho fatto una query per ottenere tutti i dati che ho bisogno di passare dalla pratica alla ReportOptions, ma sto avendo difficoltà a riempire la tabella intermedia
--Auxiliary tables
DECLARE @ReportOption TABLE (PracticeId int /*This field is not on the actual ReportOption table*/, field1, field2...)
DECLARE @PracticeReportOption TABLE (PracticeId int, ReportOptionId int, field1, field2)
--First I get all the data I need to move
INSERT INTO @ReportOption
SELECT P.practiceId, field1, field2...
FROM Practice P
--I insert it into the new table, but somehow I need to have the repation PracticeId/ReportOptionId
INSERT INTO ReportOption (field1, field2...)
OUTPUT @ReportOption.PracticeId, --> this is the field I don't know how to get
inserted.ReportOptionId
INTO @PracticeReportOption (PracticeId, ReportOptionId)
SELECT field1, field2
FROM @ReportOption
--This would insert the relationship, If I knew how to get it!
INSERT INTO @PracticeReportOption (PracticeId, ReportOptionId)
SELECT PracticeId, ReportOptionId
FROM @ReportOption
Se potessi fare riferimento a un campo che non è nella tabella di destinazione nella clausola OUTPUT, sarebbe fantastico (penso di non poterlo fare, ma non lo so per certo). Qualche idea su come accoppiare il mio bisogno?
Grazie in anticipo.
È possibile restituire una qualsiasi delle colonne del tavolo in cui è stata inserita una riga, nella clausola 'OUTPUT'. Quindi, anche se non si fornisce un valore per una data colonna nell'istruzione 'INSERT', è ancora possibile specificare quella colonna nella clausola' OUTPUT'. Tuttavia, non è possibile restituire variabili o colonne SQL da altre tabelle. –
@marc_s grazie per la risposta, ma non ho il campo che mi serve nella tabella di destinazione (ho bisogno di PracticeId, che non è su ReportOption) –