使用临时表创建VIEW

时间:2017-06-15 18:36:57

标签: sql tsql

所以我的任务是每周将数据集导出到excel文件。我试图创建一个视图,以便我可以利用SSIS导出文件,但是因为我没有找到从下面的查询创建视图的方法,因为它使用的是临时表。任何建议都会受到很多关注。

请注意,运行下面的单独工作正常并返回所需的正确结果集。就在我尝试将其包装在创建视图中时,它会产生问题。

CREATE VIEW OFRESULTS

AS

CREATE TABLE #t1
(
ID INT IDENTITY(1, 1) Primary Key
,IncNum nvarchar (255)
,ParentIncNum nvarchar (255)
,InAbbr nvarchar (255)
,SpecificInc nvarchar (255)
,CCSection nvarchar (255)
,PCode nvarchar (255)
,IncTime nvarchar (255)
,IncFac nvarchar (255)
,Location nvarchar (255)
,IncDate nvarchar (255)
,Program nvarchar (255)
,Extra nvarchar (255)
,Fac nvarchar (255)
,IncVoid nvarchar (255)
)

INSERT Into #t1 SELECT a.IncNum, 
CASE WHEN RIGHT(a.IncNum, 2) like '[a-z][0-9]%' THEN LEFT(a.IncNum, 
LEN(a.IncNum) - 2) ELSE a.IncNum END As ParentID, a.IncAbbr,
COALESCE(a.IncSpc, l.IncSpc),
a.IncVio,
a.IncPC,
CONVERT(varchar(8), a.IncTime, 108) as IncTime,
a.IncSite,
a.IncLoc,
CONVERT(varchar(10), a.IncDate, 101) as IncDate,
a.IncProg,
CASE WHEN ex.IncExtCalc = 1 Then 'Controlled'
     WHEN ex.IncExtEme = 1 Then 'Immediate'
     WHEN ex.IncExtCalc = 1 and ex.IncExtEme = 1 Then 'Controlled and Immediate'
     ELSE ' '
END AS [Extr],
CASE WHEN a.IncFa = 1 Then 'Lvl I'
     WHEN     a.IncFac2 = 1 Then 'Lvl II'
     WHEN     a.IncFac3 = 1 Then 'Lvl III'
     WHEN     a.IncFac4 = 1 Then 'Lvl IV'
END AS [FacLevel],
l.IncVoid
  FROM [DBSQL].[dbo].[tblA] as a full outer join [DBSQL].[dbo].[tblLog] as l
  on a.IncNum = l.IncNum
  full outer join [DBSQL].[dbo].[tblEXTR] as ex on a.IncNum = ex.IncNum
 WHERE 
  a.IncDate >= DATEADD(day, -21, Getdate()) and a.IncDate <= DATEADD(day, -7, GetDate()) and l.IncVoid = 0
  or
  a.IncDate >= DATEADD(day, -21, Getdate()) and a.IncDate <= DATEADD(day, -7, GetDate()) and l.IncVoid is null



SELECT tt.*
FROM #t1 tt
INNER JOIN
    (
    SELECT ParentIRNum, MAX(ID) AS MaxID
    FROM #t1
    GROUP BY ParentIRNum
    ) groupedtt ON tt.ParentIRNum = groupedtt.ParentIRNum  AND tt.ID = groupedtt.MaxID
       ORDER BY ID 


DROP Table #t1

3 个答案:

答案 0 :(得分:1)

我看到这个问题已经得到解答,但我想我还要加10美分。 有问题的查询不需要临时表,可以使用CTE重写,这将使其与View兼容,如下例所示:

