我正在创建一个从多个表中返回数据的存储过程,因此它有几个块,最终导致一个临时表。唯一的问题是它没有生产任何东西。所有单独的块似乎都可以工作,但总的来说并非如此。
我觉得我可能会遗漏一些小东西,所以感谢您的意见。
@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是我前三个临时表之间的笛卡尔积。
答案 0 :(得分: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)