Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rubs_65
Posting Yak Master
144 Posts |
Posted - 2003-12-17 : 10:48:18
|
| Hi,We are writing a procedure in which we use bcp to archive the data out of tables and then delete data from these tables. At any point if we get error we want procedure to return with failure status. If we get error during bcp operation then it will just return back and leave the files on hard disk and if we get error during delete operation then it will rollback the transaction and return. Now we are trying to simulate the failure that bcp fails with insufficient space on hard disk and I can see that it is not returning immediately when it encountered the first error and keep trying to archive the tables with 0 bytes file on hard disk for each table.....How to return immediately with first error.Thanks--HarvinderFollowing is the sample code of procedure:create procedure bcpout1 (@interval int,@status int output)asset nocount ondeclare @sqlstmt nvarchar(1000)declare @sql1 varchar(8000)declare @var1 varchar(100)declare @result intdeclare c1 cursor for select distinct col_name from t_table1 where interval = @intervalopen c1fetch next from c1 into @var1while (@@fetch_status = 0)beginSET @SQLStmt = N'IF EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(''bcpview'')) DROP view bcpview' EXEC sp_executesql @SQLStmtselect @str1 = N'create view bcpview as SELECT top 100 percent pv.* FROM db1..' + @var11 + ' pv inner join db1..t_table1 au on pv.col2=au.col2 where au.interval=' + cast (@interval as char (5))'exec (@str1)select @sql1 = 'bcp "db1..bcpview" out c:\temp\' + @tab1 + cast (@interval as char (5)) + '.txt -c -Sserver1'exec @result = master.dbo.xp_cmdshell @sql1if (@result <> 0)beginclose c1deallocate c1set @status = -99returnendfetch next from c1 into @var1endclose c1deallocate c1declare c1 cursor for select distinct col_name from t_table1 where interval = @intervalbegin tranopen c1fetch next from c1 into @var1while (@@fetch_status = 0)beginselect @str1 = N'delete FROM db1..' + @var1 + ' where col2 in (select col2 from db1..t_table1 au where au.interval=' + cast (@interval as char (5)) + ' and view =' + + cast (@var1 as char (5)) +')'exec (@str1)if (@@error <> 0)beginselect @status = -99rollback tranclose c1deallocate c1returnendfetch next from c1 into @var1endclose c1deallocate c1commit transelect @status = 0 |
|
|
|
|
|