Solved

Re-run dynamic openquery

Posted on 2010-11-12
6
852 Views
Last Modified: 2012-05-10
In SQL Server 2008, I am grabbing data from another database.  The interface for that other database is through OPENQUERY.

Occasionally, some of the calls fail.  If they do, I would like to be able to re-run that query, so that I can grab the complete set of data and store the results in another table.

I am using dynamic SQL to construct the query, which I store in @OpenQueryString, and then I execute it via "exec (@OpenQueryString)"

Can someone tell me how I would accomplish this?  I suppose I could try a TRY-CATCH, but that doesn't seem like it is the right solution.  What I think I would want is a WHILE LOOP that would loop if there was an error condition (at least try a few times before bugging out).

0
Comment
Question by:Airmaster
[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
  • 4
  • 2
6 Comments
 

Author Comment

by:Airmaster
ID: 34124430
I tired the Try-Catch, but got the error  3930

"The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction."
0
 
LVL 1

Expert Comment

by:bclevlnd
ID: 34124510
try-catch will only stop on errors/warnings that have a severity <= 10 or > 20 and do not break the DB connection.  If you are returning the value into a variable, a while loop that is checking that variable for a specific value may work.

Like:
declare @MyVar varchar(20)
set @MyVar=''
while (@MyVar = '')
begin

0
 

Author Comment

by:Airmaster
ID: 34140017
What I am returning not a value, but ultimately something that will be inserted into a table.  In other words, I execute the query, and it may return several rows and columns of data that I then insert into a table.

Maybe something like this would work if it didn't return any rows of data.  I don't see anything that I could use to determine if the query failed.

Here is an example query, which I stores in @strSQL:

SELECT   * FROM OPENQUERY("SERVER",'SET SamplingMode = calculated, CalculationMode=Average, intervalmilliseconds = 1d,
StartTime = 1-2-2007, EndTime = 09/15/10 Select tagname, timestamp, value, quality
from ihrawdata
where tagname = Tag.Name')

This query is part of a stored procedure (procDataAverage), which generates the query via dynamics sql, I execute it via exec (@strSQL).

I then save the data into another table by the following

INSERT INTO tblResults (tagname, TagDate, value, quality)
  EXEC procDataAverage ....variables to be passed into procedure.....

So, if the OpenQuery fails, then nothing is stored in tblResults.  Note that I execute this multiple times, so tblResults fills up with a lot of data.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Airmaster
ID: 34140416
Here is the error I get:

Msg 7320, Level 16, State 2, Line 1
Cannot execute the query "SET SamplingMode = calculated, CalculationMode=Average, intervalmilliseconds = 1d,
StartTime = 1-2-2007, EndTime = 09/15/10 Select tagname, timestamp, value, quality
from ihrawdata
where tagname = Tag.Name" against OLE DB provider "IhOLEDB.iHistorian" for linked server "SERVER".

What I would like to do is to call the routine until it is successful.
0
 
LVL 1

Accepted Solution

by:
bclevlnd earned 125 total points
ID: 34140591
You could add output inserted.tagname (or any of the fields you are inserting into tblResults) into a @TempTable and then select count(*) from @TempTable to set an @Variable.  You could then have your while statement check for @Variable = 0.  This would work only if you know you should always receive a value back from you openquery.
0
 

Author Closing Comment

by:Airmaster
ID: 34292917
Thanks for the help.  I did something very similar with your assistance.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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