jueves, 23 de noviembre de 2017

TOP EN CONSULTA SAP

SELECT
    FEBEP~BELNR
    PAGO~ZBUKR
    PAGO~UBNKS
    PAGO~UBNKL
    PAGO~ZBNKS
    PAGO~ZBNKL
    PAGO~ZBNKN
    PAGO~ZIBAN
    PAGO~RWBTR
    PAGO~WAERS
    PAGO~ZNME1
    PAGO~BKREF
    PAGO~ZSTRA
    PAGO~ZPSTL
    PAGO~ZORT1
    PAGO~ZLAND
*    PAGO~AUGBL
    PAGO~ZBUKR

  INTO  corresponding FIELDS OF TABLE ST_REGUH

   FROM  BSEG
   INNER JOIN febep
    ON febep~BELNR BSEG~BELNR
   INNER JOIN REGUH AS PAGO
    ON BSEG~ZUONR PAGO~VBLNR AND PAGO~ZBUKR p_bukrs
   INNER JOIN T001
    ON T001~BUKRS PAGO~ZBUKR  UP TO ROWS
   WHERE febep~kukey r_kukey AND
         febep~esnum r_esnum AND
         BSEG~BUKRS  p_bukrs AND
         BSEG~BUZEI  1.

viernes, 17 de noviembre de 2017

Stored Procedure SQL SERVER numero de registros por tabla y tamaño

En el siguiente sp se obtienen todas las tablas de la base de datos y datos como el numero de registos y peso de cada tabla.


create PROCEDURE [dbo].[GetAllTableSizes]
AS
/*
    Obtains spaced used data for ALL user tables in the database
*/
DECLARE @TableName VARCHAR(100)    --For storing values in the cursor

--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
 select schemas.name+'.'+tables.name as name from sys.tables
inner join sys.schemas  on tables.schema_id =schemas.schema_id
FOR READ ONLY




--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)

--Open the cursor
OPEN tableCursor

--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
    --Dump the results of the sp_spaceused query to the temp table
    INSERT  #TempTable
        EXEC sp_spaceused @TableName

    --Get the next table name
    FETCH NEXT FROM tableCursor INTO @TableName
END

--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor

--Select all records so we can use the reults
SELECT *
FROM #TempTable order by 2

--Final cleanup!

DROP TABLE #TempTable

jueves, 9 de noviembre de 2017

Agregar validación de lista a celda de excel desde lista de SQL ADDIN VSTO

Validation.Add Method (Excel) add-in VSTO
Agregar lista de validación a Excel desde base de datos
Agrega validación de datos al rango especificado

   string Clientes = string.Empty;//"[Seleccione un Cliente...]"; 
            ClienteIncortemList oClienteIncortemList = new ClienteIncortemList();//obteto tipo lista de clientes
            ConfigurationOperation.GetClienteIncortemByAll(oClienteIncortemList);//metodo que trae a clientes de base de datos
            foreach (ClienteIncortem oClienteIncortem in oClienteIncortemList.lstClienteIncortem) //itera la lista para formar una cadena separada por comas
            {
                Clientes += Clientes == string.Empty ? "" : ",";
                Clientes += oClienteIncortem.cvCliente + " - " + oClienteIncortem.nbCliente.Replace(',', ' ');
            }


            wrksh.get_Range("B11:B11", System.Type.Missing).Validation.Add(XlDVType.xlValidateList, System.Type.Missing, XlFormatConditionOperator.xlBetween, Clientes, System.Type.Missing); // VALDACION DE LISTA

domingo, 5 de noviembre de 2017

Función SQL para determinar fecha con Días festivos y Domingos

La función recibe una fecha inicial, y un numero de días los cuales se les va sumar a la fecha inicial, para calcular los días festivos se ocupa una tabla configuration.diasfestivos, si les es de utilidad la tabla comenten para que les pase el script



CREATE FUNCTION dbo.[CalcularFechaConDiasFestivosYDomingos]
(
@Fecha datetime,
@noDias int
)
RETURNS datetime
as
begin
declare @resFecha datetime
declare @FeFin datetime
declare @result int
declare @DiasFeriados int
declare @noDomingos int
declare @Correcto  int
--set @ini = '2016-09-01'
--set @fin = '2016-09-30'
set @FeFin =DATEADD(DAY,@noDias,@Fecha)
set @noDomingos =( Select (datediff(day,@Fecha,@FeFin)-DATEPART(dw,@FeFin)+8)/7)
--select * from configuration.diasfestivos
set @DiasFeriados =( select COUNT(1) FROM configuration.diasfestivos WHERE fedia >= @Fecha AND fedia <= @FeFin)
set @Correcto =0
set @result = @noDomingos+@DiasFeriados
set @resFecha = dateadd(DAY,@result,@FeFin)
/*WHILE (@varfecha<>(@FechaFinal + 1)) */

WHILE (@Correcto =0)
BEGIN
IF (DATEPART(dw,@resFecha) IN (1))
       BEGIN
             SET @resFecha =DATEADD(dd,1,@resFecha)
             SET @Correcto =0
       END
ELSE
       begin
             IF (select COUNT(1) FROM configuration.diasfestivos WHERE fedia = @resFecha)=1
                    BEGIN
                           SET @resFecha =DATEADD(dd,1,@resFecha)
                           SET @Correcto =0
                    END
             ELSE
                    BEGIN
                           SET @Correcto =1
                    END         
       end
END


return @resFecha

end
---------------------------------


Script para creación de tabla días festivos.


CREATE SCHEMA [Configuration]
GO
/****** Object:  Table [Configuration].[DiasFestivos]    Script Date: 21/05/2018 04:29:30 p. m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Configuration].[DiasFestivos](
       [idDiaFestivo] [int] IDENTITY(1,1) NOT NULL,
       [dsDiaFestivo] [nvarchar](100) NULL,
       [feDia] [datetime] NULL,
       [esActivo] [bit] NULL,
 CONSTRAINT [PK_DIASFESTIVOS] PRIMARY KEY CLUSTERED
(
       [idDiaFestivo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [Configuration].[DiasFestivos] ADD  DEFAULT ((0)) FOR [esActivo]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Dias Festivos ' , @level0type=N'SCHEMA',@level0name=N'Configuration', @level1type=N'TABLE',@level1name=N'DiasFestivos'
GO