首页 > 解决方案 > 在 SQL Server 中声明表变量问题

问题描述

我想声明一个表变量,并按照此页面中的步骤进行操作,但出现错误SELECT @StdRevenue;。看来我需要声明变量 - 我做了。

Purchasing.ProductVendor

在此处输入图像描述

我的代码

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;

标签: sql-serverdeclare

解决方案


我希望下面的查询对您有所帮助....

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;

----到这里


推荐阅读