CREATE VIEW MyView 
AS
WITH CTE_T1
AS
(
    SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT 1 AS [Order])) as ID 
            ,a.IncNum 
            ,(
                CASE 
                    WHEN RIGHT(a.IncNum, 2) like '[a-z][0-9]%' THEN LEFT(a.IncNum, LEN(a.IncNum) - 2) 
                    ELSE a.IncNum 
                END
            )                                       AS ParentIncNum
            ,a.IncAbbr                              AS InAbbr
            ,COALESCE(a.IncSpc, l.IncSpc)           AS SpecificInc
            ,a.IncVio                               AS CCSection
            ,a.IncPC                                AS PCode
            ,CONVERT(varchar(8), a.IncTime, 108)    AS IncTime
            ,a.IncSite                              AS IncFac
            ,a.IncLoc                               AS [Location]
            ,CONVERT(varchar(10), a.IncDate, 101)   AS IncDate
            ,a.IncProg                              AS [Program]
            ,(
                CASE WHEN ex.IncExtCalc = 1 Then 'Controlled'
                    WHEN ex.IncExtEme = 1 Then 'Immediate'
                    WHEN ex.IncExtCalc = 1 and ex.IncExtEme = 1 Then 'Controlled and Immediate'
                    ELSE ' '
                END 
            )                                       AS [Extra]
            ,(
                CASE WHEN a.IncFa = 1 Then 'Lvl I'
                    WHEN     a.IncFac2 = 1 Then 'Lvl II'
                    WHEN     a.IncFac3 = 1 Then 'Lvl III'
                    WHEN     a.IncFac4 = 1 Then 'Lvl IV'
                END 
            )                                       AS [Fac]
            ,l.IncVoid                              AS IncVoid
    FROM [DBSQL].[dbo].[tblA] as a full outer join [DBSQL].[dbo].[tblLog] as l
        on a.IncNum = l.IncNum
    FULL OUTER JOIN [DBSQL].[dbo].[tblEXTR] as ex on a.IncNum = ex.IncNum
    WHERE 
      a.IncDate >= DATEADD(day, -21, Getdate()) and a.IncDate <= DATEADD(day, -7, GetDate()) and l.IncVoid = 0
      or
      a.IncDate >= DATEADD(day, -21, Getdate()) and a.IncDate <= DATEADD(day, -7, GetDate()) and l.IncVoid is null

), CTE_GroupedTT
AS
(
    SELECT  ParentIncNum
            ,MAX(ID) AS MaxID
    FROM CTE_T1
    GROUP BY ParentIncNum
)
SELECT      TOP 100 PERCENT
            tt.*
FROM        CTE_T1 tt
INNER JOIN  CTE_GroupedTT groupedtt ON tt.ParentIncNum = groupedtt.ParentIncNum  AND tt.ID = groupedtt.MaxID
ORDER BY    ID

在上面的例子中,CTE_T1执行与临时表#t1相同的功能。

答案 1 :(得分:0)

我认为您可以使用存储过程或表值函数与内存表(声明@ t1表...)。

或者您可以在没有SSIS的情况下运行相同的脚本,并使用bcp或openrowset将数据写入文件。

答案 2 :(得分:0)

如果您绝对需要使用临时表,可以将其放在存储过程中,然后调用/执行它。像下面这样的东西可能有效。

 CREATE PROCEDURE dbo.UsingTempTables
 AS 
 BEGIN
 SET NOCOUNT ON;

CREATE TABLE #TempTable(ColumnA INT, ColumnB Varchar(50));

INSERT INTO #TempTable(ColumnA,ColumnB) VALUES(5,'ABC');
INSERT INTO #TempTable(ColumnA,ColumnB) VALUES(6,'DEF');

SELECT ColOne = ColumnA
    ,  ColTwo = ColumnB
    FROM #TempTable
 END;

然后在SSIS中的execute sql任务中使用类似下面的内容:

 EXEC('EXEC dbo.UsingTempTables') 
 WITH RESULT SETS
 ( ( ColOne INT, ColTwo VARCHAR(50)) )

仅供参考:查看以下链接以获取更多参考(上述答案基于此)https://www.sqlservercentral.com/Forums/Topic1574990-364-1.aspx