我在下面有一个SQL表(注意:用于使其更易于阅读的引号
Table1.Description
"HOUSING,WRAP RECESSED"
"HOUSING,ASSEMBLY 2-FEED"
"LENS,FLUSH SUS"
Table2.Abbreviations
ASSY = ASSEMBLY
FD = FEED
FLUSH = FL
HSG = HOUSING
LENS = LNS
RECESSED = REC
我想要更新,或者更新的东西,并创建一个新表(或者很好),Table1.Description与Table2.Abbreviations。它应该是这样的:
Table1.Description
"HSG,WRAP REC"
"HSG,ASSY 2-FD"
"LNS,FL SUS"
答案 0 :(得分:0)
一般策略是遍历每个单词/缩写对,并根据每对更新Table1.Description。我使用临时表创建了一个解决方案来说明如何执行此操作:
CREATE TABLE #table1 (descr VARCHAR(1000))
CREATE TABLE #abbr (word VARCHAR(1000), abr VARCHAR(1000), processed int)
DECLARE @word VARCHAR(1000)
DECLARE @abbr VARCHAR(1000)
INSERT INTO #table1
( descr )
VALUES ( 'HOUSING,WRAP RECESSED' -- descr - varchar(1000)
),
( 'HOUSING,ASSEMBLY 2-FEED' -- descr - varchar(1000)
),
( 'LENS,FLUSH SUS' -- descr - varchar(1000)
)
INSERT INTO #abbr
( word, abr, processed )
VALUES ( 'ASSEMBLY', -- word - varchar(1000)
'ASSY' -- abr - varchar(1000)
,0
),
( 'FEED', -- word - varchar(1000)
'FD' -- abr - varchar(1000)
,0
),
( 'FLUSH', -- word - varchar(1000)
'FL' -- abr - varchar(1000)
,0
),
( 'HOUSING', -- word - varchar(1000)
'HSG' -- abr - varchar(1000)
,0
),
( 'LENS', -- word - varchar(1000)
'LNS' -- abr - varchar(1000)
,0
),
( 'RECESSED', -- word - varchar(1000)
'REC' -- abr - varchar(1000)
,0
)
WHILE EXISTS (SELECT * FROM #abbr WHERE processed = 0)
BEGIN
SELECT TOP 1 @word = word, @abbr = abr FROM #abbr WHERE processed = 0
UPDATE #table1
SET descr = REPLACE(descr,@word,@abbr)
UPDATE #abbr
SET processed = 1
WHERE word = @word AND abr = @abbr
END
SELECT * FROM #table1
答案 1 :(得分:0)
试试这样:
缩写表
CREATE FUNCTION dbo.ReplaceAbbreviations(@string VARCHAR(MAX))
RETURNS VARCHAR(MAX) AS
BEGIN
SELECT @string=REPLACE(@string,LongTerm,ShortTerm)
FROM Abbreviations;
RETURN @string;
END
GO
执行魔术的功能
CREATE TABLE YourDescription (LongDescription VARCHAR(MAX));
INSERT INTO YourDescription VALUES
('HOUSING,WRAP RECESSED')
,('HOUSING,ASSEMBLY 2-FEED')
,('LENS,FLUSH SUS');
GO
包含字符串的表格
SELECT LongDescription
,dbo.ReplaceAbbreviations(LongDescription) AS ShortDescription
FROM YourDescription;
GO
使用函数
进行简单的选择DROP TABLE YourDescription;
DROP FUNCTION dbo.ReplaceAbbreviations;
DROP TABLE Abbreviations;
清理测试场景
LongDescription ShortDescription
HOUSING,WRAP RECESSED HSG,WRAP REC
HOUSING,ASSEMBLY 2-FEED HSG,ASSY 2-FD
LENS,FLUSH SUS LNS,FL SUS
结果
{{1}}