Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

If exists update table (still errors)

$
0
0

I'm trying the following code to update a column.  My need is that it will not error, so I can run 200 of these at one time.

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'SOMETABLE' AND COLUMN_NAME = 'SOMECOLUMN') 

BEGIN UPDATE A SET A.SOMECOLUMN = NULL FROM stg_CPIData_Mart_Load as A 
WHERE A.SOMECOLUMN = 'NULL' or A.SOMECOLUMN = '' END;

Although it's not working.  If the column is not found I get an error:

Msg 207, Level 16, State 1, Line 1
Invalid column name 'SOMECOLUMN'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'SOMECOLUMN'.


-Al H


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>