I have Plan table that has a one to many relationship with a Price table. (1 plan can have many prices)
However the problem is I have another table called "StandardPrices" that holds the names of all the prices (The reason for this is I want to be able to add or remove prices at any time)
Plan Table:
ID int primary key,
plan Name varchar(200),
...
PriceTable:
ID int primary key,
PlanId int foreign key references plan(ID)
PriceName ID foreign key standardprices(id)
StandardPrices:
ID int primary key,
PriceName varchar(200),
DefaultPrice money
So whenever a plan is created, it automatically creates a List of all the prices in the StandardPrice list (with default values).
The problem I have, Is I need, whenever I create a new StandardPrice, it automatically checks if that price exists in every plan, and if it doesnt, create an entry in the price table for that planid.
I use Stored procedures and thought the best way to do this would be through SQL.
When StandardPrices are created:
begin
insert into StandardPrices (PriceName, Defaultprice)
values (@priceName, @DefaultPrice)
end
begin
//list all plans.
//cross reference PriceTable to see if plan exists
//if not insert priceplan with default value
end
I am a bit confused how i can implement such a sql command?