Principales respuestas
Errores de sintaxis en cursor

Pregunta
-
Hello everyone.
I'm trying to execute a cursor over SQL Server 2005 and I'm lost trying to fix this syntax errors that has been thrown by the query.
The query is this:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --1º PASADA TRUNCATE TABLE tDTUSupervisors TRUNCATE TABLE tSupervisors -- Transfer data from the typed pre-staging to the staging table. INSERT INTO tDTUSupervisors ( Supervisor1ID, Supervisor2ID, PID_GlobalEmpCode, HRPlanPerID ) SELECT -1, -1, Sup.GlobalEmpCode, ( -- Extraemos el identificador de la tabla final SELECT HRPlanPerID FROM tHRPlanningPeriodText tHR WHERE tHR.Code = Sup.EvalPeriod AND tHR.SysLID = 3082 ) FROM tDTUSupervisors2_Typed Sup WHERE Sup.DTUIsGarbage = 0 -- 2º PASADA -- AHORA ACTUALIZAMOS LAS ANTERIORES FILAS CON SUS SUPERVISORES-- DECLARE @GEC INT, @SGEC INT, @POSID INT, @PIDSUP INT DECLARE cPIDs CURSOR FOR ( SELECT tDTUst.GlobalEmpCode, tDTUst.SuperGlobalEmpCode, tDTUst.PosHierID FROM tDTUSupervisors_Typed tDTUst WHERE tDTUst.DTUIsGarbage = 0 ) OPEN cPIDs -- ¿Hay que hacer un fetch antes de este ciclo? -- FETCH cPIDs -------------------------------------------------- WHILE (@@FETCH_STATUS = 0) BEGIN FETCH NEXT FROM cPIDs INTO @GEC,@SGEC,@POSID SELECT tE.PID INTO @PIDSUP FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGEC IF (@POSID = 1) BEGIN UPDATE TABLE tDTUSupervisors tDTUSup SET Supervisor1ID = @PIDSUP END ELSE BEGIN UPDATE TABLE tDTUSupervisors tDTUSup SET Supervisor2ID = @PIDSUP END WHERE tDTUSup.PID = (SELECT tE.PID FROM tEmployee tE WHERE tE.GlobalEmpCode = @GEC) END CLOSE cPIDs DEALLOCATE cPIDs
Msg 102, Level 15, State 1, Line 54
Incorrect syntax near '@PIDSUP'.
Msg 156, Level 15, State 1, Line 57
Incorrect syntax near the keyword 'TABLE'.
Msg 156, Level 15, State 1, Line 61
Incorrect syntax near the keyword 'TABLE'.
Msg 102, Level 15, State 1, Line 66
Incorrect syntax near 'cPIDs'.
Can anyone help me? I'm going crazy solving this.
Very thanks.- Editado Gustavo LarrieraModerator sábado, 17 de octubre de 2009 22:49 Título traducido a español.
sábado, 17 de octubre de 2009 16:21
Respuestas
-
Hola.Lo primero, este foro es de SQL Server en Español, así que para que todos nos entendamos, te contesto en este idioma.En la línea que indica el error encuentras esto:SELECT tE.PID INTO @PIDSUP FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGEC@PIDSUP es una variable declarada como entero, no es una tabla. Si es una asignación de la variable, sería así:SELECT @PIDSUP = tE.PID FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGECSi fuera una variable de tipo tabla, tendrías que declararla y luego rellenarla con insert... select, no con select ... into.Más abajo, la sintaxis del update, no es correcta, no es "update table...", es sin la palabra "table".Si no das con ello, nos dices.
Alberto López Grande (Visita mi blog en http://qwalgrande.blogspot.es/)- Propuesto como respuesta Alberto López Grande (qwalgrande)Moderator sábado, 17 de octubre de 2009 16:27
- Marcado como respuesta felmoltor sábado, 17 de octubre de 2009 17:41
sábado, 17 de octubre de 2009 16:27Moderador
Todas las respuestas
-
Hola.Lo primero, este foro es de SQL Server en Español, así que para que todos nos entendamos, te contesto en este idioma.En la línea que indica el error encuentras esto:SELECT tE.PID INTO @PIDSUP FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGEC@PIDSUP es una variable declarada como entero, no es una tabla. Si es una asignación de la variable, sería así:SELECT @PIDSUP = tE.PID FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGECSi fuera una variable de tipo tabla, tendrías que declararla y luego rellenarla con insert... select, no con select ... into.Más abajo, la sintaxis del update, no es correcta, no es "update table...", es sin la palabra "table".Si no das con ello, nos dices.
Alberto López Grande (Visita mi blog en http://qwalgrande.blogspot.es/)- Propuesto como respuesta Alberto López Grande (qwalgrande)Moderator sábado, 17 de octubre de 2009 16:27
- Marcado como respuesta felmoltor sábado, 17 de octubre de 2009 17:41
sábado, 17 de octubre de 2009 16:27Moderador -
¡Ups!
Muchas gracias por la respuesta.
He escrito en inglés por que tenía puesto el automático de escribir en los foros.
Ahora no me da fallos de sintaxis:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --1º PASADA TRUNCATE TABLE tDTUSupervisors TRUNCATE TABLE tSupervisors -- Transfer data from the typed pre-staging to the staging table. INSERT INTO tDTUSupervisors ( Supervisor1ID, Supervisor2ID, PID_GlobalEmpCode, HRPlanPerID ) SELECT -1, -1, Sup.GlobalEmpCode, ( -- Extraemos el identificador de la tabla final SELECT HRPlanPerID FROM tHRPlanningPeriodText tHR WHERE tHR.Code = Sup.EvalPeriod AND tHR.SysLID = 3082 ) FROM tDTUSupervisors2_Typed Sup WHERE Sup.DTUIsGarbage = 0 -- 2º PASADA -- AHORA ACTUALIZAMOS LAS ANTERIORES FILAS CON SUS SUPERVISORES-- DECLARE @GEC INT, @SGEC INT, @POSID INT, @PIDSUP INT, @PIDEMP INT DECLARE cPIDs CURSOR FOR ( SELECT tDTUst.GlobalEmpCode, tDTUst.SuperGlobalEmpCode, tDTUst.PosHierID FROM tDTUSupervisors_Typed tDTUst WHERE tDTUst.DTUIsGarbage = 0 ) OPEN cPIDs -- ¿Hay que hacer un fetch antes de este ciclo? -- FETCH cPIDs INTO @GEC,@SGEC,@POSID -------------------------------------------------- WHILE (@@FETCH_STATUS = 0) BEGIN SELECT @PIDSUP = tE.PID FROM tEmployee tE WHERE tE.GlobalEmpCode = @SGEC SELECT @PIDEMP = tE.PID FROM tEmployee tE WHERE tE.GlobalEmpCode = @GEC IF (@POSID = 1) BEGIN UPDATE tDTUSupervisors SET Supervisor1ID = @PIDSUP WHERE tDTUSup.PID = @PIDEMP END ELSE BEGIN UPDATE tDTUSupervisors SET Supervisor2ID = @PIDSUP WHERE tDTUSup.PID = @PIDEMP END FETCH NEXT FROM cPIDs INTO @GEC,@SGEC,@POSID END CLOSE cPIDs DEALLOCATE cPIDs
¡Gracias!sábado, 17 de octubre de 2009 17:43