Ver Mensaje Individual
  #17 (permalink)  
Antiguo 07/12/2012, 11:46
jnavarrete2311
 
Fecha de Ingreso: noviembre-2012
Mensajes: 30
Antigüedad: 12 años
Puntos: 0
Respuesta: Asignar valor a variable dentro de if

Pues no consigo hacerlo en workbench ni en phpmyadmin, workbench solo me dice que tengo errores de sintaxis pero no me dice donde y phpmyadmin me marca un error en un insert pero no tengo idea de por que este es el query ya con delimiter en phpmyadmin:

Código MySQL:
Ver original
  1. Delimiter //
  2. CREATE PROCEDURE sp_InsertCustomer (
  3.     IN Firstname VARCHAR(100),
  4.     IN Lastname VARCHAR(100),
  5.     IN Email VARCHAR(100),
  6.     IN Birthday DATETIME,
  7.    
  8.     IN Company VARCHAR(100),
  9.     IN Address1 VARCHAR(100),
  10.     IN Postcode INT,
  11.     IN City VARCHAR(100),
  12.     IN Phone INT
  13.     )
  14. DECLARE regs        INT;
  15.     DECLARE ExistsUser   INT;
  16.     DECLARE Customer_ID INT;
  17.     SET regs = 0;
  18.     SELECT COUNT(*) INTO regs
  19.     FROM `ps_customer`
  20.     WHERE id_customer = 3;
  21.  
  22.     IF regs > 0 THEN
  23.         SET ExistsUser = 1;
  24.     END IF;
  25.  
  26.     IF ExistsUser = 1 THEN
  27.         INSERT INTO  `ps_customer`
  28.             (id_default_group,id_risk,firstname,lastname,email, passwd,birthday, newsletter_date_add,max_payment_days,secure_key,ac tive,date_add,date_upd)
  29.         VALUES(
  30.                3, 0, Firstname, Lastname, Email, '12345',Birthday,'0000-00-00 00:00:00',0,
  31.                '2293b4751c4ff4517bb997eb9c141a76',1,CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()
  32.               );
  33.  
  34.         SET Customer_ID = LAST_INSERT_ID();
  35.         IF Customer_ID > 0 THEN
  36.             INSERT INTO  `ps_customer_group` (id_customer, id_group)
  37.                 VALUES(Customer_ID, 3);
  38.             INSERT INTO  `ps_guest`(id_operating_system, id_web_browser, id_customer, screen_resolution_x, screen_resolution_y,
  39.  screen_color, sun_java, adobe_flash, adobe_director, apple_quicktime, real_player, windows_media, accept_language)
  40.                 VALUES(3, 10, Customer_ID, 0, 0, 0, 0, 0, 0, 0, 0, 0,'');
  41.             INSERT INTO  `ps_address`(id_country, id_state, id_customer, id_manufacturer, id _supplier, id_warehouse, alias, company, lastname, firstname, address1,
  42.  postcode, city, phone, vat_number, dni, DATE_ADD, date_upd, active, deleted)
  43.                 VALUES(145, 67, Customer_ID, 0, 0, 0, 'Business', Company, Lastname, Firstname, Address1, Postcode, City, Phone, '','' , CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 1, 0);
  44.         END IF;
  45.     END IF;
  46. //
-----------------------------------------------------------------------------------------------------------

y este es el error que marca:

