SQL 服务器替换()

SQL Server REPLACE()

如果我在 SQL 服务器中有一个动态查询,其中包含列名变量 first_namelast_nameref_IDorder_IDpost_code_ID 如何用相同的字符串替换所有后缀为 _ID 的值,例如 look_up_value.

期望的输出是

first_name, last_name, look_up_value, look_up_value, look_up_value

目前有 SELECT REPLACE(@Var, '_ID', 'look_up_value') 但显然留下了列的前缀。

谢谢

DECLARE @SQL NVARCHAR(MAX)
DECLARE @Fields NVARCHAR(MAX)      
DECLARE @Fields2 NVARCHAR(MAX)                         
DECLARE @Return NVARCHAR(MAX) = Char(13)                                        
                              
SET @Fields = ''

SELECT @Fields = @Fields + Column_name + ',' + @Return
FROM Orders.INFORMATION_SCHEMA.columns 
WHERE TABLE_NAME = 'Recent_Orders'

SELECT @Fields = LEFT(@Fields, (LEN(@Fields) - 2))

SELECT REPLACE(@Fields, '_ID', 'look_up_value')
....    
SELECT @Fields = @Fields + case when Column_name like '%[_]ID' then 'look_up_value' else Column_name end + ',' + @Return
....