2016-01-13 50 views
0

我有三個表[PublicationNotice][PublicationNoticeClient][PublicationNoticeInvoice]聯接表。插入數據分爲三個使用存儲過程

表1與表2和表3一個一對一的關係。

我使用的存儲過程將數據插入表中。我的表單由來自table1,table2和table3的所有屬性組成。

我的問題是,當我提交數據到它插入的所有數據接受表1 table2_id和table3_id這三個表。

提示:我使用ASP .Net和MSSQL

我的存儲過程是這樣的:

CREATE procedure [dbo].[AddUpdatePublicationNotice] 
    @ID bigint = NULL, 
    @Title varchar(max)= NULL, 
    @NewspaperName varchar(50) =NULL, 
    @Cities varchar(max)= NULL, 
    @PublicationSize varchar(8) =NULL, 
    @PublicationDate date =NULL, 
    @PublicationType varchar(50)= NULL, 
    @NewspaperPageNo smallint= NULL, 
    @Colored bit= NULL, 
    @CaseNature varchar(15)= NULL, 
    @ImagePath varchar(max)= NULL, 
    @ClientId bigint =NULL, 
    @InvoiceId bigint= NULL, 
    @CreatedById bigint = NULL, 
    @EditedById bigint= NULL, 
    @EditedDate datetime =NULL, 
    --******************************************-- 
    @ClientName varchar(max)= NULL, 
    @ClientType varchar(50)= NULL, 
    @ClientContactPerson varchar(max)= NULL, 
    @ClientAddress varchar(max)= NULL, 
    @ClientCity varchar(50) =NULL, 
    @ClientCountry varchar(50)= NULL, 
    --******************************************-- 
    @InvoiceDate date= NULL, 
    @InvoiceTotalAmount bigint= NULL, 
    @InvoicePaymentRecievedDate date= NULL, 
    @InvoiceChequeNo bigint= NULL, 
    @InvoiceBankName varchar(50)= NULL 
    --******************************************-- 
    AS 
    Insert into PublicationNotice (
     [Title] 
     ,[NewspaperName] 
     ,[Cities] 
     ,[PublicationSize] 
     ,[PublicationDate] 
     ,[PublicationType] 
     ,[NewspaperPageNo] 
     ,[Colored] 
     ,[CaseNature] 
     ,[ImagePath] 
     ,[ClientId] 
     ,[InvoiceId] 
     ,CreatedById 
    )Values(
     @Title 
     ,@NewspaperName 
     ,@Cities 
     ,@PublicationSize 
     ,@PublicationDate 
     ,@PublicationType 
     ,@NewspaperPageNo 
     ,@Colored 
     ,@CaseNature 
     ,@ImagePath 
     ,@ClientId 
     ,@InvoiceId 
     ,@CreatedById) 

     insert into [dbo].[PublicationNoticeClient] ( 
     [Name] 
     ,[Type] 
     ,[ContactPerson] 
     ,[Address] 
     ,[City] 
     ,[Country] 
     ,[CreatedById]) 
     Values(@ClientName 
     ,@ClientType 
     ,@ClientContactPerson 
     ,@ClientAddress 
     ,@ClientCity 
     ,@ClientCountry 
     ,@CreatedById) 

     Insert Into [dbo].[PublicationNoticeInvoice] (
     [Date] 
     ,[TotalAmount] 
     ,[PaymentRecievedDate] 
     ,[ChequeNo] 
     ,[BankName] 
     ,[CreatedById]) 
     Values (
     @InvoiceDate 
     ,@InvoiceTotalAmount 
     ,@InvoicePaymentRecievedDate 
     ,@InvoiceChequeNo 
     ,@InvoiceBankName 
     ,@CreatedById) 
     GO 

我知道我可以先插入表2和表3的值,然後從中選擇最後插入值表2表3和(被table2_id和table3_id),然後將它們插入到表1

是否有插入這樣的數據的任何其他快捷的方式???

+1

使用我已經刪除了MySQL的標籤,因爲它在這裏並不適用OUTPUT子句 –

+0

。 MySQL和MS SQL通常需要不同的解決方案。 –

回答

2

您可以使用@@IDENTITY , SCOPE_IDENTITY, IDENT_CURRENTOUTPUT方法來檢索最後一個ID。 Output是唯一安全的。

你需要插入到表變量,然後再查詢它

create table table1(
id int identity(1,1), 
id_table2 int, 
id_table3 int); 

create table table2 (
id int identity(100,1), 
val varchar(20)); 

create table table3 (
id int identity(200,1), 
val varchar(20)); 

declare @varTable2 table (LastID int); 
declare @varTable3 table (LastID int); 

insert into table2 
output inserted.id into @varTable2 values ('a'); 

insert into table3 
output inserted.id into @varTable3 values ('a'); 

insert into table1 (id_table2, id_table3) values 
((select LastID from @varTable2), 
    (select LastID from @varTable3) 
); 

    select * from table1 
+0

我有兩個與table1相關的表,所以我需要在table1中插入兩個不同的id。如何獲得這兩個id(table2_id和table3_id)到表1 – zulqarnainjalil

+0

@zulqarnainjalil我編輯了一個答案,以更詳細的方式回答你的問題。 – dcieslak

1
--You need to declare two variables to get identity values from table 2 and table 3 As 
DECLARE @table2_identity AS INT 
DECLARE @table3_identity AS INT 

--After insert in Table 2 set table2_identity variable as follows 
SET @table2_identity = SELECT SCOPE_IDENTITY() 

--After insert in Table 3 set table3_identity variable as follows 
SET @table3_identity = SELECT SCOPE_IDENTITY() 

--Then assign those variable values in insert query of Table 1