Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Exportar datos masivamente a un archivo csv

Estas en el tema de Exportar datos masivamente a un archivo csv en el foro de SQL Server en Foros del Web. Hola!! Estoy tratando de hacer un job para que exporte datos de una consulta automaticamente.. para subirlos a un ftp... pero tengo problema al querer ...
  #1 (permalink)  
Antiguo 08/07/2011, 09:29
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Exportar datos masivamente a un archivo csv

Hola!!

Estoy tratando de hacer un job para que exporte datos de una consulta automaticamente.. para subirlos a un ftp... pero tengo problema al querer exportar... este es mi código
:


Código SQL:
Ver original
  1. bcp "select a.Clave,a.texto1,a.descripcion,a.descripcion2,a.Modelo,a.Numero2,a.Moneda,
  2.     Sum(B.OrdenadaVenta) oRDENADOVENTA, Sum(C.Existencia) AS Existencia
  3.         from articulo a, ArticuloSaldoAlmacen B, ArticuloExistencia C
  4.             where A.Clave = B.Articulo AND A.CLAVE=C.ARTICULO AND B.ARTICULO=C.ARTICULO AND A.CLAVE='6160624'
  5.         GROUP BY  a.Clave,a.texto1,a.descripcion,a.descripcion2,a.Modelo,a.Numero2,a.Moneda"
  6. queryout a.txt -c -T

Pero me da este error:

