我是SQL新手并尝试确定如何将变量设置为 A 或 B 。
以下是声明:
DECLARE @Planner AS VARCHAR(50) = '2566927' OR @Planner = '12201704'
我收到的错误:
执行SQL查询时发生以下错误:
关键字“OR”附近的语法不正确。
这是一个更完整的样本:
DECLARE
@Planner AS VARCHAR(50) = '2566927'
--Temp Table for Final
CREATE TABLE #PP1(
Part_Key varChar(50)
,Part_No varChar(50)
,Part_Name varChar(50)
,CurInv DECIMAL(10,2)
,MinInv DECIMAL(10,2)
,Past_Due DECIMAL(10,2)
,Week2 DECIMAL(10,2)
,Week4 DECIMAL(10,2)
,Week8 DECIMAL(10,2)
,Week12 DECIMAL(10,2)
,Plus12 DECIMAL(10,2)
,Dep26w DECIMAL(10,1)
,Stock DECIMAL(10,1)
,StockPur DECIMAL (10,1)
)
--Temp Table to Limit Parts
CREATE TABLE #MRP_Parts(
MRP_PK varChar(50)
,MRP_PN varChar(50)
,MRP_PNAME varChar(50)
)
--Insert into Temp Part Table
INSERT #MRP_Parts
SELECT
PP.Part_Key
,PP.Part_No
,PP.Name
FROM Part_v_Part AS PP
WHERE (PP.Planner = @Planner OR @Planner = '')
--BEGIN Temp Table for Inventory
CREATE TABLE #CurrInv(
CI_Part_Key varChar(50)
,CI_Part_No varChar(50)
,CI_Qty DECIMAL(10,1)
,CI_Min DECIMAL(10,2)
)
INSERT #CurrInv
SELECT
PP.PArt_Key
,PP.Part_No
,ISNULL(PC1.Quantity,0)
,PP.Minimum_Inventory_Quantity
FROM Part_v_Part AS PP
OUTER APPLY
(
SELECT
SUM(PC.Quantity) AS Quantity
FROM Part_v_Container as PC
WHERE PP.part_Key=PC.part_Key
AND (PC.Container_Status = 'OK'
OR PC.Container_Status = 'Receiving'
OR PC.Container_Status = 'Testing Hold')
AND PC.Active = '1'
AND (PP.Planner = @Planner OR @Planner = '')
) AS PC1
我希望@Planner成为A或B
答案 0 :(得分:3)
第二个变量必须具有不同的名称。 E.g。
DECLARE @Planner1 VARCHAR(50) = '2566927',
@Planner2 varchar(10) = '12201704',
@OtherVar int = 42
并使用逗号,
答案 1 :(得分:2)
你不能这样做,但是你可以将一个变量声明为一个表并将多个值放入表中
DECLARE @Planner AS TABLE (P VARCHAR(50))
INSERT @Planner SELECT '2566927'
INSERT @Planner SELECT '12201704'
然后您可以在where in
类型子句
SELECT
PP.Part_Key
,PP.Part_No
,PP.Name
FROM Part_v_Part AS PP
WHERE PP.Planner IN (SELECT P FROM @Planner)