我有一個查詢被用來返回數據:結合使用SELECT結果,其中多行變量
Select top 100
cnb.ndc_id,
cnb.contract_num_val,
cnb.quote_price,
cnb.eff_dt,
cnb.end_dt,
cnb.discount_pct,
cnb.rebate_pct
from [NDC_ATTR] ndc
INNER JOIN CONTRACT_NDC_BRG cnb ON ndc.attr_val=cnb.NDC_ID
where ndc.field_id = 69
and cnb.contract_ndc_brg_id = @CONT;
當我運行它,我得到一個錯誤:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我知道這是因爲@CONT有多行。但這正是我需要的。
我一直在尋找通過帖子,它似乎不鼓勵在SQL中使用循環。 (糾正我,如果我錯了)
我將如何返回上面的查詢的聯合,但每行返回@CONT中的每一行?
編輯:
@CONT定義:
declare @CONT varchar(50) = (
SELECT contract_ndc_brg_id
FROM contract_ndc_brg
INNER JOIN ndc ON contract_ndc_brg.ndc_id = ndc.item_pk
where item_id = @VALUE
);
是@cont表變量? – RThomas
@RThomas它是一個選擇語句,其中一列放入一個變量中。 –
如何定義「@ CONT」? – dnoeth