-1

I want to declare a table variable and I followed the steps in this page but I get error on the line SELECT @StdRevenue;. It seems that I need to declare the variable - which I did.

The table Purchasing.ProductVendor:

enter image description here

My code

DECLARE @StdRevenue TABLE
(
  ProductID int, 
  Revenue money
);

--insert values to columns
INSERT INTO @StdRevenue (ProductID, Revenue)
    SELECT ProductID, StandardPrice * OnOrderQty 
    FROM Purchasing.ProductVendor
    GROUP BY ProductID;

SELECT @StdRevenue;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Osca
  • 1,588
  • 2
  • 20
  • 41

3 Answers3

2

I hope below queries help you....

Create table #temp

(
id int,
price varchar(5),
qty int
)

insert into #temp values(1,'50',1)

insert into #temp values(2,'500',4)

insert into #temp values(3,'100',6)



--Select All queries together

DECLARE @StdRevenue TABLE
(
  ProductID int, 
  Revenue money
);

--insert values to columns

INSERT INTO @StdRevenue (ProductID, Revenue)
    SELECT id, price * qty 
    FROM #temp


SELECT * from @StdRevenue;

----Till here

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
2

Use

SELECT * FROM @StdRevenue;

instead of

SELECT @StdRevenue;

DECLARE @StdRevenue TABLE
(
  ProductID int, 
  Revenue money
);

--insert values to columns
INSERT INTO @StdRevenue (ProductID, Revenue)
    SELECT ProductID, StandardPrice * OnOrderQty 
    FROM Purchasing.ProductVendor
    GROUP BY ProductID;

SELECT * FROM @StdRevenue;
Dumi
  • 1,414
  • 4
  • 21
  • 41
1

Try the following code

DECLARE @StdRevenue TABLE
(
  ProductID int, 
  Revenue money
);

--insert values to columns
INSERT INTO @StdRevenue (ProductID, Revenue)
    SELECT 1, 2

SELECT * from @StdRevenue;

Here you have missed to write * from before table name like @StdRevenue

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42