Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
kloepper
Yak Posting Veteran
77 Posts |
Posted - 2005-11-16 : 16:05:24
|
| I don't understand how to insert a record or a recordset into a set of normalized tables that have left outer joins. How is that done?The data originates in a table with the following columns...CompanyName FormType CIK DateFiled SEC WebAddressA R 1324999 20050419 ...49.txtB 8 946738 20050427 ...91.txtC 10 1062273 20050427 ...16.txtC 10 1062273 20050427 ...17.txtD R 1225082 20050421 ...34.txtE 4 1318932 20050427 ...69.txtF 8 883787 20050427 ...70.txtG 5 910638 20050427 ...58.txtG 5 910638 20050427 ...59.txtH 4 58592 20050427 ...08.txtH 4 58592 20050427 ...09.txt-----------------------------------------------------------The normalized tables would look something like this...(correct?)Company_Name_and_CIKCompanyName CIKA 1324999B 946738C 1062273D 1225082E 1318932F 883787G 910638H 58592Form_TypeFormType FormType_ID4 15 28 310 4R 5Date_Filed DateFiled_IDDateFiled 120050419 220050421 320050427 4SEC_WEB_AddressSECWebAddress SECWebAddress_ID...08.txt 1...09.txt 2...16.txt 3 ...17.txt 4...34.txt 5...49.txt 6...58.txt 7...59.txt 8...69.txt 9...70.txt 10...91.txt 11So how does one insert new data to this type of table structure?Thanks,Paul==============================================================GOCREATE TABLE [dbo].[_Company_Info_01]([ID] [int] IDENTITY(1,1) NOT NULL,[CompanyNameCIK_ID] [int] NULL,[FormType_ID] [int] NULL,[DateFiled_ID] [int] NULL,[SECWebAddress_ID] [int] NULL,[FormXML_ID] [int] NULL,[LastUpdated] [timestamp] NULL ([ID] ASC) ON [PRIMARY]) ON [PRIMARY]-------------------------------------------------GOCREATE TABLE [dbo].[Company_Name_and_CIK]([CompanyNameCIK_ID] [int] IDENTITY(1,1) NOT NULL,[CompanyName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOTNULL,[CIK] [int] NOT NULL,CONSTRAINT [PK_Company_Name_and_CIK] PRIMARY KEY CLUSTERED ([CompanyNameCIK_ID] ASC) ON [PRIMARY]) ON [PRIMARY]GOCREATE TABLE [dbo].[Date_Filed]([DateFiled_ID] [int] IDENTITY(1,1) NOT NULL,[DateFiled] [int] NULL,CONSTRAINT [PK_Date_Filed] PRIMARY KEY CLUSTERED ([DateFiled_ID] ASC) ON [PRIMARY]) ON [PRIMARY]GOCREATE TABLE [dbo].[Form_Type]([FormType_ID] [int] IDENTITY(1,1) NOT NULL,[FormType] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,CONSTRAINT [PK_Form_Type] PRIMARY KEY CLUSTERED ([FormType_ID] ASC) ON [PRIMARY]) ON [PRIMARY]GOCREATE TABLE [dbo].[Form_XML]([FormXML_ID] [int] IDENTITY(1,1) NOT NULL,[FormXML] [xml] NULL,CONSTRAINT [aaaaaForm_XML_PK] PRIMARY KEY CLUSTERED ([FormXML_ID] ASC) ON [PRIMARY]) ON [PRIMARY]GOCREATE TABLE [dbo].[SEC_Web_Address]([SECWebAddress_ID] [int] IDENTITY(1,1) NOT NULL,[SECWebAddress] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOTNULL,CONSTRAINT [PK_SEC_Web_Address] PRIMARY KEY CLUSTERED ([SECWebAddress_ID] ASC) ON [PRIMARY]) ON [PRIMARY]=======================================================My select statement looks like this...Use SECDeclare @FormType nvarchar(50), @DateFiled int;SET @FormType = N'4';SET @DateFiled = 20050421;SET NOCOUNT OFF;SELECT Company_Name_and_CIK.CompanyName, Company_Name_and_CIK.CIK, Form_Type.FormType, Date_Filed.DateFiled, SEC_Web_Address.SECWebAddress, Form_XML.FormXMLFROM _Company_Info_01 LEFT OUTER JOINCompany_Name_and_CIK ON _Company_Info_01.CompanyNameCIK_ID =Company_Name_and_CIK.CompanyNameCIK_ID LEFT OUTER JOINDate_Filed ON _Company_Info_01.DateFiled_ID = Date_Filed.DateFiled_ID LEFT OUTER JOINForm_Type ON _Company_Info_01.FormType_ID = Form_Type.FormType_ID LEFT OUTER JOINSEC_Web_Address ON _Company_Info_01.SECWebAddress_ID =SEC_Web_Address.SECWebAddress_ID LEFT OUTER JOINForm_XML ON _Company_Info_01.FormXML_ID = Form_XML.FormXML_IDWHERE (Form_Type.FormType = @FormType) AND (Date_Filed.DateFiled =@DateFiled) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-11-16 : 20:10:43
|
| INSERT statements affect one table. For each table that you want data to be inserted into, you'll need another statement. Tara Kizeraka tduggan |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-11-16 : 21:12:26
|
| if you have foreign key on the tables, you need to insert into the tabels in the correct sequence.Example : Company_Name_and_CIK, Date_Filed first before _Company_Info_01and you will need to use @@identity to obtain the ID inserted[KH] |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-11-16 : 22:31:24
|
| Why do you have a "DateFiled" table? A date is an attribute, something that should be stored with a transaction, not an entity that you need to track and generate artifical "ID"'s for. What purpose would breaking dates out into another table serve?I think you might need some help with your design to properly normalize it. |
 |
|
|
|
|
|
|
|