【问题标题】:Variable declaration not finding data type变量声明未找到数据类型
【发布时间】:2016-01-21 17:26:02
【问题描述】:

我想将一些数据存储到 SQL Server 中的表类型中。在下面的代码中,我定义了表的结构并将其声明为一个类型。但后来,用该类型声明的变量找不到它。错误是:

列、参数或变量 #17:找不到数据类型 ARTSRetailPriceModifier。

声明和变量赋值的代码是:

CREATE TYPE ARTSRetailPriceModifier AS TABLE   
( 
    ID int NOT NULL IDENTITY(1,1),
    RecID int,
    PromoId int,
    SNumber int,
    MCode VARCHAR(50),
    Amount DECIMAL(7,2),
    [Action] VARCHAR(20),
    NPrice DECIMAL(7,2),
    PPrice DECIMAL(7,2),
    RCode VARCHAR(1),
    PCondition VARCHAR(1)
);

DECLARE @PriceModifiers AS ARTSRetailPriceModifier

INSERT INTO @PriceModifiers (
    RecID,
    PromoId,
    SNumber,
    MdCode,
    Amount,
    [Action],
    NPrice,
    PPrice,
    RCode,
    PCondition
)

SELECT  d.RecID AS 'RecID'
    , 1 AS 'PromotionID'
    , d.SequenceNumber + 1 AS 'SequenceNumber'
    , 'PriceOverride'AS 'MethodCode'
    , ABS(d.RetailPrice - d.UnitListPrice) AS 'Amount'
    , 'Replace' AS 'Action'
    , d.UnitListPrice AS 'NewPrice'
    , d.RetailPrice AS 'PreviousPrice'
    , '1' AS 'ReasonCode'
, '1' AS 'PromoCondition' 
FROM #Items d

表格#Items已正确填写。

【问题讨论】:

    标签: sql-server sql-server-2014 temp-tables user-defined-types


    【解决方案1】:

    CREATE TYPE 之后添加GO。 查看示例here

    可能的解释是在事务完成之前没有构造或添加到系统中,但是当我们要使用它执行一些操作时,我们必须在系统中有一个真实的类型。

    【讨论】:

    • 包含 GO 不会在该上下文中编译。仅供参考,我使用的是 SQL Server 2014。
    • 是的,它无法编译,因为您有一个名为 PriceModifiers 而不是 Items 的变量。并且没有专栏SequenceNumber ,但我相信SNumber
    • 这不是重点。这些是更改的变量名称。在我的原始代码中很好。它只是显示 GO: Incorrect syntax near 'GO' 的编译错误。
    • 嗯,是的,这是可能的。但是,如果您尝试将 GO 添加到您帖子中的代码中,那么一切正常,没有错误。可能,您的真实代码中有一些情况会导致此错误,我不知道。
    • 无论如何,我建议将类型创建和实际代码分开,因为您将面临类型已经存在的错误。分开的代码应该可以正常工作,
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多