Ver Mensaje Individual
  #6 (permalink)  
Antiguo 18/04/2012, 09:26
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: Sentencia update con if

¬¬ pues nada mas agregas estos campos que quieres actualizar a tu select algo asi:

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. id INT IDENTITY(1,1),
  4. nombre VARCHAR(10),
  5. date1 datetime,
  6. date2 datetime,
  7. STATUS bit
  8. )
  9.  
  10. INSERT INTO #temp VALUES ('libras',getdate(),getdate()-1,1)
  11. INSERT INTO #temp VALUES ('libras1',getdate(),getdate()-2,0)
  12. INSERT INTO #temp VALUES ('libras2',getdate(),getdate()-3,0)
  13. INSERT INTO #temp VALUES ('libras3',getdate(),getdate()-4,1)
  14. INSERT INTO #temp VALUES ('libras4',getdate(),getdate()-5,1)
  15.  
  16.  
  17. SELECT * FROM #temp
  18.  
  19. id  nombre  date1   date2   STATUS
  20. 1   libras  2012-04-18 11:22:18.853 2012-04-17 11:22:18.853 1
  21. 2   libras1 2012-04-18 11:22:18.853 2012-04-16 11:22:18.853 0
  22. 3   libras2 2012-04-18 11:22:18.853 2012-04-15 11:22:18.853 0
  23. 4   libras3 2012-04-18 11:22:18.853 2012-04-14 11:22:18.853 1
  24. 5   libras4 2012-04-18 11:22:18.853 2012-04-13 11:22:18.853 1
  25.  
  26. UPDATE #temp
  27. SET date1=t1.fecha,
  28.     nombre=t1.nombre,
  29.     STATUS=t1.STATUS
  30. FROM
  31. (
  32. SELECT id,CASE WHEN STATUS=1 THEN date2 ELSE date1 END AS fecha,'sin nombre' AS nombre,
  33. CASE WHEN STATUS=1 THEN 0 ELSE 1 END AS STATUS FROM #temp
  34. ) AS t1 WHERE t1.id=#temp.id
  35.  
  36. SELECT * FROM #temp
  37.  
  38. id  nombre  date1   date2   STATUS
  39. 1   sin nombre  2012-04-17 11:22:18.853 2012-04-17 11:22:18.853 0
  40. 2   sin nombre  2012-04-18 11:22:18.853 2012-04-16 11:22:18.853 1
  41. 3   sin nombre  2012-04-18 11:22:18.853 2012-04-15 11:22:18.853 1
  42. 4   sin nombre  2012-04-14 11:22:18.853 2012-04-14 11:22:18.853 0
  43. 5   sin nombre  2012-04-13 11:22:18.853 2012-04-13 11:22:18.853 0

El mismo query actualizando "muchos" datos
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me