在存储过程中的SQL Server中的不同表中添加列

时间:2015-10-09 07:34:07

标签: sql sql-server stored-procedures sql-server-2014

我正在编写此存储过程以使用连接和简单逻辑从4个不同的表中获取详细信息,但是最终使用UNION操作系统将其全部归结为不能正常运行,因为我收到错误:期待AS,ID或quoted_id,请提出我在这方面做错了什么。

ALTER PROCEDURE [dbo].[cp_RejectionAnalysis] 
    (@RunDate DATE --'20150501')
AS
BEGIN
    SET NOCOUNT ON; 

    --DECLARE @RunDate DATE = '20150401'
    DECLARE @StartDate DATE = DATEADD(DD, -30, @RunDate);

    DECLARE @GeRejectionDate AS TABLE (PayerName VARCHAR(120), 
                                       ClaimCount INT);

    WITH  rejections AS 
    (
        SELECT 
            i.Number,
            COUNT(DISTINCT CASE WHEN far.SubmissionDate < @RunDate THEN far.ClaimKey ELSE NULL END) AS TotalRejectsFirst30, 
            COUNT(DISTINCT CASE WHEN far.SubmissionDate = @RunDate THEN far.ClaimKey ELSE NULL END) AS TotalRejectsRunDate
        FROM 
            table1 far
        INNER JOIN 
            table2 i ON far.InsurerInfoKey = i.InsurerKey
        WHERE 
            far.isRejection = 1
            AND (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate)
        GROUP BY 
            i.Number), 
    totalclaims AS 
    (
        SELECT 
            i.Number,
            SUM(CASE WHEN ts.SubmissionDate < @RunDate THEN ts.TotalClaims ELSE 0 END) AS TotalClaimsFirst30, 
            SUM(CASE WHEN ts.SubmissionDate = @RunDate THEN ts.TotalClaims ELSE 0 END) AS TotalClaimsRunDate
        FROM 
            table3 ts
        INNER JOIN 
            table2 i ON ts.InsurerInfoKey = i.InsurerKey
        WHERE 
            ts.SubmissionDate >= @StartDate 
            AND ts.SubmissionDate <= @RunDate
        GROUP BY 
            i.Number
        --ORDER BY i.Number
        )
    /*, PaymentRejectionDetails AS (
        (SELECT DISTINCT ra.Message AS ErrorMessage, ra.ErrorListKey AS ErrorListKey
                        FROM dimErrorListRejectionAnalysis ra
                            INNER JOIN table1 far on ra.ErrorListKey= far.ErrorListKey
                        WHERE (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate))
                    UNION
                        (SELECT DISTINCT QcHistClaimId AS ClaimId FROM  table1 far
                        WHERE (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate))
                    UNION
                        (SELECT DISTINCT SITEID FROM table4 ds
                            INNER JOIN table1 far ON ds.sitekey=far.sitekey
                        WHERE (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate))  
                )*/
        , ClaimsID AS
        (
            SELECT DISTINCT ClaimId 
            FROM table1 far
            WHERE (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate))
        , SiteId AS
        (
            SELECT DISTINCT SITEID 
            FROM table4 ds
            INNER JOIN table1 far ON ds.sitekey = far.sitekey
            WHERE (far.MessageDate >= @StartDate AND far.MessageDate <= @RunDate))
    SELECT * 
    INTO D 
    FROM
        (SELECT ClaimId FROM ClaimsID 
         UNION 
         SELECT SITEID FROM SiteId
         UNION
         SELECT 
             r.PayerNumber, r.TotalRejectsFirst30, 
             r.TotalRejectsRunDate, 
             ISNULL(t.TotalClaimsFirst30, 0) AS ClaimsFirst30, 
             ISNULL(t.TotalClaimsRunDate, 0) AS ClaimsRunDate
         FROM 
             rejections r
         LEFT OUTER JOIN 
             totalclaims t ON t.PayerNumber = r.PayerNumber)
END

2 个答案:

答案 0 :(得分:2)

此处无效使用UNION

 SELECT ClaimId FROM ClaimsID 
 UNION 
 SELECT SITEID FROM SiteId
 UNION
 SELECT r.PayerNumber, r.TotalRejectsFirst30, r.TotalRejectsRunDate, ISNULL(t.TotalClaimsFirst30, 0) AS ClaimsFirst30, ISNULL(t.TotalClaimsRunDate, 0) AS ClaimsRunDate
 FROM rejections r

首先,您需要了解UNION

的情况
  

将两个或多个查询的结果合并到一个结果集中   包括属于union中所有查询的所有行。   UNION操作与使用组合列的连接不同   来自两张桌子。

以下是使用UNION组合两个查询的结果集的基本规则:

  1. 所有查询中列的数量和顺序必须相同。

  2. 数据类型必须兼容。

  3. 编辑2:

    我不确定你真正想要什么,而且你没有回复我的评论。 但我猜您可以尝试使用LEFT JOIN

    SELECT 
        r.PayerNumber,
        r.TotalRejectsFirst30,
        r.TotalRejectsRunDate,
        ISNULL(t.TotalClaimsFirst30, 0) AS ClaimsFirst30,
        ISNULL(t.TotalClaimsRunDate, 0) AS ClaimsRunDate,
        c.ClaimId as ClaimId
    FROM rejections r LEFT JOIN ClaimsID c On r.ClaimId=c.ClaimId
    

答案 1 :(得分:0)

请删除最后一个块周围的星号,并为子查询添加别名。

            *SELECT * INTO D FROM
            (
            SELECT ClaimId FROM ClaimsID UNION SELECT SITEID FROM SiteId
            UNION
            SELECT r.PayerNumber, r.TotalRejectsFirst30, r.TotalRejectsRunDate, ISNULL(t.TotalClaimsFirst30, 0) AS ClaimsFirst30, ISNULL(t.TotalClaimsRunDate, 0) AS ClaimsRunDate
            FROM rejections r
            LEFT OUTER JOIN totalclaims t ON t.PayerNumber = r.PayerNumber)*  

您无法将行与数字/字段类型合并。您对整个程序的最终结果是什么?顺便说一下,发布表格的创建脚本将有助于回答这个问题。