0

我正在创建一个从许多表返回数据的存储过程,因此它有几个块,最终导致一个临时表。唯一的问题是它没有产生任何东西。所有单独的块似乎都可以工作,但作为一个整体却没有。

我觉得我可能遗漏了一些小东西,因此感谢您的意见。

@ref_num    VARCHAR(20)

AS
BEGIN
    SET NOCOUNT ON;

BEGIN TRY

    DECLARE @clKey INT
    DECLARE @clName VARCHAR(50)
    SET @clKey = (SELECT ClearingBroker FROM DocumentRecieved WHERE ref_num = @ref_num)
    SET @clName = (SELECT EB_Name FROM EB_Company WHERE EB_dtc_num = @clKey)

    SELECT
                a.ref_num
                ,b.DateSent
                ,b.Document as 'FormName'
                ,b.ClearingBroker as 'ClearingBrokerID'
                ,b.ExecutingBroker as 'ExecutingBrokerID'
                ,c.EB_Name
                ,a.AccountName
    INTO        
                #Temp1
    FROM
                EB_Company c JOIN PBIN_records a ON c.EB_dtc_num = a.eb_key 
                             JOIN DocumentRecieved b ON a.ref_num = b.ref_num 
    WHERE
                a.ref_num = @ref_num

--================================================================================================

    CREATE TABLE 
                #Temp2 (
                            ClearingBroker  VARCHAR(50)
                        )

--================================================================================================

    INSERT INTO
                #Temp2  (
                            ClearingBroker
                        )

    VALUES
                        (
                            @clName
                        )

--================================================================================================                      

    SELECT
            fName + ' ' + lName as 'SentBy'
    INTO
            #Temp3
    FROM
            Master_Contacts JOIN PBIN_records ON PBIN_records.eb_contact_key = Master_Contacts.contactID
    WHERE
            PBIN_records.ref_num = @ref_num

--================================================================================================

    SELECT
            #Temp1.ref_num,
            #Temp1.FormName,
            #Temp1.DateSent,
            #Temp1.ClearingBrokerID,
            #Temp2.ClearingBroker,
            #Temp1.ExecutingBrokerID,
            #Temp1.EB_Name,
            #Temp1.AccountName,
            #Temp3.SentBy
    INTO
            #Temp4
    FROM
            #Temp1, #Temp2, #Temp3

--================================================================================================

    SELECT
            *
    FROM
            #Temp4

--================================================================================================

END TRY

编辑:似乎因为我的一个表目前没有记录,它破坏了整个过程,因为我的最终 SELECT INTO 是我的前三个临时表之间的笛卡尔积。

4

1 回答 1

5

问题:您真的想要#Temp1, #Temp2,之间的笛卡尔积#Temp3吗?我问是因为FROM #Temp1, #Temp2, #Temp3代表笛卡尔积。

可能的原因之一是这三个表之一没有行。

示例:以下查询

SELECT  *
FROM    (SELECT 1 UNION ALL SELECT 2) AS TableA(ID)
CROSS JOIN (SELECT 1 WHERE 1=0) AS TableB(ID)

输出 0 行

ID          ID
----------- -----------

(0 row(s) affected)
于 2013-10-09T18:50:47.647 回答