我有一个查询被用来返回数据:结合使用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