具有可变列名的动态更新语句 [英] Dynamic update statement with variable column names

查看:25
本文介绍了具有可变列名的动态更新语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望在多个 SQL Server 数据库中进行更新,以将某个表中的所有 NULL 值更改为空字符串而不是 NULL.我们可能会在数百个数据库中执行此操作.表名将始终相同,但列名根据前端应用程序的配置方式是可变的(不要判断......我没有创建这个系统).

We're looking to do an update in several SQL Server databases to change all NULL values in a certain table to be empty strings instead of NULL. We're potentially going to be doing this across hundreds of databases. The table name will always be the same, but the column names are variable based on how the front-end application is configured (don't judge... I didn't create this system).

有没有办法在不提前知道列名的情况下对所有这些列进行更新?

Is there a way to do an update on all of these columns without knowing the column names ahead of time?

推荐答案

动态sql中可以传入列名:

You can pass the name of the column in dynamic sql:

declare @sql nvarchar (1000);
set @sql = N'update table set ' + @column_name + '= ''''';

exec sp_executesql @sql;

这篇关于具有可变列名的动态更新语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