0

我有一个包含帐号和标签(关键词)的表。我的查询检查该帐户是否存在,如果存在,则需要将新关键字附加到已存在的内容中。

如果帐户没有退出,它只是做一个简单的插入。

我的斗争是从 IF Exists 子句中获取当前标签,并在更新语句中将新数据附加到其中。有任何想法吗?

BEGIN
SET NOCOUNT ON;

IF EXISTS (SELECT id, tags FROM AccountLogAccounts WHERE account = @account)
BEGIN
    UPDATE AccountLogAccounts 
    SET tags = (
        SELECT tags 
        FROM AccountLogAccounts
        WHERE account = @account
    ) + ',' @tags --This doesn't work :)
    WHERE account = @account
END
ELSE
    BEGIN
         INSERT INTO AccountLogAccounts (
            account,
            location,
            tags,
            whoAdded,
            whenAdded
        ) VALUES (
            @account,
            @location,
            @tags,
            @ntid,
            GETDATE()
        )
            END
END
4

1 回答 1

2

代替...

UPDATE AccountLogAccounts 
    SET tags = (
        SELECT tags 
        FROM AccountLogAccounts
        WHERE account = @account
    ) + ',' @tags --This doesn't work :)
    WHERE account = @account

...尝试...

UPDATE AccountLogAccounts 
    SET tags = tags + ',' + @tags
    WHERE account = @account

编辑

这是一个小演示(未经测试,但应该可以):

CREATE PROCEDURE UpdateMyText
    @account int,
    @tags nvarchar(max)
AS
UPDATE AccountLogAccounts 
    SET tags = CAST(tags as nvarchar(max)) + ',' + @tags
    WHERE account = @account
GO

EXEC UpdateMyText 12345, 'abc'
GO
于 2013-10-30T21:02:32.160 回答