我有三个表[PublicationNotice]
,[PublicationNoticeClient]
和[PublicationNoticeInvoice]
。
Table1与table2和table3具有一对一的关系。
我正在使用存储过程将数据插入表中。我的表单由table1,table2和table3的所有属性组成。
我的问题是,当我将数据提交到这三个表时,它会在table1中插入所有数据accept 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
我知道我可以先插入table2和table3值,然后从table2和table3中选择最后插入的值(即table2_id和table3_id),然后将它们插入table1
还有其他快速插入数据的方法???
答案 0 :(得分:2)
您可以使用integer
或@@IDENTITY , SCOPE_IDENTITY, IDENT_CURRENT
方法检索最后一个ID。 OUTPUT
是唯一安全的。{/ p>
您需要先插入表变量然后再查询
Output
答案 1 :(得分: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