Current working on a project where I have to update the data on 85 tables replacing the current empty string to a NULL value. it is a simple SQL query for this but since it is a sensitive environment, if anything goes wrong, we need to revert this.
The main idea was to create a table to save the data to roll back. but I am trying to avoid creating 85 tables.
I will give a smaller example:
there is 4 tables
------------------------------------
| airplane |
------------------------------------
| air_ID | color | tail_number |
------------------------------------
| 1 | red | |
| 2 | green | |
| 3 | black | 21AF |
------------------------------------
------------------------------------
| bus |
------------------------------------
| bus_ID | color | tag_number |
------------------------------------
| 1 | red | AAY-464 |
| 2 | green | |
| 3 | black | |
------------------------------------
------------------------------------
| train |
------------------------------------
| tr_ID | color | designated_name |
------------------------------------
| 1 | red | 99212 |
| 2 | green | |
| 3 | black | |
------------------------------------
------------------------------------
| Cruise_Ship |
------------------------------------
| sea_ID | color | hull_number |
------------------------------------
| 1 | red | |
| 2 | green | MAGDA |
| 3 | black | |
------------------------------------
So I created a temp table with the data
-------------------------------------------------
| update_table |
-------------------------------------------------
| table_name | ID_colname | ID | col_name |
-------------------------------------------------
| airplane | air_ID | 1 | tail_number |
| airplane | air_ID | 2 | tail_number |
| bus | bus_ID | 2 | tag_number |
| bus | bus_ID | 3 | tag_number |
| train | tr_ID | 2 |designated_name|
| train | tr_ID | 3 |designated_name|
|Cruise_Ship | sea_ID | 1 | hull_number |
|Cruise_Ship | sea_ID | 3 | hull_number |
-------------------------------------------------
With this table I was trying to generate a dynamic SQL to update all the tables with one call
SET @SQLString = N'UPDATE @table
SET @value = '+ @empty +'
where @key = @id';
SET @ParmDefinition = N'@table nvarchar(max),
@value nvarchar(max) ,
@key nvarchar(max) ,
@id int';
DECLARE @table nvarchar(255)
DECLARE @value nvarchar(255)
DECLARE @key nvarchar(255)
DECLARE @id int
select @table = table_name, @id = ID, @key = ID_colname , @value = col_name from update_table
EXECUTE sp_executesql
@SQLString
,@ParmDefinition
,@table
,@value
,@key
,@id
;
But this is not working, anyone has a idea on how to improve this query?
This is a high profile environment and the developers are not the ones executing the code, so it need to be customer proof. The code is ran overnight to not disturb daytime operations.