pchak
Starting Member
2 Posts |
Posted - 2009-08-03 : 22:37:38
|
I am using dynamic sql to create some queries to hit a number of remote servers to replicate data. It works fine (no issues w/syntax), and is more efficient than any other segment of the process. It returns 4-7 tables with different schemas from 15 remote locations, and is run hourly as part of a DTS package.Dynamic SQL being built:"INSERT INTO SlitRollDetail SELECT '00' as Site, remote.*, getdate() as ReplicationTimeStamp FROM OPENROWSET('SQLOLEDB',servername;'uid';pwd,'SELECT [LotID],[LineNumber],[SequenceNumber],[RunNumber],[RollNumber],[Item],[ItemDesc],[RollWidth],[RollWidthUOM],[RollLength],[RollLengthUOM],[SalesOrder],[SOLineNumber],[TappiID],[Completed],[DispositionCode],[Splices],[Disposition],[Created],[DeliveryDate],[DeliveryPref],[ExtraRunFlag] FROM remoteservername.dbo.SlitRollDetail where Created > ''07/24/09 9:00:00 AM'' ORDER BY Created') as remote"This is sufficent while working. However, a potential failure would be a server/db being down, WAN connection, etc. I would expect to get an error code(pertinent part of sproc):BEGINSET NOCOUNT ON;--exec(@RowsetCode) --THIS DOES NOT RETURN @@ERROR eitherexec @err = sp_executesql @RowsetCode --SELECT @err = @@errorselect 'return: ' = @@error --debug REGARDLESS-THIS SHOWS 0 on errorDECLARE @currtimeholder datetime set @currtimeholder = current_timestamp ----error handling -WANT THIS TO RUNSELECT @err = @@errorIf @err <> 0BEGINEXECUTE WriteLogRecord @site, @db, @table, @currtimeholder , @err , 'issue with getting result from remote server'return @err --leaveEND(otherwise other processing happens)If I mimic a failure " such as inserting a bad password, instead of a return code result when executing the sproc (and in the debug select statements, I get a run-time message:(1 row(s) affected)Msg 18456, Level 14, State 1, Line 1Login failed for user 'fropro'.The sproc stops executing at this point.I thought that sp_executesql avoids the scope issue with dynamic sql. I just want to capture the return code that something happend, rather than having a complete failure, which is presenting the rest of my flow from occurring.I've considered the architectural cumbersomeness, anti-practice, sql injection, etc. of how this is designed, but time and current architecture dictate this method of retrieval. I would have liked to set up hourly SQL replication, but the schemas being hit do not allow for it.Thanks! |
|