viernes, 11 de enero de 2019

Leer Excel desde SQL Server



descargar Access Database Engine X64.exe

https://www.microsoft.com/en-us/download/details.aspx?id=13255


instalarlo dentro del server con el siguiente parámetro   /passive

C:\Users\desacop>C:\DW\AccessDatabaseEngine_X64.exe /passive




sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO

RECONFIGURE;

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters',



ejecutar:







DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp "My Select Query Here" queryout "My Excel File Path Here" -U linkuser -P linkuser01 -c'
Exec xp_cmdshell @cmd

para ver posibles errores














The setting can be applied as follows
Open SQL Server Configuration Manager -> Services -> SQLServer service.
Right click and choose properties.
Go to advanced tab and append -g512; to startup parameters property and it will resolve










SELECT * INTO Data_dq





FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0; Database=C:\BASURA\reporte.xls', [R$]);
GO


SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\BASURA\reporte.xls;Extended Properties=Excel 8.0')...[R]


SELECT * INTO XLImport4 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=E:\OOlguinRemate@gmail.com\PROYECTOS\11.- DESARROLLOS\Excel a SQL\reporte.xls', [R$])



SELECT * INTO XLImport5 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=E:\OOlguinRemate@gmail.com\PROYECTOS\11.- DESARROLLOS\Excel a SQL\reporte.xls', 'SELECT * FROM [R$]')


martes, 17 de abril de 2018

Caracter de escape


Cómo se implementa un caracter de escape en una consulta con filtro
-- Los corchetes son caracteres especiales, por eso hay que omitirlos


      ,[ColumnName]
      ,…
  FROM [DatabaseName].[dbo].[TableName]
  WHERE TextData like '%![GroupBy1!]%' ESCAPE '!'



viernes, 2 de marzo de 2018

Valor recursivo tipo factorial


sin la necesidad de realizar un while



DECLARE @Valor DECIMAL (18,8)
SET @Valor = 1

 SELECT @Valor = ( 1 + (RateValue/36000)) * @Valor
         FROM [REMATE2G].[Middle].[RateValues]
  WHERE IdRateName = 24
 AND RateDate BETWEEN '2018-02-15' AND '2018-02-28'

 SET @Valor = ((@Valor-1)*36000)/14
 SELECT @VALOR

lunes, 12 de febrero de 2018

SUBIR ARCHIVOS PLANOS A SQL SERVER

A continuación se coloca un ejemplo de cómo subir un archivo plano de texto a una tabla de SQL





















OPCIÓN 1:




CREA LA TABLA Y METE EN DIRECTO LA INFORMACIÓN DEL CONTENIDO SIN SEPARAR COLUMNAS

CREATE TABLE Staging.temp2 (contenido varchar(max))


BULK INSERT Staging.temp2
FROM 'C:\PASO\NombreArchivo.log'
  WITH
  (
    FIELDTERMINATOR = '\r',
    ROWTERMINATOR = '\n'
  );












OPCIÓN 2


SELECT * FROM OPENROWSET (
                           BULK  'C:\PASO\archivo.log'
                           ,SINGLE_CLOB                                             
                         ) AS X



Mete todo el contenido en un solo registro















OPCIÓN 3

Se tiene la ventaja de Hacer un SELECT 






SELECT * FROM OPENROWSET (
                          BULK  'C:\PASO\NombreArchivo.log',
                          FORMATFILE = 'C:\PASO\FormatoImportV2.fmt'                       
                         ) AS X
RESULTADO






















CONTENIDO DEL ARCHIVO DE FORMATO .fmt


12.0
4
1 SQLCHAR 0 50 " "       1 Fecha     Latin1_General_CI_AS
2 SQLCHAR 0 50 " "        2 Hora     Latin1_General_CI_AS
3 SQLCHAR 0 50 "]"        3 Validacion Latin1_General_CI_AS
4 SQLCHAR 0 50 "\r\n"      4 Comando    Latin1_General_CI_AS








miércoles, 7 de febrero de 2018

Símbolos SQL-T

© § ×

--                                                                                            PRUEBA
Alt + 0153.. ™… trademark symbol                    ->
Alt + 0169. ©…. copyright symbol                    -> ©
Alt + 0174.. ®….registered ­ trademark symbol        ->
Alt + 0177 …±….plus-or ­-minus sign                   -> ±
Alt + 0215 .×…..multi­plication sign                 -> ×
Alt + 1………..smiley face                            ->
Alt + 13……♪…..e­ighth note                            ->
Alt + 14……♫…… ­beamed eighth note                     ->
Alt + 8721. . N-ary summation (auto sum)         -> 
Alt + 251.. ..square root check mark              ->
Alt + 8236.. .. ­infinity                          ->
Alt + 24…….↑….. ­up arrow                                          ->
Alt + 25……↓…… ­down arrow                                          ->
Alt + 26..→…..r­ght arrow                                         ->
Alt + 27……←…..l­eft arrow                                          ->
Alt + 18..↕……u­p/down arrow                                       ->

Alt + 29……↔…lef­t right arrow                                      ->

DE: