SQL Server 2012:一个存储过程中的多个查询

时间:2014-08-06 19:50:52

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

如何为这些查询创建存储过程,并且输出应显示从中捕获异常的检查以及所有相关数据。

SELECT cm.Cust_id, cm.cust_ref_id4, cm.cust_ref_id3, cm.plan_group, cm.Company_name, cm.Cust_firstname, cm.Cust_lastname 
COALESCE(c.pkCustomerID, c2.fkCustomerID, c3.pkCustomerID, c4.pkCustomerID) AS pkCustomerID, c3.CompanyName FROM PRODUCTIONSQL.[SigmaPaTri].[dbo].[CUSTOMER_MASTER] cm 
LEFT JOIN PHOENIX.CORE.dbo.Customers AS c ON cust_ref_id4 = c.pkCustomerID AND cm.cust_ref_id3 = c.pkCustomerID AND cm.cust_ref_id3 >= 1000000 AND cm.Cust_firstname + ' ' + cm.Cust_lastname = c.CompanyName 
LEFT JOIN PHOENIX.CORE.dbo.Contracts AS c2 ON cm.cust_ref_id3 = c2.ConfirmationNumber 
WHERE cm.cust_status IN ('A','P','R','G') AND COALESCE(c.pkCustomerID, c2.fkCustomerID) IS NULL ORDER BY cust_ref_id4;

SELECT [pkCustomerID],b.[pkContractID],[pkCustomerTypeID],[CustomerType],b.[ContractType] AS Contractype1,c.[ContractType]
AS Contractype2 FROM [CORE].[dbo].[Customers] a 
JOIN [CORE].[dbo].[CustomerTypes] ON [pkCustomerTypeID] = [fkCustomerTypeID] 
LEFT JOIN (SELECT [pkContractID],[ContractType],[fkCustomerID] FROM [CORE].[dbo].[Contracts]
JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID] WHERE [ContractType] NOT LIKE 'Holdover%')b ON a.pkCustomerID=b.fkCustomerID
LEFT JOIN (SELECT [pkContractID],[fkCustomerID],[ContractType] FROM [CORE].[dbo].[Contracts]
JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID] WHERE ContractType LIKE 'Holdover%')c ON b.fkCustomerID=c.fkCustomerID WHERE [CustomerType] IN ('Customer','Former Customer') AND (b.ContractType IS NULL OR c.ContractType IS NULL)

1 个答案:

答案 0 :(得分:0)

你的问题是缺少一个非常重要的信息,解释你正在尝试做什么。我在黑暗中拍了一枪,猜测你可能在寻找什么。顺便说一句,我通过格式化程序运行它,因此它很清晰。

    SELECT 'Found in query1'
    ,cm.Cust_id
    ,cm.cust_ref_id4
    ,cm.cust_ref_id3
    ,cm.plan_group
    ,cm.Company_name
    ,cm.Cust_firstname
    ,cm.Cust_lastname
    ,COALESCE(c.pkCustomerID, c2.fkCustomerID, c3.pkCustomerID, c4.pkCustomerID) AS pkCustomerID
    ,c3.CompanyName
FROM PRODUCTIONSQL.[SigmaPaTri].[dbo].[CUSTOMER_MASTER] cm
LEFT JOIN PHOENIX.CORE.dbo.Customers AS c ON cust_ref_id4 = c.pkCustomerID
    AND cm.cust_ref_id3 = c.pkCustomerID
    AND cm.cust_ref_id3 >= 1000000
    AND cm.Cust_firstname + ' ' + cm.Cust_lastname = c.CompanyName
LEFT JOIN PHOENIX.CORE.dbo.Contracts AS c2 ON cm.cust_ref_id3 = c2.ConfirmationNumber
WHERE cm.cust_status IN (
        'A'
        ,'P'
        ,'R'
        ,'G'
        )
    AND COALESCE(c.pkCustomerID, c2.fkCustomerID) IS NULL

SELECT 'Found in query 2'
    ,[pkCustomerID]
    ,b.[pkContractID]
    ,[pkCustomerTypeID]
    ,[CustomerType]
    ,b.[ContractType] AS Contractype1
    ,c.[ContractType] AS Contractype2
FROM [CORE].[dbo].[Customers] a
INNER JOIN [CORE].[dbo].[CustomerTypes] ON [pkCustomerTypeID] = [fkCustomerTypeID]
LEFT JOIN (
    SELECT [pkContractID]
        ,[ContractType]
        ,[fkCustomerID]
    FROM [CORE].[dbo].[Contracts]
    INNER JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID]
    WHERE [ContractType] NOT LIKE 'Holdover%'
    ) b ON a.pkCustomerID = b.fkCustomerID
LEFT JOIN (
    SELECT [pkContractID]
        ,[fkCustomerID]
        ,[ContractType]
    FROM [CORE].[dbo].[Contracts]
    INNER JOIN [CORE].[dbo].[ContractTypes] ON [fkContractTypeID] = [pkContractTypeID]
    WHERE ContractType LIKE 'Holdover%'
    ) c ON b.fkCustomerID = c.fkCustomerID
WHERE [CustomerType] IN (
        'Customer'
        ,'Former Customer'
        )
    AND (
        b.ContractType IS NULL
        OR c.ContractType IS NULL
        )