我只想在field_tag值不存在時才插入表中。我想在插入表之前檢查。如果收據存在於相同的field_tag值之前。如果不存在,那麼只有我想插入。我使用SQL Server 2008只有當一個字段值不存在時才插入表值
INSERT INTO [INVENTORY].[TBL_RECEIPTS]
([TicketNbr]
,[RevisionNbr]
,[Location_ID]
,[ContractNumber]
,[Storage_ID]
,[VarietyID]
,[GrossWeight]
,[TareWeight]
,[Carrier]
,[Receipt_date]
,[Last_update]
,[Inv_weight]
,[SAPBatch]
,[MoistureFactor]
,[Load_Harvested]
,[FIELDID]
,[GROWER_LOAD_NBR]
,[HARVEST_NBR]
,[TRACE_WORMS]
,[COR_CERT]
,[GFF_DEST]
--- ,[GFF_CONTRACT]
,[CAB_ID]
,[sOURCE]
,[CREATED_BY]
,[TOTALBONUSES]
,[PIDNUMBER]
,[CARRIERVENDORID]
,[HAUL_ZONE]
,[FIELD_Tag]
,[Station]
,[COUNTY]
,[GradeDate]
,[Load_Received]
,[Load_Processed]
)
VALUES
(@CERT
,@RevisionNbr
,@Location_ID
,@ContractNUmber
,@Storage_ID
,@VarietyID
,@Gross_Weight
,@TareWeight
,@CarrierName
,@receipt_date
,Getdate()
,@Inv_weight
,@receiptbATCHnbr
,@DockageWeight
,@HARVEST_DT
,@FIELDID
,@GROWER_LOAD_NBR
,@HARVEST_NUMBER
,@TRACE_WORMS
,@COR_CERT
,@GFF_DEST
--- ,@CONTRACT
,@CAB_ID
,'I'
,'Scale Interface'
,0
,@ContractPIDNumber
,@CARRIERID
,@HAUL_ZONE
,@FIELD_TAG
,@STATION
,@COUNTY
,@GRADE_DT
,@Arrival_dt
,@current_Date
)
我不喜歡這樣的:
insert into yourtable
(field1, field2, etc)
select value1, value2, etc
where not exists
(SELECT * FROM [INVENTORY].[TBL_RECEIPTS] WHERE Field_Tag = @FIELD_TAG)
如何打印錯誤,我想要做的錯誤處理這一點。如果重複,那麼應該打印消息。
類似,我想知道在子查詢和條件中寫什麼。整個sp在這裏http://pastebin.com/0yFAXM59 –
在這個語句中SELECT的語法不正確set @Records =來自[INVENTORY]的SELECT count(*)[TBL_RECEIPTS] WHERE field_tag = @Field_Tag; –