SQL Dinamicamente ( En Run-Time )

02/05/2004 - 17:19 por Mario Reiley | Informe spam
Al tratar de crear dinamicamente el siguiente SQL, el servidor me arroja
este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue, htDetalleNormal.Vie,
htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS Hasta
' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId FULL
OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId = htDetalleNormal.HojaId '
+
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ' >= ' +
CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario

Preguntas similare

Leer las respuestas

#1 Juan Carlos Leguizamón
02/05/2004 - 21:54 | Informe spam
Prueba agregando un RTRIM a los CONVERT(DATETIME,@Fxxx, 102))

"Mario Reiley" escribió en el mensaje
news:
Al tratar de crear dinamicamente el siguiente SQL, el servidor me arroja
este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue, htDetalleNormal.Vie,
htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS


Hasta
' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId > Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId FULL
OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId = htDetalleNormal.HojaId


'
+
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ' >= '


+
CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario


Respuesta Responder a este mensaje
#2 Maxi
03/05/2004 - 05:04 | Informe spam
Hola, revisate este articulo si?

http://www.mug.org.ar/SQL/ArticSQL/240.aspx

Suerte


Salu2

Maxi

Desarrollador 3 estrellas .NET
Buenos Aires - Argentina

MSN:

"Mario Reiley" escribió en el mensaje
news:
Al tratar de crear dinamicamente el siguiente SQL, el servidor me arroja
este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue, htDetalleNormal.Vie,
htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS


Hasta
' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId > Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId FULL
OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId = htDetalleNormal.HojaId


'
+
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ' >= '


+
CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario


Respuesta Responder a este mensaje
#3 ulises
03/05/2004 - 05:04 | Informe spam
Maneja las fechas en formato ISO "AAAAMMDD HH:MM:SS".

Saludos,
Ulises

On Sun, 2 May 2004 11:19:00 -0400, "Mario Reiley"
wrote:

Al tratar de crear dinamicamente el siguiente SQL, el servidor me arroja
este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue, htDetalleNormal.Vie,
htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS Hasta
' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId >Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId FULL
OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId = htDetalleNormal.HojaId '
+
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ' >= ' +
CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario

Respuesta Responder a este mensaje
#4 Mario Reiley
03/05/2004 - 13:39 | Informe spam
Gracias por sus comentarios. Lamentablemente para mi no he logrado resolver
mi problema.

Gracias nuevamente
Mario

"Mario Reiley" wrote in message
news:
Al tratar de crear dinamicamente el siguiente SQL, el servidor me arroja
este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue, htDetalleNormal.Vie,
htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS


Hasta
' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId > Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId FULL
OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId = htDetalleNormal.HojaId


'
+
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ' >= '


+
CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario


Respuesta Responder a este mensaje
#5 Javier Loria
03/05/2004 - 15:30 | Informe spam
Hola Mario:
Asumiendo que: @Desde y @Hasta son INT, y que @FDesde y @FHasta son
DateTime o SmallDatetime.
Hay varios errores de sintaxis:
a) En:
+ (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + ...
Debe eliminarse el parentisis inicial.
b) En:
... + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ..
c) La conversion:
... CONVERT(DATETIME,@FDesde, 102)) ...
Debe ser hacia un CHAR(10) y eliminar el parentisis final.
d) La conversion:
... CONVERT(DATETIME,@FHasta, 102)) ...
Igualmente debe ser hacia un CHAR(10) y eliminar el parentisis final.
e) Deber revisar que tengas espacios adecuados en la concatenaciones por
ejemplo debe ser ' AND ' con espacios antes y despues.
En general esos son los errores mas obvios de sintaxis, pero el error
mas grande es usar SQL Dinamico, que te produce problemas de seguridad, de
rendimiento y de mantenimiento haciendo tu codigo imposible de depurar y de
entender.
Saludos,


Javier Loria
Costa Rica
Se aprecia la inclusion de DDL (CREATE, INSERTS, etc.)
que pueda ser copiado y pegado al Query Analizer.
La version de SQL y Service Pack tambien ayuda.
Mario Reiley escribio:
Al tratar de crear dinamicamente el siguiente SQL, el servidor me
arroja este error:

Error de sintaxis al convertir una cadena de caracteres a datetime

este es el SQL:

set @sql = N'SELECT htDetalleNormal.Lun, htDetalleNormal.Mar,
htDetalleNormal.Mie, htDetalleNormal.Jue,
htDetalleNormal.Vie, htDetalleNormal.HojaId, ' +
'htDetalleNormal.FechaMie AS Desde, htDetalleNormal.FechaMie AS
Hasta ' +
'FROM personal FULL OUTER JOIN ' +
'Clasificacion ON personal.ClasificacionId > Clasificacion.ClasificacionId FULL OUTER JOIN ' +
'Hojatiempo ON personal.CedulaId = Hojatiempo.CedulaId
FULL OUTER JOIN ' +
'htDetalleNormal ON Hojatiempo.HojaId > htDetalleNormal.HojaId ' +
'WHERE ' + (COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Desde) + '
>= ' + CONVERT(DATETIME,@FDesde, 102)) + 'AND ' +
(COL_NAME(OBJECT_ID('dbo.HtDetalleNormal'),@Hasta) + ' <= ' +
CONVERT(DATETIME,@FHasta, 102)) + ' for browse'

Gracias de antemano.

Mario
Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaSiguiente Respuesta Tengo una respuesta
Search Busqueda sugerida