Buenas tardes,
Les escribo ya que requiero ayuda con el siguiente query, pues estamos migrando de Sql a Hana y en el momento en que me encontré este query para migrarlo no me funciona, según entiendo HANA no permite crear tablas temporales, la verdad aquí si quede 100% perdido, alguien podría ayudarme con la solución de como podría migrar este query de SQL a HANA, de antemano agradezco la colaboración de cada uno de ustedes, aquí dejo el query
/*SELECT FROM [dbo].[OWHS] TT1*/
declare @BodegaI as varchar (8)
/* WHERE */
set @BodegaI = /* TT1.WhsCode */'[%0]'
/*SELECT FROM [dbo].[OWHS] TT1*/
declare @BodegaF as varchar (8)
/* WHERE */
set @BodegaF = /* TT1.WhsCode */'[%1]'
/*SELECT FROM [dbo].[OITB] TT2*/
declare @GrupoI as varchar (20)
/* WHERE */
set @GrupoI = /* TT2.[ItmsGrpNam] */'[%2]'
/*SELECT FROM [dbo].[OITB] TT2*/
declare @GrupoF as varchar (20)
/* WHERE */
set @GrupoF = /* TT2.[ItmsGrpNam] */'[%3]'
CREATE TABLE [dbo].[#Template_Entrada_Detalle1](
[Num_line] [int] IDENTITY (1,1) NOT NULL,
[DocNum] [int] NOT NULL,
[LineNum] [int] NOT NULL,
[ItemCode] [nvarchar] (20) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[Dscription] [nvarchar] (100) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[Quantity] [numeric](19, 6) NOT NULL,
[Price] [numeric](19, 6) NOT NULL,
[Currency] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[WhsCode] [nvarchar] (8) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[AcctCode] [nvarchar] (15) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[OcrCode] [nvarchar] (8) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[TaxStatus] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[DeferrTax] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[LineTotal] [numeric](19, 6) NOT NULL
)
CREATE TABLE [dbo].[#Template_Entrada_Detalle2](
[DocNum] [int] NOT NULL,
[LineNum] [int] NOT NULL,
[ItemCode] [nvarchar] (20) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[Dscription] [nvarchar] (100) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[Quantity] [numeric](19, 6) NOT NULL,
[Price] [numeric](19, 6) NOT NULL,
[Currency] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[WhsCode] [nvarchar] (8) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[AcctCode] [nvarchar] (15) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[OcrCode] [nvarchar] (8) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[TaxStatus] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[DeferrTax] [nvarchar] (3) COLLATE SQL_Latin1_General_CP850_CI_AS NOT NULL,
[LineTotal] [numeric](19, 6) NOT NULL
)
insert into #Template_Entrada_Detalle1
SELECT 1 'DocNum', 0 'LineNum', T0.[Articulo] 'ItemCode', T0.Descripcion 'Dscription', SUM(T0.Diferencia_Cantidad) 'Quantity',
SUM(T0.Diferencia_Costo) 'Price',
'$' 'Currency', T0.Bodega 'WhsCode', '73959503' 'AcctCode', '601099' 'OcrCode',
'tNO' 'TaxStatus', 'tNO' 'DeferrTax', SUM(T0.Diferencia_Costo) 'LineTotal'
FROM
(
SELECT T0.Bodega, T0.Articulo, T0.Descripcion, T0.Lote, T0.Costo_Unitario, T0.Diferencia_Cantidad, T0.Diferencia_Costo
FROM
-- Tomado del Informe de Diferencias---
(
SELECT T0.Bodega 'Bodega', T0.Articulo 'Articulo', T0.Descripcion 'Descripcion', ISNULL(T0.Lote,'') 'Lote',
T2.AvgPrice 'Costo_Unitario',
SUM(T0.Existencia) 'Existencia_Cantidad', SUM(T0.Existencia * T2.AvgPrice) 'Existencia_Costo',
SUM(T0.Fisico) 'Físico_Cantidad', SUM(T0.Fisico * T2.AvgPrice) 'Físico_Costo',
SUM(T0.Fisico) - SUM(T0.Existencia) 'Diferencia_Cantidad', (SUM(T0.Fisico) - SUM(T0.Existencia)) * T2.AvgPrice 'Diferencia_Costo'
FROM
(
SELECT T0.[WhsCode] 'Bodega', T0.[ItemCode] 'Articulo', T2.[ItemName] 'Descripcion', ISNULL(T1.[BatchNum],'') 'Lote',
CASE
WHEN ISNULL(T1.[BatchNum],'') = '' THEN T0.OnHand
ELSE T1.Quantity
END 'Existencia', 0 'Fisico'
FROM [dbo].[OITW] T0 LEFT JOIN [dbo].[OIBT] T1 ON T1.WhsCode = T0.WhsCode AND T1.ItemCode = T0.ItemCode
INNER JOIN [dbo].[OITM] T2 ON T0.ItemCode = T2.ItemCode
INNER JOIN OITB T3 ON T3.ItmsGrpCod = T2.ItmsGrpCod
WHERE ( T0.[WhsCode] >= @BodegaI AND T0.WhsCode <= @BodegaF ) AND
( T3.ItmsGrpNam >= @GrupoI AND T3.ItmsGrpNam <= @GrupoF )
UNION ALL
SELECT T0.[U_CodAlm] 'Bodega', T0.[U_CodArt] 'Articulo', T1.ItemName 'Descripcion', ISNULL(T0.[U_NroLote],'') 'Lote',
0 'Existencia', SUM(T0.[U_Cantidad]) 'Fisico'
FROM [dbo].[@HBT_FISICO] T0 LEFT JOIN OITM T1 ON T1.ItemCode = T0.U_CodArt
INNER JOIN OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
WHERE ( T0.[U_CodAlm] >= @BodegaI AND T0.[U_CodAlm] <= @BodegaF ) AND
( T2.ItmsGrpNam >= @GrupoI AND T2.ItmsGrpNam <= @GrupoF )
GROUP BY T0.[U_CodAlm], T0.[U_CodArt], T1.ItemName, ISNULL(T0.[U_NroLote],'')
) T0
INNER JOIN OITW T2 ON T2.WhsCode = T0.Bodega AND T2.ItemCode = T0.Articulo
GROUP BY T0.Bodega, T0.Articulo, T0.Descripcion, ISNULL(T0.Lote,''), T2.AvgPrice
HAVING (SUM(T0.Existencia) <> 0) OR (ISNULL(SUM(T0.Fisico),0) <> 0)
) T0
-- Fin Informe de Diferencias-----------------------------------------------------------------------------------------------------------------------
WHERE T0.Diferencia_Cantidad > 0
) T0
GROUP BY T0.Articulo, T0.Descripcion, T0.Bodega
insert into #Template_Entrada_Detalle2
SELECT T0.DocNum 'DocNum', T0.Num_Line - 1 'LineNum', T0.ItemCode 'ItemCode', T0.Dscription 'Dscription', T0.Quantity 'Quantity',
T0.Price 'Price',
T0.Currency 'Currency',
T0.WhsCode 'WhsCode', T0.AcctCode 'AcctCode', T0.OcrCode 'OcrCode', T0.TaxStatus 'TaxStatus', T0.DeferrTax 'DeferrTax', T0.LineTotal 'LineTotal'
FROM #Template_Entrada_Detalle1 T0
ORDER BY T0.[WhsCode], T0.[ItemCode]
SELECT T0.DocNum 'ParentKey', CONVERT(VARCHAR(10),T0.LineNum) 'LineNum', T0.ItemCode, T0.Dscription 'ItemDescription', T0.Quantity 'Quantity', T0.Price, T0.Currency,
T0.WhsCode 'WarehouseCode', T0.AcctCode 'AccountCode', T0.OcrCode 'CostingCode',
T0.TaxStatus 'TaxLiable', T0.DeferrTax 'DeferredTax', T0.LineTotal 'LineTotal'
FROM #Template_Entrada_Detalle2 T0
ORDER BY T0.DocNum, T0.[ItemCode]
--ORDER BY T0.DocNum, T0.LineNum
Este es el fin del query