7

I have problem declaring multible table variables in Microsoft SQL Server. Trying to separate the tables with comma but seems to give me some syntax errors.

Code looks like this. Show both code as sample and as a picture in order for you to see the error marks:

DECLARE
@StructuredProducts TABLE(
    StructuredProductId INT
    ,ArrangerIdv2 INT
    ,ISIN VARCHAR(50)
    ,InstrumentId INT
    ,Caption VARCHAR(100)
    ,DbRegDate DATE
    ,EndDate DATE
),
@PriceDataOld TABLE(
    StructuredProductId INT
    ,FinalDate DATE
    ,FinalPriceDate DATE
    ,FinalPrice DECIMAL(9,3)
    ,FinalPriceSek DECIMAL(9,3)
),
@PriceDataEarlyExercise TABLE(
    StructuredProductId INT
    ,EEDate DATE
    ,EEPriceDate DATE
    ,EEPrice DECIMAL(9,3)
    ,EEPriceSek DECIMAL(9,3)
),
@PriceDataActive TABLE(
    StructuredProductId INT
    ,ActiveDate DATE
    ,ActivePriceDate DATE
    ,ActivePrice DECIMAL(9,3)
    ,ActivePriceSek DECIMAL(9,3)
)

enter image description here

3
  • 4
    Use a DECLARE for each table, instead of a , Commented Mar 1, 2019 at 10:00
  • 1
    Want the code to be as neat as possible. Isn't comma the way to declare multiple variables? Commented Mar 1, 2019 at 10:02
  • 2
    @David tables and variables are completely different please check the syntax Commented Mar 1, 2019 at 10:04

3 Answers 3

8

To address your comment

Isn't comma the way to declare multiple variables?

well, yes a comma can be used to declare multiple local variables and cursors, but not table variables - the docs explicitly say this:

n

Is a placeholder indicating that multiple variables can be specified and assigned values. When declaring table variables, the table variable must be the only variable being declared in the DECLARE statement.

Sign up to request clarification or add additional context in comments.

Comments

1
SQL server we have to create table like this format only there is no other styles 

 DECLARE @StructuredProducts TABLE(
StructuredProductId INT
,ArrangerIdv2 INT
,ISIN VARCHAR(50)
,InstrumentId INT
,Caption VARCHAR(100)
,DbRegDate DATE
,EndDate DATE
) ;
DECLARE @PriceDataOld TABLE(
StructuredProductId INT
,FinalDate DATE
,FinalPriceDate DATE
,FinalPrice DECIMAL(9,3)
,FinalPriceSek DECIMAL(9,3)
);
DECLARE @PriceDataEarlyExercise TABLE(
StructuredProductId INT
,EEDate DATE
,EEPriceDate DATE
,EEPrice DECIMAL(9,3)
,EEPriceSek DECIMAL(9,3)
);
DECLARE @PriceDataActive TABLE(
StructuredProductId INT
,ActiveDate DATE
,ActivePriceDate DATE
,ActivePrice DECIMAL(9,3)
,ActivePriceSek DECIMAL(9,3)
);    

Comments

1

change it like below

DECLARE
@StructuredProducts TABLE(
    StructuredProductId INT
    ,ArrangerIdv2 INT
    ,ISIN VARCHAR(50)
    ,InstrumentId INT
    ,Caption VARCHAR(100)
    ,DbRegDate DATE
    ,EndDate DATE
);DECLARE
@PriceDataOld TABLE(
    StructuredProductId INT
    ,FinalDate DATE
    ,FinalPriceDate DATE
    ,FinalPrice DECIMAL(9,3)
    ,FinalPriceSek DECIMAL(9,3)
);DECLARE
@PriceDataEarlyExercise TABLE(
    StructuredProductId INT
    ,EEDate DATE
    ,EEPriceDate DATE
    ,EEPrice DECIMAL(9,3)
    ,EEPriceSek DECIMAL(9,3)
);DECLARE
@PriceDataActive TABLE(
    StructuredProductId INT
    ,ActiveDate DATE
    ,ActivePriceDate DATE
    ,ActivePrice DECIMAL(9,3)
    ,ActivePriceSek DECIMAL(9,3)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.