Dette er forespørgslen, der slår to rækker sammen baseret på en fremmednøgle (bryder reglen for 1NF).
DECLARE @StrengthID INTDECLARE @GenericDrugDetailsID INTDEClARE @DosageStrength INTDECLARE @PresentationUnitID INTDECLARE @DosageFormID INTDECLARE @RouteOfAdministrationID INTDECLARE @nCombinations INTDECLARE @maxCombinations INTDECLARE @CurrCombinationTotal INTDECLARE @CurrGenericDrugDetailsID INTDECLARE @PName VARCHAR(100)DECLARE @DName VARCHAR(100) DECLARE @DNameAbbrev VARCHAR(50)DECLARE @RName VARCHAR(100)DECLARE @GetGenericDrugStrengths CURSORSET @nCombinations =0SET @CurrGenericDrugDetailsID =0--Få det maksimale antal kombinationer--=================================================================================SET @maxCombinations =(VÆLG TOP 1 COUNT(Generic DrugDetailsID) AS maxCombinationsFROM Pharmacy_StrengthsGROUP BY GenericDrugDetailsIDORDER BY maxCombinations desc)--=======================================================================================--Opret en midlertidig vikar til at holde styrkerne--========================================================================================DEKLARE @tmpSQL VARCHAR(max)SET @tmpSQL =''HVIS FINDER (VÆLG * FRA tempdb .sys.tables WHERE NAME LIKE '%tmpStrengths%')DROP TABLE #tmpStrengthsCREATE TABLE #tmpStrengths ( StrengthID INT NOT NULL, Generic DrugDetailsID INT NOT NULL, NumberOfCombinations INT NOT NULL, INT NULL 1, Dosage NOT NULL, INT NULL, INT NULL, INT NULL, INT NULL ) NOT NULL, DosageFormID1 INT NOT NULL, DosageFormName1 VARCHAR(100) NOT NULL, DosageFormNameAbbrev1 VARCHAR(50) NULL, RouteOfAdministrationID1 INT NOT NULL, RouteOfAdministrationName1 VARCHAR(100) NOT NULL); HVIS (@maxCombinations> 1) BEGIN DECLARE @counter int SET @counter =1 MENS @counter <@maxCombinations BEGIN SET @counter =@counter + 1 SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD StrengthID' + CAST( @counter AS VARCHAR(50)) + ' INT NULL;' SET @tmpSQL =@tmpSQL + 'ÆNDRINGSTABEL #tmpStrengths ADD DosageStrength' + CAST(@counter AS VARCHAR(50)) + 'INT NULL;' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD PresentationUnitID' + CAST(@counter AS VARCHAR(50)) + ' INT NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD PresentationUnitName' + CAST(@counter AS VARCHAR(100)) + ' VARCHAR(100) NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD DosageFormID' + CAST(@counter AS VARCHAR(50)) + ' INT NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD DosageFormName' + CAST(@counter AS VARCHAR(100)) + ' VARCHAR(100) NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD DosageFormNameAbbrev' + CAST(@counter AS VARCHAR(50)) + ' VARCHAR(50) NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD RouteOfAdministrationID' + CAST(@counter AS VARCHAR(50)) + ' INT NULL; ' SET @tmpSQL =@tmpSQL + 'ALTER TABLE #tmpStrengths ADD RouteOfAdministrationName' + CAST(@counter AS VARCHAR(100)) + ' VARCHAR(100) NULL; ' END ENDEXEC(@tmpSQL)--=======================================================================================SET @tmpSQL =''SET @GetGenericDrugStrengths =CURSOR FOR SELECT StrengthID, GenericDrugDetailsID, DosageStrength, PresentationUnitID, DosageFormID, RouteOfAdministrationID FROM Pharmacy_Strengths ORDER BY GenericDrugDetailsID ASCOPEN @GetGenericDrugStrengths FETCH NEXT FROM @GetGenericDrugStrengths INTO @StrengthID, @GenericDrugDetailsID, @DosageStrength, @PresentationUnitID, @DosageFormID, @RouteOfAdministrationID WHILE @ @FETCH_STATUS =0 BEGIN --Hent værdierne for Præsentationsenhed, Doseringsform og Administrationsvej. SELECT @PName =PresentationUnitName FROM Pharmacy_PresentationUnit WHERE PresentationUnitID =@PresentationUnitID SELECT @DName =DosageFormName, @DNameAbbrev =DosageFormNameAbbrev FROM Pharmacy_DosageForm WHERE DosageFormID =@DosageFormID SELECT @RName =RouteOfAdministrationName FROM Pharmacy_RouteOfAdministration WHERE RouteOfAdministrationID =@RouteOfAdministrationID IF (@GenericDrugDetailsID =@CurrGenericDrugDetailsID) BEGIN SET @nCombinations =(@nCombinations + 1) --Rediger den midlertidige tabel nu SET @tmpSQL =@tmpSQL + 'OPDATERING #tmpStrengths SET StrengthID' + CAST(@nCombinations AS VARCHAR(50)) + ' =' + CAST(@ StrengthID AS VARCHAR(50)) + ', DosageStrength' + CAST(@nCombinations AS VARCHAR(50)) + ' =' + CAST(@DosageStrength AS VARCHAR(50)) + ', PresentationUnitID' + CAST(@nCombinations AS VARC HAR(50)) + ' =' + CAST(@PresentationUnitID AS VARCHAR(50)) + ', PresentationUnitName' + CAST(@nCombinations AS VARCHAR(50)) + ' =''' + CAST(@PName AS VARCHAR( 100)) + ''', DoseringsformID' + CAST(@nCombinations AS VARCHAR(50)) + ' =' + CAST(@DosisFormID AS VARCHAR(50)) + ', Doseringsformnavn' + CAST(@nCombinations AS VARCHAR(50) )) + ' =''' + CAST(@DName AS VARCHAR(100)) + ''', DosageFormNameAbbrev' + CAST(@nCombinations AS VARCHAR(50)) + ' =''' + CAST(@DNameAbbrev AS VARCHAR (50)) + ''', RouteOfAdministrationID' + CAST(@nCombinations AS VARCHAR(50)) + ' =' + CAST(@RouteOfAdministrationID AS VARCHAR(50)) + ', RouteOfAdministrationName' + CAST(@nCombinations AS VARCHAR( 50)) + ' =''' + CAST(@RName AS VARCHAR(100)) + ''' WHERE GenericDrugDetailsID =' + CAST(@GenericDrugDetailsID AS VARCHAR(50)); EXEC(@tmpSQL); END ELSE BEGIN SET @nCombinations =1 SET @CurrGenericDrugDetailsID =@GenericDrugDetailsID; --Få det samlede antal kombinationer på forhånd SET @CurrCombinationTotal =(SELECT COUNT(GenericDrugDetailsID) FROM Pharmacy_Strengths WHERE GenericDrugDetailsID =@GenericDrugDetailsID GROUP BY GenericDrugDetailsID); --Indsæt i den midlertidige tabel nu INSERT INTO #tmpStrengths (StrengthID, GenericDrugDetailsID, NumberOfCombinations, DosageStrength1, PresentationUnitID1, PresentationUnitName1, DosageFormID1, DosageFormName1, DosageFormNameAbbrev1, @DosageFormNameAbbrev1, @RuteOfRugtalID, @1RouteOfRgSnavn, @DruteOfAdministrationIDDrenge PresentationUnitID, @PName, @DosageFormID, @DName, @DNameAbbrev, @RouteOfAdministrationID, @RName) END --PRINT CAST(@StrengthID AS varchar(50)) + ' ' + CAST(@GenericDrugDetailsID AS varchar(50)) + ' ' + CAST(@DosageStrength AS varchar(50)) + ' ' + CAST(@PresentationUnitID AS varchar(50)) + ' ' + CAST(@DosageFormID as varchar(50)) + ' ' + CAST(@RouteOfAdministrationID som varchar (50)) FETCH NEXT FROM @GetGenericDrugStrengths INTO @StrengthID, @GenericDrugDetailsID, @Dosage Strength, @PresentationUnitID, @DosageFormID, @RouteOfAdministrationID END --SELECT * fra #tmpStrengthsCLOSE @GetGenericDrugStrengthsDEALLOCATE @GetGenericDrugStrengthsSELECT a.GenericDrugID, a.GenericDrugName, f.*Inertdate, a.Update, a.AccountI,a.Update, a.AccountI a.VersionFROM Pharmacy_GenericDrug aINNER JOIN Pharmacy_ControlStatus d ON d.ControlStatusID =a.ControlStatusIDINNER JOIN Pharmacy_GenericDrugDetails e ON e.GenericDrugID =a.GenericDrugIDINNER JOIN f.Generic.DrugeGeneric.Generic.DrugIDGeneric.
Dette er, hvad jeg får før forespørgslen:
Generisk DrugID GenericDrugName DrugSubClassificationID DrugSubClassificationName DrugClassificationID DrugClassificationName GenericDrugDetailsID DosageStrength PresentationUnitID PresentationUnitName DosageFormID DosageFormName DosageFormNameAbbrev RouteOfAdministrationID RouteOfAdministration ControlStatusID ControlStatusName InsertDate InsertFKUserAccountId UpdateDate UpdateFKUserAccountId Version------------------------------------------------- -------------------------------------------------- ------------------------------------------------------------------ ---------------------------- ---------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------- ---------------------------- --------------- ----------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------- -------------------- -------------------- - ------------------ ---------------------------------- ----------- -------------------------------------------------- ------ -------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------ --------------------------------------- -------------------------------------------------- ------------------------------------------ ---------- ------ -------------------------------------------- -------------------------------------------------- ------ ----------------------- ---------------------------- ----------------------- ---------------------------- ------ ------------------83222B76-6690-42F2-BDAD-BE5BD2D19D3B Generisk Drug One 8ED135ED-5FDB-419A-9C7D-E788A40EAEAC Drug Subclassification Two 61D618F3-A38B09A-4466F-44416F Drug Cla ssification En 2 300 1 ml 2 Pille Pille 1 Mund 3 Narkotiske 2009-10-25 10:10:06.077 1 2009-11-15 07:06:03.733 1 2009-10-27 0620-705 0620-705 0620-705 1026-705:326-705 0620-705:326-705 0620-705 -BDAD-BE5BD2D19D3B Generisk Drug One 8ED135ED-5FDB-419A-9C7D-E788A40EAEAC Lægemiddelunderklassifikation To 61D618F3-A38A-4416-B0A4-FB29AD614B4B Lægemiddelklassificering En 2 400 1 ml 2 Pille 3 N Mundpille 2009-10-25 10:10:06.077 1 2009-11-15 07:06:03.733 1 2009-10-25 10:10:06.07783222B76-6690-49F5B2BDD1 Drug-42F2BD5D1D1 Generel DDF3BD5D1D1D3BD2D1D1D-49F5B2D1D1 9C7D-E788A40EAEAC Lægemiddelunderklassificering To 61D618F3-A38A-4416-B0A4-FB29AD614B4B Lægemiddelklassificering En 4 500 1 ml 2 Pille Pille 2 Indsprøjtning 3 Narkotikum 2009-10-25 10:10:06.077 1 2009-11-15 07:06:03.733 1 2009-10-25 10:10:06.077A1D226FD-7B Generel D226A-7B29A Generel 6B29A-7B24B7A 7B 7B 2009-10-2009 -34B8-43B4-A96B-78374F0081B0 Lægemiddelunderklassifikation En C10E9F1B-3985-4DB8-8EF8-A6C82B004B9F Lægemiddelklassifikation to 1 200 1 ml 2 Pille Pille 1 Mund 2 Ingen 2009-10-25 10:47:10.623 1 2009-11-15 04:02:47.890 1 2009-10-25 10:47:106
.
Dette er, hvad jeg får nu:
Generisk DrugID GenericDrugName DrugSubClassificationID DrugSubClassificationName DrugClassificationID DrugClassificationName StrengthID GenericDrugDetailsID NumberOfCombinations DosageStrength1 PresentationUnitID1 PresentationUnitName1 DosageFormID1 DosageFormNameAbbrevageO ionID1 RouteOfAdministrationName1 StrengthID2 DosageStrength2 PresentationUnitID2 PresentationUnitName2 DosageFormID2 DosageFormName2 DosageFormNameAbbrev2 RouteOfAdministrationID2 RouteOfAdministrationName2 InsertDate InsertFKUserAccountId UpdateDate UpdateFKU Version------------------------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------- ---------------------- -------------------------- ---------- ------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------- ---------- -------------------- ---------- ---------- --------------- ------------------- ------ -------------------------------------------------- -------------------------------------------- ------ ------- ------------------------------------------- -------------------------------------------------- ------- ------------------------------------------- ------------------------------------------------------- -------------------------------------------------- ------------------------------------------------------- ------- ------------------------------------------- -------------------------------------------------- -------------------------- -------------------- ----------- -------------------------------------------------- ------------------------------------ -------------------------------------------------- ---------------------------------- ------------------ -------------------------------------------------- -------------------------- ------------------ ----------------------------- ---------------------------- - -------------------- -----------------------83222B76-6690-42F2-BDAD -BE5BD2D19D3B Generisk lægemiddel One 8ED135ED-5FDB-419A-9C7D-E788A40EAEAC Lægemiddelunderklassifikation To 61D618F3-A38A-4416-B0A4-FB29AD614B4B Lægemiddelklassifikation 1 320 ml 2 Pille Pille 1 Mund 3 400 1 ml 2 Pille Pille 1 Mund 2009-10-25 10:10:06.077 1 2009-11-15 07:06:03.733 1 2009-10-27 062.07B 062.07B -42F2-BDAD-BE5BD2D19D3B Generisk D tæppe Et 8ED135ED-5FDB-419A-9C7D-E788A40EAEAC Lægemiddelunderklassifikation To 61D618F3-A38A-4416-B0A4-FB29AD614B4B Lægemiddelklassificering En 4 4 1 500 Pill 2 Pjektion 1 ml 2 NULL NULL NULL NULL NULL NULL NULL NULL NULL 2009-10-25 10:10:06.077 1 2009-11-15 07:06:03.733 1 2009-10-25 16:09-10-25 16:102B-702A-702B-702B-1009-11-15-2009 Generisk lægemiddel To 9925D762-34B8-43B4-A96B-78374F0081B0 Lægemiddelunderklassificering En C10E9F1B-3985-4DB 8-8EF8-A6C82B004B9F Lægemiddelklassificering To 1 1 1 200 1 ml 2 Pille Pille 1 Mund NULL NULL NULL NULL NULL NULL NULL NULL 2009-10-25 10:47:10.623 1 2009-11-15 04:02:47.890 1 2009-10-25 10:47:10.623
For de interesserede er dette tabelstrukturerne:
OPRET TABEL [dbo].[Pharmacy_GenericDrug]( [GenericDrugID] [uniqueidentifier] IKKE NULL BEGRÆNSNING [DF__Pharmacy___Gener__4E53A1AA] STANDARD (newid()), [GenericDrugName] [varchar]_La_CIL_Gener_100_SQLATE_10_Gener. DrugSubClassificationID] [uniqueidentifier] NOT NULL, [ControlStatusID] [int] NOT NULL, [InsertDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_GenericDrug_InsertDate] DEFAULT (getdate()), [InsertFKUser]NOT NULL ] IKKE NULL BEGRÆNSNING [DF_Pharmacy_GenericDrug_UpdateDate] DEFAULT (getdate()), [UpdateFKUserAccountId] [int] NOT NULL, [Version] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_GenericDrug_Version] DEFAULT) [getdate_DrugdeGene] DEFAULT (getdate_drug()Drug() ]( [GenericDrugDetailsID] [int] IDENTITY(1,1) NOT NULL, [GenericDrugID] [uniqueidentifier] NOT NULL, [InsertDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_Ge nericDrugDetails_InsertDate] DEFAULT (getdate()), [InsertFKUserAccountId] [int] NOT NULL, [UpdateDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_GenericDrugDetails_UpdateDate] DEFAULT (getdate()), [Opdatering] NOT NULL] datetime] NOT NULL CONSTRAINT [DF_Pharmacy_GenericDrugDetails_Version] DEFAULT (getdate()))CREATE TABEL [dbo].[Pharmacy_Strengths]( [StrengthID] [int] IDENTITY(1,1) IKKE NULL, [Generic DrugDetails] NOTID] DosageStrength] [int] NOT NULL, [PresentationUnitID] [int] NOT NULL, [DosageFormID] [int] NOT NULL, [RouteOfAdministrationID] [int] NOT NULL, [InsertDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_StrengthGetDate] DEFAInd. ()), [InsertFKUserAccountId] [int] NOT NULL, [UpdateDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_StrengthCombination_UpdateDate] DEFAULT (getdate()), [UpdateFKUserAccountId] [int] IKKE NULL, [Version] NOT ] [dato CONSTRAINT [D F_Pharmacy_StrengthCombination_Version] DEFAULT (getdate()))CREATE TABLE [dbo].[Pharmacy_PresentationUnit]( [PresentationUnitID] [int] IDENTITY(1,1) NOT NULL, [PresentationUnitName] [varchar](100) [Pharmacy_PresentationUnit]([PresentationUnitID] [int] IDENTITY(1,1) NOT NULL, [PresentationUnitName] [varchar](100) COLLATE_1CPerLaaltin SQLen_Datum ] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_PresentationUnit_InsertDate] DEFAULT (getdate()), [InsertFKUserAccountId] [int] NOT NULL, [UpdateDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_PresentationUnit(DEFAULTAccount]) [KUintDataupdate] ] NOT NULL, [Version] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_PresentationUnit_Version] DEFAULT (getdate()))CREATE TABEL [dbo].[Pharmacy_DosageForm]( [DosageFormID] [int] IDENTITY(1,1) NOT NULL, [DosageFormName, [DosageFormName] ] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [DosageFormNameAbbrev] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [InsertDate] [datetime] [DosageFormNameAbbrev] spiste] DEFAULT (getdate()), [InsertFKUserAccountId] [int] NOT NULL, [UpdateDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_DosageForm_UpdateDate] DEFAULT (getdate()), [UpdateFKUserAccountId] [int] NOT NULL, [Version] datetime] NOT NULL CONSTRAINT [DF_Pharmacy_DosageForm_Version] DEFAULT (getdate()))CREATE TABEL [dbo].[Pharmacy_RouteOfAdministration]( [RouteOfAdministrationID] [int] IDENTITY(1,1) IKKE NULL, [RouteOfchar)COLLationName] SQL_Latin1_General_CP1_CI_AS NOT NULL, [InsertDate] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_RouteOfAdministration_InsertDate] DEFAULT (getdate()), [InsertFKUserAccountId] [int] IKKE NULL, [Updatetime]INTFaPdate_Dato_Dato_DATUM , [UpdateFKUserAccountId] [int] NOT NULL, [Version] [datetime] NOT NULL CONSTRAINT [DF_Pharmacy_RouteOfAdministration_Version] DEFAULT (getdate()))