2009-07-13 69 views
1

在我的特殊情況下,我們有一個數據庫表,其中包括一個「圖像」字段。我們希望將「.jpg」附加到此列的每一行中。爲了簡單起見,我們會說它只有兩列......一個ID列和一個Image列。 (跳過具有空字符串「Image」的行的獎勵積分。)如何根據當前數據修改所有數據庫列數據?

問題標題更具開放性,因爲我可以想象有人想要對任何行進行任何操作。 (「更新所有行,使列'X'的值除以2」,「更新所有行,使列'Y'前面加上此路徑」等)

雖然我使用SQL Server ,我懷疑答案是非常普遍的......但如果你的特定答案不是,不要讓它阻止你迴應......但請在你的文章中添加它的用途。

回答

10

您的意思是?這在SQL Server中起作用。

UPDATE [table] 
SET  [image] = [image] + '.jpg' 
WHERE ISNULL([image], '') <> '' 

如果你想這是重複的:

UPDATE [table] 
SET  [image] = [image] + '.jpg' 
WHERE ISNULL([image], '') <> '' 
AND  [image] NOT LIKE '%.jpg' 
+3

可能會添加一個「AND [圖片]不喜歡'%.jpg'」 – 2009-07-13 15:09:33

+0

@Joel - 是的,他沒有指定重複性,但沒有壞的事情...... – 2009-07-13 15:30:57

1

這樣

update Table 
set image = image + '.jpg' 
where image <> '' 

update Table 
set image = image + '.jpg' 
where rtrim(image) <> '' 

update Table 
set image = image + '.jpg' 
where rtrim(coalesce(image,'')) <> '' 
0

線沿線的東西:

Update tableName 
Set Image = Image+'.jpg' 
where 
    IsNUlL(Image,'') != '' 

這是針對T-SQL(SQL Server)的方言。

0
UPDATE imageTbl SET image = image + '.jpg' WHERE image IS NOT NULL 
1

SQLServer的2K

declare @Image table (
Id int, 
Name varchar(10) 
) 

insert into @Image values (1,'hello') 
insert into @Image values (2,'') 
insert into @Image values (3,'world') 
update @Image set Name = Name + '.jpg' 
from @Image where Name <> '' 

select * from @Image 

update BLAH from YIKES語法是當你的標準更新跨度多個表,因爲它允許加入真正有用的。