I have a procedure which I am looping BULK INSERT statement no of times with no of files available.
Now I want to handle the errors during bulk insert statement. suppose one table is not inserted due
to constraint violation, it should raise a error and write it into a log file and continue to insert
next table. If another table gets an error then the error should go same log file and continue to
insert the next tables .I will be thankful for any hint.
Is something I have to pass 'log file' as output parameter to the procedure.
CREATE PROCEDURE load_bulk_insert @XMLDOC VARCHAR(8000)
AS
DECLARE @IDOC INT
DECLARE @TMPXML VARCHAR(2000)
SET @TMPXML='<ROOT>
<Hierarchy name="OWNER">
<Levels>
<Level name="STATE" filename="state.txt" path="c:\temp" />
<Level name="DISTRICT" filename="district.txt" path="c:\temp" />
<Level name="REGION" filename="region.txt" path="c:\temp" />
<Level name="SCHOOLS" filename="schools.txt" path="c:\temp" />
<Level name="PROGRAM" filename="program.txt" path="c:\temp" />
</Levels>
</Hierarchy>
</ROOT>'
EXEC SP_XML_PREPAREDOCUMENT @idoc OUTPUT, @TMPXML
declare @sql varchar(1024),@db_table varchar(255),@data_file varchar(255),@path varchar(255)
declare c1 cursor for
select *
from openxml( @idoc, '/ROOT/Hierarchy/Levels/Level')
with ( name varchar(255) '@name', filename varchar(255) '@filename',fpath varchar (255) '@path' )
open c1
fetch next from c1 into @db_table,@data_file,@path
WHILE (@@FETCH_STATUS <> -1)
begin
IF (@@FETCH_STATUS <> -2)
begin
set @sql= "BULK INSERT "+@db_table+" FROM '"+@path+"\"+@data_file+"' WITH (FIELDTERMINATOR=',', ROWTERMINATOR = '\n')"
Execute (@sql)
end
fetch next from c1 into @db_table,@data_file,@path
end
close c1
deallocate c1
EXEC sp_xml_removedocument @idoc
GO
Now I want to handle the errors during bulk insert statement. suppose one table is not inserted due
to constraint violation, it should raise a error and write it into a log file and continue to insert
next table. If another table gets an error then the error should go same log file and continue to
insert the next tables .I will be thankful for any hint.
Is something I have to pass 'log file' as output parameter to the procedure.
CREATE PROCEDURE load_bulk_insert @XMLDOC VARCHAR(8000)
AS
DECLARE @IDOC INT
DECLARE @TMPXML VARCHAR(2000)
SET @TMPXML='<ROOT>
<Hierarchy name="OWNER">
<Levels>
<Level name="STATE" filename="state.txt" path="c:\temp" />
<Level name="DISTRICT" filename="district.txt" path="c:\temp" />
<Level name="REGION" filename="region.txt" path="c:\temp" />
<Level name="SCHOOLS" filename="schools.txt" path="c:\temp" />
<Level name="PROGRAM" filename="program.txt" path="c:\temp" />
</Levels>
</Hierarchy>
</ROOT>'
EXEC SP_XML_PREPAREDOCUMENT @idoc OUTPUT, @TMPXML
declare @sql varchar(1024),@db_table varchar(255),@data_file varchar(255),@path varchar(255)
declare c1 cursor for
select *
from openxml( @idoc, '/ROOT/Hierarchy/Levels/Level')
with ( name varchar(255) '@name', filename varchar(255) '@filename',fpath varchar (255) '@path' )
open c1
fetch next from c1 into @db_table,@data_file,@path
WHILE (@@FETCH_STATUS <> -1)
begin
IF (@@FETCH_STATUS <> -2)
begin
set @sql= "BULK INSERT "+@db_table+" FROM '"+@path+"\"+@data_file+"' WITH (FIELDTERMINATOR=',', ROWTERMINATOR = '\n')"
Execute (@sql)
end
fetch next from c1 into @db_table,@data_file,@path
end
close c1
deallocate c1
EXEC sp_xml_removedocument @idoc
GO