查询在数据后面添加名称

时间:2019-02-12 05:21:58

标签: sql-server

我正在运行一个看起来像这样的查询。

ALTER PROCEDURE [dbo].[DeleteImage]
    @AdvID INT
AS
BEGIN
    UPDATE a 
    SET a.Status = 0
    FROM Advertisement a
    WHERE a.AdvID = @AdvID
END

它将状态设置为0,但是我还想使查询在“名称”列中添加一个单词(已删除),以便在我要运行另一个查询时可以执行类似

IF a.Name LIKE "Deleted"  
   SET a.STATUS = 0
ELSE IF a.startDate <= GETDATE() 
   SET a.STATUS = 1

请帮助谢谢!!

ALTER PROCEDURE [dbo].[DeleteImage]
    @AdvID INT
AS
BEGIN
    UPDATE a 
    SET a.Status = 0, a.Name = Name + "Deleted"
    FROM Advertisement a
    WHERE a.AdvID = @AdvID
END

我以为是这样,但是我知道格式错误

1 个答案:

答案 0 :(得分:1)

您可以尝试以下查询。

这里我用的是CONCAT而不是+

 ALTER PROC [dbo].[DeleteImage]
    @AdvID int
    AS
    BEGIN
        UPDATE a SET a.Status = 0 , a.Name = CONCAT(Name,'+','Deleted')
        FROM Advertisement a
        WHERE a.AdvID = @AdvID
    END

在以下查询中使用以下条件查询条件:Name like '%Deleted%'然后Status = 0,或者startDate<=GETDATE()然后Status = 1。

UPDATE a SET a.Status = 
        CASE WHEN a.Name LIKE '%Deleted%' THEN 0  
             WHEN a.startDate <= GETDATE() THEN 1 
             ELSE a.Status END
        FROM Advertisement a
        WHERE a.AdvID = @AdvID