【发布时间】:2011-09-02 16:43:55
【问题描述】:
我正在尝试使用 try-catch 捕获 SQL 查询(而不是存储过程)中的错误。
由于某种原因,这没有处理我的错误,我仍然得到:
消息 213,第 16 级,状态 1,第 29 行 列名或提供的值的数量与表定义不匹配。
有什么帮助吗?
begin try
create table #temp_hierarchy
(temp_gl_number varchar(50)
,temp_store_location varchar(255)
,temp_store_key varchar(50)
,temp_serving_dc varchar(50)
,temp_exploris_db varchar(50)
,temp_dc_account varchar(50)
,temp_store_type varchar(50)
,temp_dvp_ops varchar(50)
,temp_rdo varchar(50)
,temp_team varchar(50)
,temp_dvp_sales varchar(50)
,temp_rds varchar(50)
,temp_closed varchar(50)
,temp_open_date varchar(50)
,temp_close_date varchar(50)
,temp_store_manager varchar(250)
,temp_sales_teammate varchar(250)
,temp_machine_shop varchar(50)
,temp_address varchar(250)
,temp_city varchar(50)
,temp_state varchar(50)
,temp_zip varchar(50)
,temp_phone varchar(50)
,temp_fax varchar(50))
insert into #temp_hierarchy
select *
from OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\SQL_DATA_REPORTING\8-31-11 Store Hierarchy.xlsx;HDR=YES',
'SELECT * FROM [Master List$]');
truncate table tbl_hierarchy
insert into tbl_hierarchy
select *
from #temp_hierarchy
where temp_gl_number is not null
and temp_gl_number <> 'GLID'
select @@ROWCOUNT + ' Records sucessfully imported'
end try
begin catch
select 'ERROR: ' & ERROR_NUMBER() + '. Unable to import records, existing data was not lost.'
end catch;
go
【问题讨论】:
-
我的猜测是 tbl_hierarchy 和 #temp_hierarchy 有不同的列定义。
-
您可以使用
select * INTO T from OPENROWSET...查看用于临时表的正确表定义。
标签: sql sql-server-2008 error-handling try-catch