Saturday, February 25, 2012

only 1 row from a join

I need to select one column of the first row of a left=20
join operation to use it to insert one row in a table, so=20
a put (in a trigger):
CREATE TRIGGER NEWCUMPLE
ON contactos
AFTER INSERT
AS
declare @.fecha datetime, @.alias varchar(20),@.usuario=20
varchar(20),@.grupo varchar(20)=20
set @.fecha =3D(select fechanac from inserted)
set @.alias=3D(select alias from inserted)
set @.usuario=3D(select usuario from inserted)
set @.grupo=3D (select grupo from inserted)
=20
IF (@.fecha IS NOT NULL) /* Tiene fecha de nacimiento */
begin
/***crear una cita de cumplea=F1os ***/
DECLARE @.hora CHAR(5)
SET @.hora=3D(SELECT horasCumples.hora FROM horasCumples=20
LEFT JOIN Vcitas ON horasCumples.hora=3DVcitas.hora)
INSERT INTO Vcitas
(usuario,fecha,hora,motivo,tipo,citaCon,
aviso,descripcion,c
ategoria,zona,horaAbsoluta,fechaAbsoluta
)
VALUES=20
(@.usuario,@.fecha,@.hora,'Cumplea=F1os','P
',@.alias,'Si',NULL,NU
LL,NULL,NULL,NULL)
/*** prueba: update contactos set fechanac=20
=3D '10/10/1980' where alias=3D@.alias and usuario=3D@.usuario and=20
grupo=3D@.grupo ***/
end=20
GO=20
this returns me a message saying that the subquery=20
returned more than 1 result and it is not allowed (in the=20
set @.hora=3D(select... line)
What may I do?
Thnx in advance(reply cross-posted to microsoft.public.sqlserver.programming)
quote:

> I need to select one column of the first row of a left
> join operation to use it to insert one row in a table, so
> a put (in a trigger):

Which row is the 'first'? The row with the earliest datetime value? Rows
in a relational database table have no order so you need to specify your
rules that identify the 'first' row.
quote:

> SET @.hora=(SELECT horasCumples.hora FROM horasCumples
> LEFT JOIN Vcitas ON horasCumples.hora=Vcitas.hora)

Only horasCumples.hora is referenced in the column so the LEFT JOIN to the
Vcitas table serves no purpose. You will get all rows from horasCumples
regardless of the Vcitas table contents. This may not be exactly what you
want but you can use the following to select the horasCumple.hora value with
the earliest date:
SELECT @.hora= MIN(hora) FROM horasCumples
There are some fundamental problems with your trigger that need to be
addressed. Importantly, more than one row can be inserted with a single
INSERT statement. The example below illustrates a set-based technique that
eliminates the need to select values from the inserted table into variables.
INSERT INTO Vcitas
(
usuario,
fecha,
hora,
motivo,
tipo,
citaCon,
aviso,
descripcion,
categoria,
zona,
horaAbsoluta,
fechaAbsoluta
)
SELECT
i.usuario,
i.fechanac,
@.hora,
'Cumpleaos',
'P',
i.alias,
'Si',
NULL,
NULL,
NULL,
NULL,
NULL
FROM inserted i
Hope this helps.
Dan Guzman
SQL Server MVP
"pedro j." <anonymous@.discussions.microsoft.com> wrote in message
news:01d101c3d893$c5d9e710$a101280a@.phx.gbl...
I need to select one column of the first row of a left
join operation to use it to insert one row in a table, so
a put (in a trigger):
CREATE TRIGGER NEWCUMPLE
ON contactos
AFTER INSERT
AS
declare @.fecha datetime, @.alias varchar(20),@.usuario
varchar(20),@.grupo varchar(20)
set @.fecha =(select fechanac from inserted)
set @.alias=(select alias from inserted)
set @.usuario=(select usuario from inserted)
set @.grupo= (select grupo from inserted)
IF (@.fecha IS NOT NULL) /* Tiene fecha de nacimiento */
begin
/***crear una cita de cumpleaos ***/
DECLARE @.hora CHAR(5)
SET @.hora=(SELECT horasCumples.hora FROM horasCumples
LEFT JOIN Vcitas ON horasCumples.hora=Vcitas.hora)
INSERT INTO Vcitas
(usuario,fecha,hora,motivo,tipo,citaCon,
aviso,descripcion,c
ategoria,zona,horaAbsoluta,fechaAbsoluta
)
VALUES
(@.usuario,@.fecha,@.hora,'Cumpleaos','P',
@.alias,'Si',NULL,NU
LL,NULL,NULL,NULL)
/*** prueba: update contactos set fechanac
= '10/10/1980' where alias=@.alias and usuario=@.usuario and
grupo=@.grupo ***/
end
GO
this returns me a message saying that the subquery
returned more than 1 result and it is not allowed (in the
set @.hora=(select... line)
What may I do?
Thnx in advance

No comments:

Post a Comment