| Author |
Topic  |
|
|
JustJones
Starting Member
4 Posts |
Posted - 01/10/2013 : 14:57:58
|
Hi everyone, I have 4 tables:
CREATE TABLE employee
(so_number char(11) not null
primary key,
work_area varchar(30) null
)
CREATE TABLE service
(ev_code int
primary key,
id_aircraft int not null
references plane (id_aircraft),
so_number char(11) not null
references employee (so_number)
)
CREATE TABLE plane
(id_aircraft int not null
primary key,
han_id int not null references hangar (han_id)
)
CREATE TABLE hangar
(han_id int not null
primary key,
location varchar(30) not null,
)
After that I inserted:
insert into hangar values
('19','easter part','7','0'),
('11','western part','5','1'),
('12','southern part','6','1'),
('14','northern part','8','1')
insert into samolot values
('123','FSS-123','19','15552'),
('452','MKN-452','12','14452'),
('113','BDG-123','14','19972'),
('872','LQH-872','19','15552'),
('820','NQG-820','14','14452'),
('012','AFG-012','11','19972'),
('044','NQG-044','12','19972')
insert into employee values
('CA-243564-R','1100','eastern part','1','88062515488'),
('GF-876876-H','3300','eastern part','1','78022216872'),
('HW-253465-J','1800','southern part','2','78011615632'),
('SD-857206-W','2100','eastern part','1','76111513321'),
('BV-476789-X','3000','western part','2','79081546588')
insert into service values
('132122','2012-12-31','5','123','CA-243564-R'),
('468202','2012-04-12','8','123','GF-876876-H'),
('146444','2012-03-23','22','452','HW-253465-J'),
('187985','2012-06-12','1','872','SD-857206-W'),
('133564','2012-06-10','48','012','BV-476789-X')
After that I wrote view:
create view workarea
as
select e.so_number, e.work_area,
h.location, p.id_aircraft from employee e
join service se on e.so_number = se.so_number
join plane p on p.id_aircraft = se.id_aircraft
join hangar h on h.han_id = p.han_id
And I would like to use it in trigger but this lines
declare @work_area varchar(40)=(select work_area from workarea where so_number = @so_number) --I take @so_number from inserted table and put it to this variable
declare @location varchar(40)=(select location from workarea where id_aircraft = @id_aircraft)--here the same, I declared id_aircraft in trigger and assigned select from inserted table cause an error and I gets a message: "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated." Could someone help me with that ?
What should i change ? |
|
|
Lamprey
Flowing Fount of Yak Knowledge
3831 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 01/10/2013 : 22:48:20
|
the way you've written logic will work correctly only for single row operations. the error clearly suggests that there was a batch operation.
To handle this you need to write logic like
...
declare @workarea table
(
work_area varchar(100)
)
declare @location table
(
location varchar(100)
)
insert @work_area
select w.work_area from workarea w
inner join inserted i
on i.so_number = w.so_number
insert @location
select w.location
from workarea w
inner join inserted i
on i.id_aircraft = w.id_aircraft
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
wholesalenfljerseyssale
Starting Member
9 Posts |
Posted - 01/11/2013 : 01:15:58
|
Our company are professional wholesaler for Nike NFL Jerseys. We wholesale all kind of jerseys, include NFL, NHL, MLB, NBA and NCAA. All stitched on numbers and names, also with authentic packing jerseys for sale at our site. Huge stock for all kind of jerseys, also full way guaranteed for your goods. Wholesale NFL jerseys, basketball jerseys, hockey jerseys will be our speciality business. We are offical jerseys supplier and manufacturer for all leagues. So you will get correct style, best quality, best service from our site. Thanks for reading and enjoy your shopping! Only Wholesale jerseys at our site! Enjoy shopping for the NFL Jerseys Wholesale online sale and cheap NFL jerseys from China in the professional China NFL jerseys outlet.high quality, lowest Attention Clearance Sale fans! Use the Clearance Sale Categories below to purchase Clearance Sale Apparel, Clearance Sale Gifts and Clearance Sale Merchandise. We are your Jerseys for sale for all the authentic Clearance Sale Gear. Because you are a Clearance Sale fanatic you should have only the best licensed Clearance Sale Apparel guaranteed to show your Clearance Sale Pride. Fanzz only carries licensed Clearance Sale Jerseys, Clearance Sale T-Shirts, Clearance Sale Hats and Clearance Sale Drinkware for all Clearance Sale Players.
Attention Clearance Sale fans! Use the Clearance Sale Categories below to purchase Clearance Sale Apparel, Clearance Sale Gifts and Clearance Sale Merchandise. We are your Jerseys for sale for all the authentic Clearance Sale Gear. Because you are a Clearance Sale fanatic you should have only the best licensed Clearance Sale Apparel guaranteed to show your Clearance Sale Pride. Fanzz only carries licensed Clearance Sale Jerseys, Clearance Sale T-Shirts, Clearance Sale Hats and Clearance Sale Drinkwar |
 |
