简体   繁体   English

插入父表和子表

[英]Insert into parent table and child tables

My problem is with @officeident . 我的问题是@officeident In that for each new record inserted into LicenseHolder it has the same officeident .. this SQL inserts into OfficeID the value 1495 for every row. 对于每个插入LicenseHolder新记录,它具有相同的officeident ..此SQL在OfficeID中为每一行插入值1495。 Which was the last identity inserted. 最后插入的身份是哪个。 Which does not create the parent child relationship. 不会创建父子关系。

I think I should be researching a for next cursor. 我认为我应该为下一个游标研究a。 For every insert into Office a new row is then added to LicenseHolder so I can build the relationship. 对于Office的每个插入,然后将新行添加到LicenseHolder以便我可以建立关系。 Or if there is a simpler way or any help would be greatly appreciated. 或者,如果有更简单的方法或任何帮助,将不胜感激。

DECLARE @officeident INT

insert into [MembersDB].[dbo].[Office] 
([AddressLine1]
,[AddressLine2]
,[State]
,[PostCode])
select [OfficeMailingAddr],[OfficeMailingAddr],[state],'1' FROM [Members].[dbo].[Main]
SET @officeident = SCOPE_IDENTITY()

INSERT INTO [MembersDB].[dbo].[LicenseHolder] ([Name]
  ,[Email]
  ,[Mobile]
  ,[OfficeNumber]
  ,[LicenseHolderTypeID]
  ,[PartyTypeID]
  ,[OfficeID])

SELECT  
   [OfficeOf]
  ,[OfficeEmail]
  ,[Phone]
  ,'1234'
  ,'1'
  ,'1'
  ,@officeident     --I want like this to be different for each row. 
FROM [Members].[dbo].[Main]

Try something like this. 尝试这样的事情。 I wrote it off the top of my head, so you might need to debug still: 我把它写在脑海中,所以您可能仍需要调试:

Basically, i wrote a cursor here, which loops through the results of the table you want to use to insert into the new tables. 基本上,我在这里写了一个游标,它遍历您要用于插入新表的表的结果。 And then create the 2 new insert records at once... (and then do the same with the next result line from the MAIN table). 然后一次创建2条新的插入记录...(然后对MAIN表中的下一条结果行执行相同操作)。

DECLARE @OfficeMailingAddr as varchar(254), @OfficeMailingAddr as varchar(254), @state as varchar(254), @Name as varchar(254), @Email as varchar(254), @Mobile as varchar(254), @Phone as varchar(254)

declare NewCursor Cursor for
SELECT [OfficeMailingAddr],[OfficeMailingAddr],[state], [Name]
  ,[Email]
  ,[Mobile]
  ,[OfficeNumber] FROM [Members].[dbo].[Main]

open NewCursor
fetch next from NewCursor into @OfficeMailingAddr, @OfficeMailingAddr, @state, @Name, @Email, @Mobile, @Phone
WHILE @@FETCH_STATUS = 0
begin

    insert into [MembersDB].[dbo].[Office] 
([AddressLine1]
,[AddressLine2]
,[State]
,[PostCode]) VALUES (@OfficeMailingAddr, @OfficeMailingAddr, @state,'1')
SET @officeident = SCOPE_IDENTITY()

INSERT INTO [MembersDB].[dbo].[LicenseHolder] ([Name]
  ,[Email]
  ,[Mobile]
  ,[OfficeNumber]
  ,[LicenseHolderTypeID]
  ,[PartyTypeID]
  ,[OfficeID]) VALUES (@Name, @Email, @Mobile, @Phone,'1234'
  ,'1'
  ,'1'
  ,@officeident)

FETCH NEXT FROM NewCursor INTO @OfficeMailingAddr, @OfficeMailingAddr, @state, @Name, @Email, @Mobile, @Phone
END

Close NewCursor
deallocate NewCursor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM