Solved

error trap in sql server 2008 r2

Posted on 2014-02-28
2
309 Views
Last Modified: 2014-02-28
I am importing data from another server into an archive database.  I'm doing it the "easy" way by executing a dynamic sql statement as such:

select @qst = 'SELECT * INTO ' + @schema + '.' + @table + ' FROM AS400.TMS.' + @schema + '.' + @table
	
execute(@qst)
	
select @err = @@ERROR

Open in new window



I'm doing this in a loop to move all the tables in a particular library (schema).  Here is the problem:

When the query fails it bombs out of the batch entirely, such as when the table has no columns.  I'm trying to catch the error and then continue on.  Any ideas on how to accomplish this?
0
Comment
Question by:dhenderson12
2 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 250 total points
ID: 39895935
Use BEGIN/TRY/CATCH new SQL functionality:

http://technet.microsoft.com/en-us/library/ms175976.aspx
0
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 250 total points
ID: 39896105
Create a table to catch the error and use this:
create table ErrTable(ID int identity, table_name varchar(100), err varchar(8000))
GO

-- LOOP here
select @qst = 'SELECT * INTO ' + @schema + '.' + @table + ' FROM AS400.TMS.' + @schema + '.' + @table


BEGIN TRY	

	execute(@qst)

END TRY
BEGIN CATCH

	INSERT INTO ErrTable(table_name, err)
	select  @schema + '.' + @table, @@ERROR

END CATCH

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question