删除/更新中的别名:关键字' AS附近的语法不正确

时间:2015-05-24 06:16:00

标签: sql-server-2008

如何在删除/更新语句中使用别名。 请参阅下面的SP。它给出错误关键字' AS

附近的语法不正确
/****** Object:  StoredProcedure [dbo].[usp_Market_Delete]    Script Date: 05/22/2015 16:37:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Market_Delete]
(
    @Original_MarketCd char(3)
)
AS
    SET NOCOUNT OFF;
DELETE FROM [MKT]  WHERE (([MKT_CD] AS MarketCd = @Original_MarketCd))
GO

2 个答案:

答案 0 :(得分:0)

如果我的理解是正确的,您需要为删除语句

的表提供别名

试试这个

DELETE From MarketCd
FROM [MKT] AS MarketCd WHERE ([MKT_CD]   = @Original_MarketCd)

答案 1 :(得分:0)

假设我正在解释你说的正确,你想要的是能够在执行更新或删除时通过别名引用字段。

你可以试试这个:

 ;
 WITH  Market
 AS   (
     SELECT    MarketCd = [MKT_CD]
     FROM      [MKT]
 )
 DELETE  Market
 FROM    Market
 WHERE   MarketCd = @Original_MarketCd

你会为更新做些类似的事情:

 ;
 WITH  Market
 AS   (
     SELECT    MarketCd = [MKT_CD]
     FROM      [MKT]
 )
 UPDATE  Market
    SET  MarketCd = @New_MarketCd
 WHERE   MarketCd = @Old_MarketCd

我正在做的是使用公用表表达式(CTE)对表和字段进行别名,然后使用首选别名执行DELETEUPDATE