Is it possible to create a temporary table in a View and drop it after select?

后端 未结 4 844
挽巷
挽巷 2020-12-28 11:44

I need to alter one view and I want to introduce 2 temporary table before the SELECT.

Is this possible? And how can I do it?

ALTER VIEW myView
AS 

S         


        
4条回答
  •  心在旅途
    2020-12-28 12:26

    Not possible but if you try CTE, this would be the code:

    ALTER VIEW [dbo].[VW_PuntosDeControlDeExpediente]
    AS
        WITH TEMP (RefLocal, IdPuntoControl, Descripcion) 
        AS 
        (
            SELECT 
                  EX.RefLocal
                , PV.IdPuntoControl
                , PV.Descripcion
            FROM [dbo].[PuntosDeControl] AS PV
            INNER JOIN [dbo].[Vertidos] AS VR ON VR.IdVertido = PV.IdVertido
            INNER JOIN [dbo].[ExpedientesMF] AS MF ON MF.IdExpedienteMF = VR.IdExpedienteMF
            INNER JOIN [dbo].[Expedientes] AS EX ON EX.IdExpediente = MF.IdExpediente
        )
        SELECT 
              Q1.[RefLocal]
            ,    [IdPuntoControl] = ( SELECT MAX(IdPuntoControl) FROM TEMP WHERE [RefLocal] = Q1.[RefLocal] AND [Descripcion] = Q1.[Descripcion] )
            , Q1.[Descripcion]
        FROM TEMP AS Q1
        GROUP BY Q1.[RefLocal], Q1.[Descripcion]
    GO
    

提交回复
热议问题