Problemas con la claussula UNION

03/08/2006 - 18:55 por Javier | Informe spam
Hola, Tengo el siguiente problema, al intenear ejecutar una consunta en al
analizador de condultas me devuelve el siguiente error:
Servidor: mensaje 104, nivel 15, estado 1, línea 5

La consulta es algo así:
Select * from (... consulta compleja...) T1
UNION
Select * from (...Consulta selncilla...) T2
ORDER BY 1

Muchas Gracias.

Javier Centurión
Argentina

Para más detalles les transcribo la consulta real:


declare @Med_ID as INT, @fecHasta as datetime
set @Med_ID = 670
set @fecHasta = '01/07/2006'

SELECT * FROM (
SELECT Tur_ID = CAST(Tur_Turnos.Tur_ID AS INT),
Pac_ID = CAST(PMC_Pacientes.Pac_ID AS INT),
Paciente = CAST(PMC_Pacientes.Pac_Apellido + ISNULL(', ' +
PMC_Pacientes.Pac_Nombre, '') AS Varchar(50)),
Cons_Descripcion = CAST(Tur_Turnos.Tur_Cons_Descripcion AS Varchar(99)),
NombreDia = CAST(DATENAME(dw, Tur_Turnos.Tur_Fecha) AS Varchar(15)),
Tur_Fecha = CAST(Tur_Turnos.Tur_Fecha AS Varchar(50)),
Os_Descripcion = CAST(PMC_ObrasSociales.Os_Descripcion AS Varchar(99)),
Tur_NomencCodigo = CAST(Tur_Turnos.Tur_NomencCodigo AS Varchar(15)),
Cantidad = CAST(1 AS INT),
Medico = CAST(PMC_Medicos.Med_Apellido + ISNULL(', ' +
PMC_Medicos.Med_Nombre, '') AS Varchar(50)),
Log_Operador = CAST(Operador.Log_Operador AS Varchar(50))
FROM Tur_Turnos
INNER JOIN PMC_Medicos ON Tur_Turnos.Tur_Med_NumeroInterno_Atiende =
PMC_Medicos.Med_NumeroInterno
INNER JOIN PMC_Pacientes ON Tur_Turnos.Pac_ID = PMC_Pacientes.Pac_ID
INNER JOIN PMC_ObrasSociales ON Tur_Turnos.Os_ID = PMC_ObrasSociales.Os_ID
INNER JOIN (
SELECT Tur_ID, Log_ID, Log_Operador
FROM Tur_LogUser T1 WHERE
(T1.Log_Tur_Presentismo=convert(bit, 1))
AND NOT EXISTS (SELECT Log_ID FROM Tur_LogUser T2 WHERE (T2.Tur_ID =
T1.Tur_ID) AND (T2.Log_Tur_Presentismo=convert(bit, 0)) AND
(T2.Log_ID>T1.Log_ID))
AND 0 = IsNull((SELECT TOP 1 Log_Tur_Presentismo FROM Tur_LogUser T2
WHERE (T2.Tur_ID = T1.Tur_ID) AND T2.Log_ID<T1.Log_ID ORDER BY Log_ID DESC),
0)
) Operador ON Tur_Turnos.Tur_ID = Operador.Tur_ID
WHERE ((PMC_Medicos.Med_ID = @Med_ID) AND (Tur_Turnos.Tur_Presentismo =
convert(bit, 1)) AND (Tur_Turnos.Tur_Fecha > convert(datetime, @fecHasta,
103)))
) T1

UNION ALL

SELECT * FROM (
SELECT Tur_ID = CAST(Tur_ID AS INT),
Pac_ID = CAST(0 AS INT),
Paciente = CAST('' AS Varchar(50)),
Cons_Descripcion = CAST('' AS Varchar(99)),
NombreDia = CAST('' AS Varchar(15)),
Tur_Fecha = CAST('' AS Varchar(50)),
Os_Descripcion = CAST('' AS Varchar(99)),
Tur_NomencCodigo = CAST(Tur_NomencCodigo AS Varchar(15)),
Cantidad = CAST(TPr_Cantidad AS Varchar(15)),
Medico = CAST('' AS Varchar(50)),
Log_Operador = CAST('' AS Varchar(50))
FROM Tur_Turnos_MasPracticas
WHERE (Tur_ID IN (
SELECT Tur_ID FROM Tur_Turnos
INNER JOIN PMC_Medicos ON Tur_Turnos.Tur_Med_NumeroInterno_Atiende =
PMC_Medicos.Med_NumeroInterno
WHERE (PMC_Medicos.Med_ID = @Med_ID) AND (Tur_Turnos.Tur_Presentismo =
convert(bit, 1)) AND (Tur_Turnos.Tur_Fecha > convert(datetime, @fecHasta,
103))
))
) T2
ORDER BY 1
 

Leer las respuestas

#1 Maxi
03/08/2006 - 19:17 | Informe spam
Hola, eso solo dice el mensaje de error? primero revisa que ambsos select
tengan la misma cantidad de columnas



