简体   繁体   中英

Creating a view within a stored procedure - unexpected results

I have put together the following create view:

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

This seems to work absolutely fine on its own. Now I'm trying to put it in a simple stored procedure like such:

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

However this doesn't seem to work. The procedure seems to run fine, however when I go to run a simple select * statement for the table it returns the error:

Cannot convert a char value to money. The char value has incorrect syntax.

It also hasn't managed to create all of the columns.

Can anyone help explain what I'm doing wrong?

Thanks.

Issue was with '' needed to be replaced with '''' (doubling quotations). Correct procedure looks like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM