Solved

Re-run dynamic openquery

Posted on 2010-11-12
6
836 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
  • 4
  • 2
6 Comments
 

Author Comment

by:Airmaster
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Airmaster
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help.  I did something very similar with your assistance.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now