Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare multiple variables in SQL

I am new to SQL and trying to determine how to set a variable to either A or B.
Here is the statement:

DECLARE @Planner AS VARCHAR(50) = '2566927' OR @Planner = '12201704'

And the error I am receiving:

The following errors occurred during execution of the SQL query:
Incorrect syntax near the keyword 'OR'.

Here is a more complete sample:

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

What I would like is for the @Planner to be either A or B

like image 563
user9324484 Avatar asked Feb 06 '18 22:02

user9324484


People also ask

Can I declare multiple variables in SQL?

SQL Server is great product and it has many feature which are very unique to SQL Server. Regarding feature of SQL Server where multiple variable can be declared in one statement, it is absolutely possible to do. From above example it is clear that multiple variables can be declared in one statement.

Can you declare multiple variables in one line?

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

Can you declare variables in SQL?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do you declare more than one variable?

When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evident. Such declarations are not required to be on a separate line, and the explanatory comment may be omitted.


2 Answers

A second variable must have a different name. E.g.

DECLARE  @Planner1 VARCHAR(50) = '2566927',
         @Planner2 varchar(10) = '12201704',
         @OtherVar int = 42

And separate each variable declaration with a comma ,

like image 81
Disillusioned Avatar answered Sep 20 '22 00:09

Disillusioned


You can't do that, but you can declare a variable as a table and put multiple values into the table

DECLARE @Planner AS TABLE (P VARCHAR(50))
INSERT @Planner SELECT '2566927' 
INSERT @Planner SELECT '12201704'

And then you can use the table variable in a where in type clause

SELECT
    PP.Part_Key
    ,PP.Part_No
    ,PP.Name
FROM Part_v_Part AS PP
WHERE PP.Planner IN (SELECT P FROM @Planner)
like image 35
Kirk Broadhurst Avatar answered Sep 19 '22 00:09

Kirk Broadhurst