Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Re-run dynamic openquery

Posted on 2010-11-12
6
Medium Priority
?
855 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 500 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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