|
|
JustJones
Starting Member
4 Posts |
Posted - 01/11/2013 : 06:04:14
|
first of all thank you guys for answers I appreciate it @Lamprey - i would like to check if the so_number (employee) have the same string in his work_area filed like aircraft hangar location to which employee will be assign
@visakh16 - So trigger like below should works, but it doesn't, am I right ?
create trigger insert_to_service
on service
for insert
as
declare @so_number char(11) = (select so_number from inserted)
declare @id_aircraft int = (select id_aircraft from inserted)
declare @wa table
(
work_area varchar(100)
)
declare @lo table
(
location varchar(100)
)
insert into @wa
select w.work_area from workarea w
inner join inserted i
on i.so_number = w.so_number
insert into @lo
select w.location
from workarea w
inner join inserted i
on i.id_aircraft = w.id_aircraft
if (select work_area from @wa) = (select location from @lo)
print 'trigger allowed to do this operation !'
else
begin
print 'trigger does not allowe to do this operation ! transaction will be rollback'
rollback
end |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 01/11/2013 : 06:06:57
|
nope...last if doesnt make any sense
can you explain what you're trying to check there?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
JustJones
Starting Member
4 Posts |
Posted - 01/11/2013 : 07:05:14
|
1) I want to insert into table service new row: exec add_to_serwis '232435','2012-10-25','500','123','CA-243564-R' 2) I want to check this guy 'CA-243564-R' to which work area is assigned, i see in the employee table this row: ('CA-243564-R','1100','eastern part','1','88062515488') so I know that he has assigned 'eastern part' 3) I want to check this plane '123' to which hangar is assigned, in the plane table i can see this row: ('123','FSS-123','19','15552') so I know that this plane is assigned to aircraft hanagr number 19 4) Then I check this aircraft hangar so to do that i look at the hangar table ans there is this row: ('19','eastern part','7','0') so know that this hangar has eastern part location 5) I want to check if these values (hanagr location and work are) are equal, and i can see that this guy has work area:eastern part and aircraft hangar location is also: easter part so if should check true otherwise false. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 01/11/2013 : 09:18:04
|
where's the insert logic in current trigger? also where does those value come from?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
Lamprey
Flowing Fount of Yak Knowledge
3831 Posts |
Posted - 01/11/2013 : 13:10:49
|
Any chance yo can fix you script so that we can run it without errors? The tables seem ok, but they are out of order and the insert statements do not make any sense at all.
If we have sample data we might be able to write a query to help. |
 |
|
|
JustJones
Starting Member
4 Posts |
Posted - 01/11/2013 : 17:35:36
|
Ok guys thanks for patience :) and I'm sorry I wrote it again, everything is in correct order with no errors besides my question error. After you run code try run last line, this is my question.
CREATE TABLE hangar
(han_id int not null
primary key,
location varchar(30) not null,
)
CREATE TABLE plane
(id_aircraft int not null
primary key,
han_id int not null references hangar (han_id)
)
CREATE TABLE employee
(so_number char(13) not null
primary key,
work_area varchar(30) null
)
CREATE TABLE service
(ev_code int
primary key,
id_aircraft int not null
references plane (id_aircraft),
so_number char(13) not null
references employee (so_number)
)
/*drop table plane
drop table hangar
drop table service
drop table employee*/
/*select * from hangar
select * from plane
select * from service
select * from employee*/
insert into hangar values
('19','easter part'),
('11','western part'),
('12','southern part'),
('14','northern part')
insert into plane values
('123','19'),
('452','12'),
('113','14'),
('820','14'),
('012','11'),
('044','12')
insert into employee values
('CA-243564-R','eastern part'),
('GF-876876-H','eastern part'),
('HW-253465-J','southern part'),
('SD-857206-W','eastern part'),
('BV-476789-X','western part')
insert into service values
('132122','123','CA-243564-R'),
('146444','452','HW-253465-J'),
('187985','820','SD-857206-W'),
('133564','012','BV-476789-X')
go
create view workarea
as
select e.so_number, e.work_area,
h.location, p.id_aircraft from employee e
join service se on e.so_number = se.so_number
join plane p on p.id_aircraft = se.id_aircraft
join hangar h on h.han_id = p.han_id
go
/*select * from workarea
drop view workarea*/
go
create procedure insert_to_service
@ev_code int,
@id_aircraft int,
@so_number char(13)
as
begin tran
insert into service values (@ev_code,@id_aircraft,@so_number)
if @@error <> 0
rollback
commit tran
go
--drop procedure insert_to_service
go
create trigger check_loc
on service
for insert
as
declare @so_number varchar(14)=(select so_number from inserted)
declare @id_aircraft int=(select id_aircraft from inserted)
declare @work_area varchar(40)=(select work_area from workarea where so_number = @so_number)
declare @location varchar(40)=(select location from workarea where id_aircraft = @id_aircraft)
if (@work_area) = (@location)
print 'hangar location and employee arok area are equal, trigger allowed to insert'
else
begin
print 'hangar location and employee work area are not equal, trigger doesn not allowe to insert, transaction will be rolled back'
rollback
end
--drop trigger check_loc
exec insert_to_service '123456','123','HW-253465-J' |
Edited by - JustJones on 01/11/2013 17:38:33 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47157 Posts |
Posted - 01/12/2013 : 04:05:33
|
I think what you need is a instead of trigger like this
create trigger check_loc
on service
instead of insert
as
insert into service
select ev_code,id_aircraft,son_number
from inserted i
inner join workarea w1
on w1.so_number = i.so_number
inner join workarea w2
on w2.id_aircraft = i.id_aircraft
where w1.work_area = w2.location
if exists (select 1
from inserted i
inner join workarea w1
on w1.so_number = i.so_number
inner join workarea w2
on w2.id_aircraft = i.id_aircraft
where w1.work_area <> w2.location
)
begin
print 'hangar location and employee work area are not equal, trigger doesn not allowe to insert, transaction will be rolled back'
rollback
end
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|
|
|