0

如果它的值为 NULL,我无法将文本添加到列。它说(1 行受影响)但它仍然说 NULL。但是如果已经存在值,它可以将文本插入列中。前任。'任何事物'

ProductGroup_Keywords(varchar(max), null)

我在下面尝试了这两条路线

`Update [dbo].[Category]
 SET ProductGroup_Keywords = ProductGroup_Keywords + 'football'
 WHERE Category_ID = 101`

'Update [dbo].[Category]
 SET ProductGroup_Keywords = convert(nvarchar(max),ProductGroup_Keywords) + 'football'
 WHERE Category_ID = 101'

然后我尝试像下面那样设置它,但它仍然说 NULL

'Update [dbo].[Category]
 SET ProductGroup_Keywords = 'football'
 WHERE ProductGroup_Keywords = 101'

请帮忙!:)

4

2 回答 2

3
 Update [dbo].[Category] 
 SET ProductGroup_Keywords = coalesce( ProductGroup_Keywords, '')  + 'football' 
 WHERE Category_ID = 101' 
于 2012-08-31T18:09:46.387 回答
1

另一种方法是使用 CASE 语句:

 Update [dbo].[Category] 
 SET ProductGroup_Keywords = CASE 
                                WHEN ProductGroup_Keywords IS NULL THEN 'football'
                                ELSE ProductGroup_Keywords + 'football'
                             END  
 WHERE Category_ID = 101' 

或 ISNULL 而不是 COALESCE ......

 Update [dbo].[Category] 
 SET ProductGroup_Keywords = ISNULL( ProductGroup_Keywords, '')  + 'football' 
 WHERE Category_ID = 101' 

重要的是@JoeStefanelli 所说的:x + NULL = NULL

于 2012-09-02T18:53:08.903 回答