Click here to Skip to main content
15,902,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to insert the record after checking if TariffName is already exists in the table tblCONTSPRateHourTariff or not.

What I have tried:

SQL
insert into tblCONTSPRateHourTariff
  select allotRateHour.Id
  ,(select id FROM [dbo].[tblACCTSPContractTariff] where TariffName='MPS_FLAT_MINIVAN_247' and ContractDetailId=1021)
  ,(select TariffType FROM [dbo].[tblACCTSPContractTariff] where TariffName='MPS_FLAT_MINIVAN_247' and ContractDetailId=1021)
  from tblCONTAllotInventory allot
  join tblCONTSPRateHour allotRateHour on allot.Id=allotRateHour.AllotInventoryId
  --join tblCONTSPRateHourTariff rateHourTariff on allotRateHour.id=rateHourTariff.RateHourId
  join [tblCONTAllotInventSegment] allotSeg on allot.Id=allotSeg.AllotInventoryId
  join tblSYSASegment seg on allotSeg.SegmentId=seg.Id
  join tblCONTServiceProperty prop on allot.SPropertyId=prop.Id
  join tblCONTServiceProvider sp on sp.Id=prop.SPId
  where seg.Name='STORBIL-MPS' and sp.ContractId in (1021)  and seg.SegmentId='ABC123'
Posted
Updated 25-Apr-16 22:07pm

1 solution

Add another "where" clause with an "exists" query:

SQL
and not exists (select * from [dbo].[tblACCTSPContractTariff] where TariffName = 'MPS_FLAT_MINIVAN_247')
 
Share this answer
 
Comments
Muhammad Islam Manzoor 2-May-16 9:01am    
Hi,

I Want to store the resultant values in some variable for 1. allotRateHour.Id (Used in select) 2. id (Used in 2nd Select) 3. TariffType (Used in 3rd select) parameters and than want to insert into the table "tblCONTSPRateHourTariff" where i have table columns as (RateHourId, TariffId, Type) after checking the resultant values stored in variable if the values exist in table or not under (RateHourId, TariffId, Type) columns.

Please suggest how to do it?
Andy Lanng 3-May-16 4:06am    
I've been off work for a few days. Have you had an answer to this?
Muhammad Islam Manzoor 3-May-16 5:20am    
hi, Actually your answer was right for i have asked before. But i have some more queries, if you like to help. I am getting error for below mentioned query.

Begin

Declare @AllotratehoursId int
Declare @Tariffid nvarchar(50)
Declare @TariffType int


IF NOT EXISTS(

Select @AllotratehoursId = allotRateHour.Id
,Select @Tariffid = (id FROM [dbo].[tblACCTSPContractTariff] where TariffName='MPS_FLAT_MINIVAN_247' and ContractDetailId=1021)
,Select @TariffType = (TariffType FROM [dbo].[tblACCTSPContractTariff] where TariffName='MPS_FLAT_MINIVAN_247' and ContractDetailId=1021)
from tblCONTAllotInventory allot
join tblCONTSPRateHour allotRateHour on allot.Id=allotRateHour.AllotInventoryId
--join tblCONTSPRateHourTariff rateHourTariff on allotRateHour.id=rateHourTariff.RateHourId
join [tblCONTAllotInventSegment] allotSeg on allot.Id=allotSeg.AllotInventoryId
join tblSYSASegment seg on allotSeg.SegmentId=seg.Id
join tblCONTServiceProperty prop on allot.SPropertyId=prop.Id
join tblCONTServiceProvider sp on sp.Id=prop.SPId
where seg.Name='STORBIL-MPS' and sp.ContractId in (1021) and seg.SegmentId='SWE001S02'--)

Begin

insert into tblCONTSPRateHourTariff (RateHourId, TariffId, Type)
values ( @AllotratehoursId,@Tariffid,@TariffType)

End

End

But i am getting errors while executing above.

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'from'.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'from'.

Please help and thanks in advance.
Andy Lanng 3-May-16 6:09am    
Ah - I see the problem.
There are two types of select query. The value select is a plain old select query but a variable select is what you use to populate the variables.

In a "Exists" clause you must use the value select query. The "Exists" clause evaluates if any results are returned from the query is evaluates. Variable selects do not return values in favour of populating the variables instead and so cannot be used in the "Exists" clause

If you want more help then you will have to post a new question. It's not very easy to respond in comments alone. You can refer to this question in your new one if you need to.
Muhammad Islam Manzoor 3-May-16 8:54am    
thanks for reply.Means i have to post one more question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900