声明标量变量问题

时间:2019-06-03 17:22:37

标签: sql sql-server

我正在尝试使用声明语句重命名某些类别来将多个数据库的物料拼凑在一起。我在确定

时遇到困难

我已经编写了大部分代码,但是得到了这个错误:

  

137级2号线第11行137消息
  必须声明标量变量“ @Item”。

SELECT        dbo.[Threshold Enterprises$Warehouse Entry].[Bin Type Code], dbo.[Threshold Enterprises$Warehouse Entry].[Item No_], dbo.[Threshold Enterprises$Item].Description, 
                         SUM(dbo.[Threshold Enterprises$Warehouse Entry].Quantity) AS Quantity, dbo.[Threshold Enterprises$Warehouse Entry].[Location Code], dbo.[Threshold Enterprises$Item].[Item Status], 
                         dbo.[Threshold Enterprises$Item].[Item UPC_EAN Number], dbo.[Threshold Enterprises$Item].[Vendor No_], dbo.[Threshold Enterprises$Item].[Brand (Dimension)], LEFT(dbo.[Threshold Enterprises$Item].No_, 2) AS Prefix
FROM            dbo.[Threshold Enterprises$Item] LEFT OUTER JOIN
                         dbo.[Threshold Enterprises$Warehouse Entry] ON dbo.[Threshold Enterprises$Item].No_ = dbo.[Threshold Enterprises$Warehouse Entry].[Item No_]
WHERE        (dbo.[Threshold Enterprises$Item].[Inventory Posting Group] = N'FIN_MFG_PR') OR
                         (dbo.[Threshold Enterprises$Item].[Inventory Posting Group] = 'VEND_PROD')
GROUP BY dbo.[Threshold Enterprises$Warehouse Entry].[Item No_], dbo.[Threshold Enterprises$Warehouse Entry].[Location Code], dbo.[Threshold Enterprises$Item].Description, 
                         dbo.[Threshold Enterprises$Warehouse Entry].[Bin Type Code], dbo.[Threshold Enterprises$Item].[Item Status], dbo.[Threshold Enterprises$Item].[Item UPC_EAN Number], dbo.[Threshold Enterprises$Item].[Vendor No_], 
                         dbo.[Threshold Enterprises$Item].[Brand (Dimension)], LEFT(dbo.[Threshold Enterprises$Item].No_, 2)
HAVING        (dbo.[Threshold Enterprises$Warehouse Entry].[Item No_] LIKE N'%' + UPPER(@Item) + N'%') AND (dbo.[Threshold Enterprises$Item].[Brand (Dimension)] LIKE N'%' + UPPER(@Brand) + N'%') AND 
                         (dbo.[Threshold Enterprises$Item].[Vendor No_] LIKE N'%' + UPPER(@Vendor) + N'%') AND (dbo.[Threshold Enterprises$Warehouse Entry].[Bin Type Code] IN (@BinType)) AND 
                         (dbo.[Threshold Enterprises$Warehouse Entry].[Location Code] IN (@Location)) AND (dbo.[Threshold Enterprises$Item].[Item Status] IN (@Status)) AND (LEFT(dbo.[Threshold Enterprises$Item].No_, 2) LIKE '%' + UPPER(@Prefix) 
                         + '%') OR
                         (dbo.[Threshold Enterprises$Warehouse Entry].[Item No_] LIKE N'%' + UPPER(@Item) + N'%') AND (dbo.[Threshold Enterprises$Item].[Brand (Dimension)] LIKE N'%' + UPPER(@Brand) + N'%') AND 
                         (dbo.[Threshold Enterprises$Item].[Vendor No_] LIKE N'%' + UPPER(@Vendor) + N'%') AND (dbo.[Threshold Enterprises$Warehouse Entry].[Bin Type Code] IN (@BinType)) AND 
                         (dbo.[Threshold Enterprises$Warehouse Entry].[Location Code] IN (@Location)) AND (dbo.[Threshold Enterprises$Item].[Item Status] IN (@Status)) AND (LEFT(dbo.[Threshold Enterprises$Item].No_, 2) LIKE '%' + UPPER(@Prefix) 
                         + '%')

我应该能够取回我指定物品的库存。

1 个答案:

答案 0 :(得分:2)

当您确认已从报表构建器使用查询设计器创建脚本时,可以理解,报表具有如此多的参数,可以在生成报表之前选择这些参数或使用UI插入值。选定/插入的参数值然后传递给查询,然后用选定/插入的值替换查询中的参数/变量后,查询将相应执行。

问题是,您正在尝试在SSMS(SQL SERVER MANAGEMENT STUDIO)中执行相同的脚本,如果不使用那里的值替换变量,则无法执行该脚本。您可以在脚本中看到有很多变量放置为-@ Item,@ Brand,@ Vendor以及更多。仅当您运行报表时报表才有意义,因为报表引擎使用从UI插入的值来处理这些变量。但是,当您想从SSMS运行脚本时,您必须需要在脚本顶部声明所有这些变量,并将必要的值传递给它们以执行脚本。

您可以如下声明所有变量,完成声明和值分配后,现在就可以在SSMS中执行脚本了。

DECLARE @Item VARCHAR(200) = 'Put Value'
DECLARE @Brand VARCHAR(200) = 'Put Value'
DECLARE @Vendor VARCHAR(200) = 'Put Value'
-- Similar way you need to Declare and assign value to all 
-- other variables used in the script.

SELECT    
dbo.[Threshold Enterprises$Warehouse Entry]
....... Rest of the script