Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi to all,

i have a task,that contain 3 tables city,state and country.

city(cid,cityname,cityStateid),

state(sid,statename,stateCountryId),

country(countryId,countryname)

cid,sid,countryid have primary keys and statecountryid has foreign key on countryid as well as citystateid has foreign key on sid.

So i have to insert into these 3 tables using a view of 3 fields city,state,country.

I have created a trigger,but unable to do.

Please reply me if you know.

thank you.

Srinivas Vadepally,
Captor Solutions.
Posted
Comments
Toniyo Jackson 9-Aug-11 6:14am    
Show your trigger code.
srinivas vadepally 9-Aug-11 6:48am    
create trigger sriviewtrigger9
on sriview
instead of insert
as
begin
set nocount on
declare @countryid int
declare @stateid int
declare @statecid int
declare @cityid int
declare @citystateid int

if(not exists (select ctry.countryname from country ctry,inserted i where ctry.countryname=i.country))
insert into country(countryname) select country from inserted


if(not exists (select s.statename from state s,inserted i where s.statename=i.state))
begin

insert into state (statecid,statename) values(select ctry.countryid from country ctry,inserted i where ctry.countryname=i.country,select state from inserted)
end

if(not exists(select c.cityname from city c,inserted i where c.cityname=i.city))
begin
set @stateid=select s.stateid from state s,inserted i where s.statname=i.state
insert into city (cityname,citystateid) values (select city from inserted,@stateid)
end

end

A view cannot be used to manipulate data - as the name suggests it helps you query data.
However, you could look at this[^] - the link uses a view to select data and then insert it into a table.
 
Share this answer
 
Hi, View is generally used show the data in a table or tables depending on the 'where ' condition given.


View cannot be used to insert data in any table or tables.You will have to use either procedure or trigger to insert values.
 
Share this answer
 
Comments
srinivas vadepally 9-Aug-11 6:02am    
hi sujith,
i have created trigger,still i am unable to that,just explain with the above example please....
sujit0761 9-Aug-11 8:58am    
@countryid
@stateid
@statecid
@cityid
@citystateid

are you passing all these parameters to the procedure .How exactly are you executing your procedure?
Could you please put down your script.I'll let you know where are you going wrong.

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