我有多个具有匹配列名的表,并希望生成一个更新语句,影响4个特定表中的1列,如下所示:
UPDATE table1, table2, table3, table4
SET table1.column_1 = 'value', table2.column_1 = 'value', table3.column_1 = 'value', table4.column_1 = 'value'
WHERE table1.column_id = 'value' OR table2.column_id = 'value' OR table3.column_id = 'value' OR table4.column_id = 'value'
这不起作用,我用Google搜索了几个小时的答案,我发现了https://stackoverflow.com/a/19797529/1824324:
Select 'UPDATE ' + TABLE_NAME + ' SET CreatedDateTime = ''<<New Value>>'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'CreatedDateTime'
稍作调整:
SELECT 'UPDATE ' + TABLE_NAME + ' SET column_1= ''Value'' where column_id=''value'''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'column_1'
现在我的问题是我不希望只更新table1,table2,table3和table4的每个具有该特定列的表。我如何剪切&#34; FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME =&#39; column_1&#39;&#34;并用4个表替换?
答案 0 :(得分:2)
我相信你正在谈论MySQL,因为你正在使用的表。
只需过滤TABLE_NAME列:
SELECT 'UPDATE ' + TABLE_NAME + ' SET column_1= ''Value'' where column_id=''value'''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'column_1'
AND TABLE_NAME IN ('table1', 'table2')