Does anyone know what might cause this chunk of code to sometimes run ok and sometimes throw up error messages?
drop table if exists mapping_rules;
create table mapping_rules
(
claimant_type char(50),
tpd_flag char(10),
tpi_flag char(10),
mapped_claim_type_1_high char(30),
mapped_claim_type_2_detailed char(30),
mapped_claim_type_3_high_ex_recovery char(30),
mapped_claim_type_4_detailed_ex_recovery char(30)
);
bulk insert mapping_rules
from '>>>censored-filename<<<.csv'
with (format = 'CSV', firstrow = 2);
-- convert TRUE and FALSE to 1 and 0
alter table mapping_rules
add tpd_flag_2 int,
tpi_flag_2 int;
update mapping_rules
set tpd_flag_2 = case when (tpd_flag = 'TRUE') then 1 else 0 end,
tpi_flag_2 = case when (tpi_flag = 'TRUE') then 1 else 0 end;
The error message I sometimes see is:
Msg 207, Level 16, State 1, Line 25
Invalid column name 'tpd_flag'.
Msg 207, Level 16, State 1, Line 25
Invalid column name 'tpi_flag'.
Strangely, if I run this code one command at a time, it always works. The errors only appear if I run the whole thing at once.
Also in the code editor, tpd_flag_2
and tpi_flag_2
are red underlined, so presumably there's a problem...? I can't figure out what it is though.
Should I modify this code to populate the mapping_rules
table using code (rather than importing it from an external .csv file) - ie easier to debug?
I've just run it again, and now I have a different error message:
Msg 7301, Level 16, State 2, Line 14
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".
1条答案
按热度按时间zfycwa2u1#
Msg 207, Level 16, State 1, Line 25 Invalid column name 'tpd_flag'. Msg 207, Level 16, State 1, Line 25 Invalid column name 'tpi_flag'.
You will get a compilation error due to invalid column names if the table already exists when the script is run and the existing table doesn't contain the columns in the error message. This is because SQL Server will validate the columns referenced by the
UPDATE
against the existing schema before executing the script that recreates the table and adds additional columns.Assuming you're executing the script with SQLCMD or other SQL Server tool, you can avoid the error by adding a
GO
batch terminator after theALTER TABLE
statement. That way, the all the columns referenced by theUPDATE
will exist at compilation time. In the case of app code, you can execute the portions of the script as separate batches.