?
Solved

error trap in sql server 2008 r2

Posted on 2014-02-28
2
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

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

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

Assisted Solution

by:Zberteoc
Zberteoc earned 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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