Msg 103, Level 15, State 4, Line 1
The identifier that starts with 'select a.Clave,a.texto1,a.descripcion,a.descripcion2,a.Mo delo,a.Numero2,a.Moneda,
Sum(B.OrdenadaVenta) oRDENADOVENTA, Sum(C.E' is too long. Maximum length is 128.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'queryout'.


Espero me puedan ayudar con este error.

Saludos

  #2 (permalink)  
Antiguo 08/07/2011, 09:40
Avatar de 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: Exportar datos masivamente a un archivo csv

ya probaste a poner todo tu query en una sola linea??

Saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #3 (permalink)  
Antiguo 08/07/2011, 09:51
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Respuesta: Exportar datos masivamente a un archivo csv

Sii y sigue =

Código SQL:
Ver original
  1. bcp "select a.Clave,a.texto1,a.descripcion,a.descripcion2,a.Modelo,a.Numero2,a.Moneda,Sum(B.OrdenadaVenta) oRDENADOVENTA, Sum(C.Existencia) AS Existencia       from P5ASC.P5ASC.articulo a, P5ASC.P5ASC.ArticuloSaldoAlmacen B, P5ASC.P5ASC.ArticuloExistencia C where A.Clave = B.Articulo AND A.CLAVE=C.ARTICULO AND B.ARTICULO=C.ARTICULO AND A.CLAVE='6160624'GROUP BY  a.Clave,a.texto1,a.descripcion,a.descripcion2,a.Modelo,a.Numero2,a.Moneda" queryout a.txt -c -T

=(¿¿cómo puedo exportar autómaticamente los datos??
  #4 (permalink)  
Antiguo 08/07/2011, 10:52
Avatar de 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: Exportar datos masivamente a un archivo csv

ya probaste ejecutando esa consulta desde consola(ms-dos)?? Puedes exportarlos automaticamente con un schedule task de windows y un bat que ejecute tu query

Saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #5 (permalink)  
Antiguo 08/07/2011, 11:12
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Respuesta: Exportar datos masivamente a un archivo csv

No la verdad nunca he ejecutado comandos desde ms-dos para ejecutar un query... tienes info para leer??.. d todos modos lo voy a buscar en san google
  #6 (permalink)  
Antiguo 08/07/2011, 11:44
Avatar de 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: Exportar datos masivamente a un archivo csv

Nada mas executa tu bcp desde modo consola (cmd, ms-dos, promth)

bcp "query" queryout archivo -f archivo_de_formato -S server -U user -P Password

http://msdn.microsoft.com/en-us/libr...sql.80%29.aspx

Saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #7 (permalink)  
Antiguo 08/07/2011, 12:12
Avatar de iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 16 años, 9 meses
Puntos: 180
Respuesta: Exportar datos masivamente a un archivo csv

¿No te hara falta un AS en esta parte?

Sum(B.OrdenadaVenta) oRDENADOVENTA

Debiendo ser:

Sum(B.OrdenadaVenta) AS oRDENADOVENTA

El error en si, se refiere al NOMBRE de las columnas, objetos, etc. que no deben ser mayores a 128 caracteres.

¿Acaso no sera que deberias ejecutar toda tu cadena con el store SP_CMDSHELL, ya que estas invocando a BCP.EXE que es un comando a nivel OS?

Por otro lado, ¿porque no utilizas los JOIN en lugar de ligar tus tablas por el WHERE?
__________________
MCTS Isaias Islas
  #8 (permalink)  
Antiguo 11/07/2011, 11:31
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Respuesta: Exportar datos masivamente a un archivo csv

Desde MS-Dos no puedo exportar ya que me marca un error al querer realizar la consulta.. de que manera puedo usar un bulk export o algo así?? para exportar los datos a un archivo CSV????? Por favor ayudenme :(
  #9 (permalink)  
Antiguo 11/07/2011, 11:53
Avatar de iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 16 años, 9 meses
Puntos: 180
Respuesta: Exportar datos masivamente a un archivo csv

Asi como lo indica LIBRAS:

bcp "query" queryout archivo -f archivo_de_formato -S server -U user -P Password
__________________
MCTS Isaias Islas
  #10 (permalink)  
Antiguo 11/07/2011, 12:02
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Respuesta: Exportar datos masivamente a un archivo csv

Ya lo he intentado así:

Código SQL:
Ver original
  1. EXEC Master..xp_cmdshell 'bcp "select * from #CyberPuertaPrecio" queryout "C:\Documents and Settings\tlemus\Escritorio\a.csv" -U sa -P interconexion -c'

Y también así:

Código SQL:
Ver original
  1. EXEC Master..xp_cmdshell 'bcp "select PreciosCyberPuerta.Clave as NoArtMayorista, PreciosCyberPuerta.NoParte as NoArtFabricante,PreciosCyberPuerta.Descripcion as NombreProducto, PreciosCyberPuerta.Descripcion2 as DescripProducto,PreciosCyberPuerta.Modelo as MarcaProducto, PreciosCyberPuerta.Precio2 as PrecioVenta,PreciosCyberPuerta.Moneda as Moneda,sum(t1.existencia-t2.ordenado) as DisponibilidadTotal, sum(PreciosCyberPuerta.Mnty-PreciosCyberPuerta.OrdMnty) as DispMty,sum(PreciosCyberPuerta.Gdl-PreciosCyberPuerta.OrdGdl) as DispGdl,sum(PreciosCyberPuerta.Mexico-PreciosCyberPuerta.OrdMex) as DispMexfrom (select clave , sum (mnty+gdl+mexico) as existencia from #CyberPuertaPrecio group by clave) as T1 ,   (select clave, sum (OrdMnty+OrdGdl+OrdMex) as ordenado from #CyberPuertaPrecio group by clave) as T2 ,  #CyberPuertaPrecio as PreciosCyberPuerta where t1.clave=t2.clave and PreciosCyberPuerta.clave=t1.clave and t2.clavE=PreciosCyberPuerta.clave    group by PreciosCyberPuerta.Clave, PreciosCyberPuerta.NoParte, PreciosCyberPuerta.Descripcion,  PreciosCyberPuerta.Descripcion2, PreciosCyberPuerta.Modelo, PreciosCyberPuerta.Precio2, PreciosCyberPuerta.Moneda" queryout "C:\Documents and Settings\tlemus\Escritorio\a.csv" -U sa -P interconexion -c'

Pero cualquiera de las dos formas marca este error.

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.


Ya intente ejecutar...

Código SQL:
Ver original
  1. sp_configure 'Web Assistant Procedures', 1;
  2. GO
  3. RECONFIGURE
  4. GO

El resultado :

Msg 15123, Level 16, State 1, Procedure sp_configure, Line 51
The configuration option 'Web Assistant Procedures' does not exist, or it may be an advanced option.
  #11 (permalink)  
Antiguo 11/07/2011, 12:06
Avatar de iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 16 años, 9 meses
Puntos: 180
Respuesta: Exportar datos masivamente a un archivo csv

No, debes activar o setear en ON la ejecucion del store XP_CMDSHELL
__________________
MCTS Isaias Islas
  #12 (permalink)  
Antiguo 11/07/2011, 12:11
Avatar de iislas
Colaborador
 
Fecha de Ingreso: julio-2007
Ubicación: Mexico, D.F.
Mensajes: 6.482
Antigüedad: 16 años, 9 meses
Puntos: 180
Respuesta: Exportar datos masivamente a un archivo csv

Una liga para saber mas sobre la EXPORTACION via BCP hacia archivos CSV

http://www.simple-talk.com/sql/datab...ed-procedures/
__________________
MCTS Isaias Islas
  #13 (permalink)  
Antiguo 12/07/2011, 08:11
Avatar de Inicia  
Fecha de Ingreso: octubre-2010
Ubicación: Actualmente en GDL
Mensajes: 135
Antigüedad: 13 años, 6 meses
Puntos: 1
Respuesta: Exportar datos masivamente a un archivo csv

Muchas gracias, me fue de gran ayuda leer toda esa información..

Pude exportar con este

Código SQL:
Ver original
  1. EXEC Master..xp_cmdshell 'bcp "select * from P5ascPrueba..PreciosCyberPuerta" queryout "C:\prueba\a.csv" -T -c'

Ahora bien... cuando tengo el formato.. según yo un formato csv te delimitaba por comas y no lo esta haciendo :( ¿¿que puedo hacer para obtenerlo separado los campos por comas??
  #14 (permalink)  
Antiguo 12/07/2011, 08:51
Avatar de 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: Exportar datos masivamente a un archivo csv

Tienes que ponerle un archivo de formato o en los delimitadores especificar cual sera tu separador

http://www.nigelrivett.net/SQLTsql/B...rmat_file.html


Saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me

Etiquetas: csv
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:25.