Salu2

Microsoft MVP SQL Server
www.sqlgururs.org
Email:


"Javier" wrote in message
news:
Hola, Tengo el siguiente problema, al intenear ejecutar una consunta en al
analizador de condultas me devuelve el siguiente error:
Servidor: mensaje 104, nivel 15, estado 1, línea 5

La consulta es algo así:
Select * from (... consulta compleja...) T1
UNION
Select * from (...Consulta selncilla...) T2
ORDER BY 1

Muchas Gracias.

Javier Centurión
Argentina

Para más detalles les transcribo la consulta real:


declare @Med_ID as INT, @fecHasta as datetime
set @Med_ID = 670
set @fecHasta = '01/07/2006'

SELECT * FROM (
SELECT Tur_ID = CAST(Tur_Turnos.Tur_ID AS INT),
Pac_ID = CAST(PMC_Pacientes.Pac_ID AS INT),
Paciente = CAST(PMC_Pacientes.Pac_Apellido + ISNULL(', ' +
PMC_Pacientes.Pac_Nombre, '') AS Varchar(50)),
Cons_Descripcion = CAST(Tur_Turnos.Tur_Cons_Descripcion AS Varchar(99)),
NombreDia = CAST(DATENAME(dw, Tur_Turnos.Tur_Fecha) AS Varchar(15)),
Tur_Fecha = CAST(Tur_Turnos.Tur_Fecha AS Varchar(50)),
Os_Descripcion = CAST(PMC_ObrasSociales.Os_Descripcion AS Varchar(99)),
Tur_NomencCodigo = CAST(Tur_Turnos.Tur_NomencCodigo AS Varchar(15)),
Cantidad = CAST(1 AS INT),
Medico = CAST(PMC_Medicos.Med_Apellido + ISNULL(', ' +
PMC_Medicos.Med_Nombre, '') AS Varchar(50)),
Log_Operador = CAST(Operador.Log_Operador AS Varchar(50))
FROM Tur_Turnos
INNER JOIN PMC_Medicos ON Tur_Turnos.Tur_Med_NumeroInterno_Atiende > PMC_Medicos.Med_NumeroInterno
INNER JOIN PMC_Pacientes ON Tur_Turnos.Pac_ID = PMC_Pacientes.Pac_ID
INNER JOIN PMC_ObrasSociales ON Tur_Turnos.Os_ID = PMC_ObrasSociales.Os_ID
INNER JOIN (
SELECT Tur_ID, Log_ID, Log_Operador
FROM Tur_LogUser T1 WHERE
(T1.Log_Tur_Presentismo=convert(bit, 1))
AND NOT EXISTS (SELECT Log_ID FROM Tur_LogUser T2 WHERE (T2.Tur_ID > T1.Tur_ID) AND (T2.Log_Tur_Presentismo=convert(bit, 0)) AND
(T2.Log_ID>T1.Log_ID))
AND 0 = IsNull((SELECT TOP 1 Log_Tur_Presentismo FROM Tur_LogUser T2
WHERE (T2.Tur_ID = T1.Tur_ID) AND T2.Log_ID<T1.Log_ID ORDER BY Log_ID
DESC),
0)
) Operador ON Tur_Turnos.Tur_ID = Operador.Tur_ID
WHERE ((PMC_Medicos.Med_ID = @Med_ID) AND (Tur_Turnos.Tur_Presentismo > convert(bit, 1)) AND (Tur_Turnos.Tur_Fecha > convert(datetime, @fecHasta,
103)))
) T1

UNION ALL

SELECT * FROM (
SELECT Tur_ID = CAST(Tur_ID AS INT),
Pac_ID = CAST(0 AS INT),
Paciente = CAST('' AS Varchar(50)),
Cons_Descripcion = CAST('' AS Varchar(99)),
NombreDia = CAST('' AS Varchar(15)),
Tur_Fecha = CAST('' AS Varchar(50)),
Os_Descripcion = CAST('' AS Varchar(99)),
Tur_NomencCodigo = CAST(Tur_NomencCodigo AS Varchar(15)),
Cantidad = CAST(TPr_Cantidad AS Varchar(15)),
Medico = CAST('' AS Varchar(50)),
Log_Operador = CAST('' AS Varchar(50))
FROM Tur_Turnos_MasPracticas
WHERE (Tur_ID IN (
SELECT Tur_ID FROM Tur_Turnos
INNER JOIN PMC_Medicos ON Tur_Turnos.Tur_Med_NumeroInterno_Atiende > PMC_Medicos.Med_NumeroInterno
WHERE (PMC_Medicos.Med_ID = @Med_ID) AND (Tur_Turnos.Tur_Presentismo > convert(bit, 1)) AND (Tur_Turnos.Tur_Fecha > convert(datetime, @fecHasta,
103))
))
) T2
ORDER BY 1




Preguntas similares