在存储过程中创建视图 - 意外结果

时间:2016-06-06 15:08:28

标签: sql sql-server tsql

我已将以下创建视图放在一起:

CREATE VIEW rm_exporttable 
AS
    SELECT DISTINCT 
        [Item].ID,[Item].ItemLookupCode,
        [ItemClassComponent].Detail1,[ItemClassComponent].Detail2,
        [ItemClassComponent].Detail3,[ItemClass].SubDescription1,
        [ItemClass].SubDescription2,
        [Item].Description,
        ISNULL(CONVERT(varchar(50), [Item].Notes), '') Notes,
        [Department].Name as DepartmentName,
        [Department].Code DepartmentCode,
        [Category].Name as CategoryName,
        [Category].Code as CategoryCode,
        [Item].Price, [Item].SalePrice, [Item].Cost,
        [Tax].Percentage as Tax, [Item].Quantity, 
        [Item].ReplacementCost,
        ISNULL(CONVERT(varchar(50), [Alias].Alias), '') Alias,
        [Item].WebItem,  
        CASE 
           WHEN [ItemDynamic].StoreID = 6 
              THEN [ItemDynamic].Price 
              ELSE '' 
        END as GoddardsPrice,
        CASE 
           WHEN [ItemDynamic].StoreID = 5 
              THEN [ItemDynamic].Price 
              ELSE '' 
        END as GoldingPrice,
        [Item].LastUpdated 
    FROM
        Item
    LEFT JOIN
        ItemClassComponent ON [Item].ID = [ItemClassComponent].ItemID
    LEFT JOIN
        ItemClass ON [ItemClassComponent].ItemClassID = [ItemClass].Id
    LEFT JOIN
        Department ON [Department].ID = [Item].DepartmentID
    LEFT JOIN
        Category ON [Category].ID = [Item].CategoryID
    LEFT JOIN
        Tax ON [Tax].ID = [Item].TaxID
    LEFT JOIN
        Alias ON [Alias].ItemId = [Item].ID
    LEFT JOIN
        ItemDynamic ON [Item].Id = [ItemDynamic].ItemID
    LEFT JOIN
        rm_procedurelastran ON [Item].LastUpdated > rm_procedurelastran.date
    WHERE 
        [Item].WebItem = 1
        AND [Item].LastUpdated > [rm_procedurelastran].date

这似乎完全没有问题。现在我试图把它放在一个简单的存储过程中:

CREATE PROCEDURE rmprocedureran1
AS
    exec('Create View rm_exporttable As...')
GO

然而,这似乎并不奏效。该过程似乎运行正常,但是当我为表运行一个简单的select *语句时,它返回错误:

  

无法将char值转换为money。 char值的语法不正确。

它还没有设法创建所有列。

任何人都可以帮助解释我做错了吗?

感谢。

1 个答案:

答案 0 :(得分:0)

问题是''​​需要用''''替换(加倍引用)。正确的程序如下:

CREATE PROCEDURE rmprocedureran1
AS
exec('Create View rm_exporttable AS
SELECT DISTINCT [Item].ID,[Item].ItemLookupCode,[ItemClassComponent].Detail1,[ItemClassComponent].Detail2,[ItemClassComponent].Detail3,[ItemClass].SubDescription1,[ItemClass].SubDescription2,[Item].Description,ISNULL(CONVERT(varchar(50),[Item].Notes),'''') Notes,[Department].Name as DepartmentName,[Department].Code DepartmentCode,[Category].Name as CategoryName,[Category].Code as CategoryCode,[Item].Price,[Item].SalePrice,[Item].Cost,[Tax].Percentage as Tax,[Item].Quantity,[Item].ReplacementCost,ISNULL(CONVERT(varchar(50),[Alias].Alias),'''') Alias,[Item].WebItem,CASE WHEN [ItemDynamic].StoreID = 6 THEN [ItemDynamic].Price ELSE '''' END as GoddardsPrice,CASE WHEN [ItemDynamic].StoreID = 5 THEN [ItemDynamic].Price ELSE '''' END as GoldingPrice From Item
Left Join ItemClassComponent
On [Item].ID = [ItemClassComponent].ItemID
Left Join ItemClass
On [ItemClassComponent].ItemClassID = [ItemClass].Id
Left Join Department
On [Department].ID = [Item].DepartmentID
Left Join Category
On [Category].ID = [Item].CategoryID
Left Join Tax
On [Tax].ID = [Item].TaxID
Left Join Alias
On [Alias].ItemId = [Item].ID
Left Join ItemDynamic
on [Item].Id = [ItemDynamic].ItemID
Left Join rm_procedurelastran
On [Item].LastUpdated > rm_procedurelastran.date
Where [Item].WebItem = 1
And [Item].LastUpdated > [rm_procedurelastran].date')
GO