基本上,我正在尝试清理数据库并使所有文件路径都指向示例文档。我正在从一个列(扩展名)中读取,然后更新另一列(文件名)并根据扩展名进行更改。我也想要全部。例如,如果扩展名为DOC,则将路径设置为“ sample.doc”;如果扩展名是PDF,则将路径设置为“ sample.pdf”;如果扩展名未知,则将路径设置为“ sample.txt”。
我可以使用此代码,但认为使用select / case循环会更好:
select * from dbo.tsrecelec
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleWord.doc'
WHERE dbo.tsrecelec.reExtension = 'DOC'
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleWord.docx'
WHERE dbo.tsrecelec.reExtension = 'DOCX'
UPDATE dbo.TSRECELEC
SET dbo.tsrecelec.reSID = '00+SampleADOBE.pdf'
WHERE dbo.tsrecelec.reExtension = 'PDF'
在我的NONE SQL大脑中,我在想类似的事情可能有用吗?
SELECT reSID, reExtension,
CASE
WHEN reExtension = 'DOC' THEN SET reSID = '00+SampleWord.doc'
WHEN dbo.tsrecelec.reExtension = 'DOCX' THEN SET dbo.tsrecelec.reSID = '00+SampleWord.docx'
ELSE dbo.tsrecelec.reSID = '00+SampleFile.txt'
END
FROM dbo.tsrecelec;
任何帮助,我们将不胜感激!
答案 0 :(得分:0)
三个更新都可以,但是您可以这样表达:
UPDATE dbo.TSRECELEC
SET reSID = (CASE WHEN reExtension = 'DOC'
THEN '00+SampleWord.doc'
WHEN reExtension = 'DOCX'
THEN '00+SampleWord.docx'
WHEN reExtension = 'PDF'
THEN '00+SampleADOBE.pdf'
END)
WHERE reExtension IN ('DOC', 'DOCX, 'PDF');
答案 1 :(得分:0)
尝试使用CASE...WHEN...THEN...
。如果您将来会获得更多扩展,则性能会更高:
UPDATE yourtable
SET col1 = (CASE WHEN col2 = 'extension you want'
THEN 'nameofyourfile.extension'
WHEN ...
THEN ...
END)
WHERE col2 IN ('extension1', 'extension2', 'extension3','...');