SQL存储过程从具有许多左连接的选择中插入表

时间:2015-02-21 15:25:57

标签: sql-server stored-procedures join insert

我在初始选择时遇到错误。我认为有一些括号(括号中插入的值)

CREATE PROCEDURE [dbo].[zMDWhp1]
--HP1!
as
BEGIN

declare @dte datetime
select dte = 
(select 
case 
when datepart(weekday, getdate()) = 1 then dateadd(dd,-9, convert(date,getdate()))
else dateadd(dd,-(datepart(weekday, Convert(date,getdate()))+1), convert(date, getdate())) 
end)


insert into mdwall 
values
(
select 
a.doc, /*@dte,*/ 'HP1' as [Type], isnull([0-3],0) as [0-3], isnull([4-10],0) as [4-10], isnull([11-29],0) as [11-29], isnull([30-59],0) as [30-59], isnull([60-119],0) as [60-119], isnull([120+],0) as [120+]
FROM

(
select distinct doc
from mdwfile) 
a 

LEFT JOIN

(
Select 
Doc,  
count(*) as '0-3'
from daycountview 
where day_count='0-3' and unit like '%HP1'
group by doc) 
b on a.doc=b.doc

LEFT JOIN

(
Select 
Doc,  
count(*) as '4-10'
from daycountview
where day_count='4-10' and unit like '%HP1'
group by doc) 
c on a.doc=c.doc

LEFT JOIN

(
Select 
Doc,  
count(*) as '11-29'
from daycountview
where day_count='11-29' and unit like '%HP1'
group by doc) 
d on a.doc=d.doc

LEFT JOIN

(
Select 
Doc,  
count(*) as '30-59'
from daycountview
where day_count='30-59' and unit like '%HP1'
group by doc) 
e on a.doc=e.doc

LEFT JOIN

(
Select 
Doc,  
count(*) as '60-119'
from daycountview
where day_count='60-119' and unit like '%HP1'
group by doc)
f on a.doc=f.doc

LEFT JOIN

(
Select 
Doc,  
count(*) as '120+'
from daycountview
where day_count='120+' and unit like '%HP1'
group by doc) 
g on a.doc=g.doc
)

1 个答案:

答案 0 :(得分:0)

您应该在此处使用INSERT SELECT声明。试试这个:

CREATE PROCEDURE [dbo].[zMDWhp1] --HP1! 
AS
    BEGIN

        DECLARE @dte DATETIME 
        SELECT  dte = ( SELECT  CASE WHEN DATEPART(WEEKDAY, GETDATE()) = 1
                                     THEN DATEADD(dd, -9,
                                                  CONVERT(DATE, GETDATE()))
                                     ELSE DATEADD(dd,
                                                  -( DATEPART(WEEKDAY,
                                                              CONVERT(DATE, GETDATE()))
                                                     + 1 ),
                                                  CONVERT(DATE, GETDATE()))
                                END
                      )

        INSERT  INTO mdwall
                SELECT  a.doc ,
                        @dte ,
                        'HP1' AS [Type] ,
                        ISNULL([0-3], 0) AS [0-3] ,
                        ISNULL([4-10], 0) AS [4-10] ,
                        ISNULL([11-29], 0) AS [11-29] ,
                        ISNULL([30-59], 0) AS [30-59] ,
                        ISNULL([60-119], 0) AS [60-119] ,
                        ISNULL([120+], 0) AS [120+]
                FROM    ( SELECT DISTINCT
                                    doc
                          FROM      mdwfile
                        ) a
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '0-3'
                                    FROM    daycountview
                                    WHERE   day_count = '0-3'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) b ON a.doc = b.doc
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '4-10'
                                    FROM    daycountview
                                    WHERE   day_count = '4-10'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) c ON a.doc = c.doc
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '11-29'
                                    FROM    daycountview
                                    WHERE   day_count = '11-29'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) d ON a.doc = d.doc
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '30-59'
                                    FROM    daycountview
                                    WHERE   day_count = '30-59'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) e ON a.doc = e.doc
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '60-119'
                                    FROM    daycountview
                                    WHERE   day_count = '60-119'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) f ON a.doc = f.doc
                        LEFT JOIN ( SELECT  Doc ,
                                            COUNT(*) AS '120+'
                                    FROM    daycountview
                                    WHERE   day_count = '120+'
                                            AND unit LIKE '%HP1'
                                    GROUP BY doc
                                  ) g ON a.doc = g.doc 

    END