Código MySQL:
Ver original
  1. Error
  2.  
  3. Parece haber un error en su consulta de SQL. La salida generada por el servidor de MySQL, de existir, aparece abajo, en cuyo caso puede ayudar a diagnosticar el problema.
  4.  
  5. ERROR: Signo de puntuación desconocido @ 1947
  6. STR: //
  7. SQL: CREATE PROCEDURE sp_InsertCustomer (
  8.     IN Firstname VARCHAR(100),
  9.     IN Lastname VARCHAR(100),
  10.     IN Email VARCHAR(100),
  11.     IN Birthday DATETIME,
  12.    
  13.     IN Company VARCHAR(100),
  14.     IN Address1 VARCHAR(100),
  15.     IN Postcode INT,
  16.     IN City VARCHAR(100),
  17.     IN Phone INT
  18.     )
  19. DECLARE regs        INT;
  20.     DECLARE ExistsUser   INT;
  21.     DECLARE Customer_ID INT;
  22.     SET regs = 0;
  23.     SELECT COUNT(*) INTO regs
  24.     FROM `ps_customer`
  25.     WHERE id_customer = 3;
  26.  
  27.     IF regs > 0 THEN
  28.         SET ExistsUser = 1;
  29.     END IF;
  30.  
  31.     IF ExistsUser = 1 THEN
  32.         INSERT INTO  `ps_customer`
  33.             (id_default_group,id_risk,firstname,lastname,email, passwd,birthday, newsletter_date_add,max_payment_days,secure_key,ac tive,date_add,date_upd)
  34.         VALUES(
  35.                3, 0, Firstname, Lastname, Email, '12345',Birthday,'0000-00-00 00:00:00',0,
  36.                '2293b4751c4ff4517bb997eb9c141a76',1,CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()
  37.               );
  38.  
  39.         SET Customer_ID = LAST_INSERT_ID();
  40.         IF Customer_ID > 0 THEN
  41.             INSERT INTO  `ps_customer_group` (id_customer, id_group)
  42.                 VALUES(Customer_ID, 3);
  43.             INSERT INTO  `ps_guest`(id_operating_system, id_web_browser, id_customer, screen_resolution_x, screen_resolution_y,
  44.  screen_color, sun_java, adobe_flash, adobe_director, apple_quicktime, real_player, windows_media, accept_language)
  45.                 VALUES(3, 10, Customer_ID, 0, 0, 0, 0, 0, 0, 0, 0, 0,'');
  46.             INSERT INTO  `ps_address`(id_country, id_state, id_customer, id_manufacturer, id _supplier, id_warehouse, alias, company, lastname, firstname, address1,
  47.  postcode, city, phone, vat_number, dni, DATE_ADD, date_upd, active, deleted)
  48.                 VALUES(145, 67, Customer_ID, 0, 0, 0, 'Business', Company, Lastname, Firstname, Address1, Postcode, City, Phone, '','' , CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 1, 0);
  49.         END IF;
  50.     END IF;
  51. //
  52.  
  53.  
  54. consulta SQL: Documentación
  55.  
  56. CREATE PROCEDURE sp_InsertCustomer ( IN Firstname VARCHAR(100), IN Lastname VARCHAR(100), IN Email VARCHAR(100), IN Birthday DATETIME, IN Company VARCHAR(100), IN Address1 VARCHAR(100), IN Postcode INT, IN City VARCHAR(100), IN Phone INT ) BEGIN DECLARE regs INT; DECLARE ExistsUser INT; DECLARE Customer_ID INT; SET regs = 0; SELECT COUNT(*) INTO regs FROM `ps_customer` WHERE id_customer = 3; IF regs > 0 THEN SET ExistsUser = 1; END IF; IF ExistsUser = 1 THEN INSERT INTO `ps_customer` (id_default_group,id_risk,firstname,lastname,email, passwd,birthday, newsletter_date_add,max_payment_days,secure_key,ac tive,date_add,date_upd) VALUES( 3, 0, Firstname, Lastname, Email, '12345',Birthday,'0000-00-00 00:00:00',0, '2293b4751c4ff4517bb997eb9c141a76',1,CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP() ); SET Customer_ID = LAST_INSERT_ID(); IF Customer_ID > 0 THEN INSERT INTO `ps_customer_group` (id_customer, id_group) VALUES(Customer_ID, 3); INSERT INTO `ps_guest`(id_operating_system, id_web_browser, id_customer, screen_resolution_x, screen_resolution_y, screen_color, sun_java, adobe_flash, adobe_director, apple_quicktime, real_player, windows_media, accept_language) VALUES(3, 10, Customer_ID, 0, 0, 0, 0, 0, 0, 0, 0, 0,''); INSERT INTO `ps_address`(id_country, id_state, id_customer, id_manufacturer, id _supplier, id_warehouse, alias, company, lastname, firstname, address1, postcode, city, phone, vat_number, dni, DATE_ADD, date_upd, active, deleted) VALUES(145, 67, Customer_ID, 0, 0, 0, 'Business', Company, Lastname, Firstname, Address1, Postcode, City, Phone, '','' , CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(), 1, 0); END IF; END IF; END //
  57.  
  58. MySQL ha dicho: Documentación
  59. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tive,date_add,date_upd)
  60.         VALUES(
  61.                3, 0, Firstname, Lastna' at line 28