Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716

    Unanswered: Cursors and Linked servers don't seem to like each other.

    ...well...at least the cursor seems to despise the linked server....

    I have a cursor (yeah, I know...not MY cursor, just an inherited one) and it works just peachy when accessing the local database, but when I add the server identifier for a linked server, the compiler complains when I try to execute it. Works fine otherwise.

    The cursor references a function on the remote server that returns a table. The complaint in the compiler is that there is a syntax error, not a specific issue with the linked server aspect....but it looks like it is expecting the function to be a table reference, so it complains about the leading parenthesis.
    This workie:
    Code:
    SELECT PortfolioID, isTestType
    			FROM Index.dbo.fn_CSVList_FilteredPortfolioTable('100,125')
    this no workie:
    Code:
    SELECT PortfolioID, isTestType
    			FROM L_AP.Index.dbo.fn_CSVList_FilteredPortfolioTable('100,125')
    L_AP is the linked server's alias.
    what I get back from the compiler when I try to execute is
    Code:
    Server: Msg 170, Level 15, State 31, Line 2
    Line 3: Incorrect syntax near '('.
    I checked (and am still checking) BOL but have found no info on the issue so far. Any thoughts??? I'm hoping maybe it's just a typing error, but I don't see it.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Remote Procedure Calls?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    what happens when you reference the linked server outside of the cursor?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    It works just fine outside the cursor. We use the linked servers every day for production processes and they work just fine for all those procedures, In fact, elsewhere in this stored procedure (which contains the cursor) the linked server is referenced and there is no problem with that reference's execution. I think it's related to the function call...I've seen some instances before (that were mostly syntax generated errors) that made the compiler think that it was a table reference rather than a function call. For instance, if the "dbo" (database owner) is left off the function call, sometimes it is interpreted as a table reference.

    Maybe none of that is related, but it "feels" similar.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  5. #5
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    Just checked also, thought it might be a security issue, but the userid set up to access the remote server is set up in the remote server as a "database owner", so I think it should be able to "see" (and execute) everything in the remote server.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  6. #6
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    and we are certain that this takes one argument ('100,125'). I am not a big users of functions, but does the function take a single string CSV integers? Or should this be 2 arguments?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  7. #7
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    Yep, one string containing a CSV list of integers.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  8. #8
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Just to clarify, does the function call itself work at all without the cursor? if it does, you could dump the resultset into a temp table to process at a later date.

  9. #9
    Join Date
    Nov 2008
    Posts
    23
    --scalar function
    select * from OpenQuery(LinkedServerName,'select AEPns.dbo.jk_func2(1,2)')

    --table valued function
    select * from OpenQuery(LinkedServerName,'select * from AEPns.dbo.jk_func1(1,2)')

  10. #10
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    Nice thought, MC, yeah, it works just fine anywhere else, and with linked servers galore. If the OpenQuery doesn't help me I'll check out the temp table idea....I was just trying to figure out why it doesn't work, when I can't find any "advertised" reason for it not to work. Tilting and windmills, I suppose.

    Thanks for the post, Kjambu...I'll give that a try. We are trying to use OpenQuery at other times also, I'm just not familiar with the underlying functionality, so wanted to read up on it before we start using it larger-scale.

    I'll give it a try in this case, however. I'll post back with results.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  11. #11
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    Well, I don't think the OPENQUERY is gonna cut it for me, because (bad problem description, sorry...) I need to pass a variable list of the CSV numeric values. Tried that
    Code:
    DECLARE PortfolioCursor  CURSOR FOR
    			SELECT PortfolioID, isTestType
    			FROM OpenQuery(L_AP, 'SELECT * FROM L_AP.IBDIndex.dbo.fn_CSVList_FilteredPortfolioTable(@PortfolioList)')
    and got
    Code:
    Server: Msg 8180, Level 16, State 1, Procedure Index_AP_CloseIndex_Match, Line 88
    Statement(s) could not be prepared.
    Server: Msg 137, Level 15, State 1, Procedure Index_AP_CloseIndex_Match, Line 88
    Must declare the variable '@PortfolioList'.
    [OLE/DB provider returned message: Deferred prepare could not be completed.]
    Since OPENQUERY doesn't accept variables as arguments, I can't even build the string containing the query and pass it like that.

    I guess the local table might be my best option at this point...I'll give that a try.
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  12. #12
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Code:
    SELECT name, is_rpc_enabled FROM sys.servers

    If not enabled, try enabling using sp_serveroption.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  13. #13
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Cannot call a function over a linked server, wrap it in a sproc, and call the sproc remotely.

  14. #14
    Join Date
    Feb 2004
    Location
    Burbank & Santa Cruz de la Sierra
    Posts
    3,716
    PMASchmed - got any "official" link to BOL or whatever that supports your claim? Obviously it is likely the case, because I certainly am unable to do it, but I can't find anything in my searching so far that documents that position. I did some other testing though, along those lines, and do find that when I call a different function from the remote database, the compiler complains that I have too many prefixes on the object. Says the maximum is two. Interesting that. I'll have to go read up on UDF's and see if the documentation there says anything about linked servers and function calls.

    Thought about putting it inside an sproc on the remote server, but for my purposes, that complicates things more than they need to be complicated, as I would still have to pass the table (output of the function call) back via the remote sproc. So I thought the local table would be easier....but also got a failure when I tried to load the local table using the remote function call (obviously related to PMASchmed's input).

    So long story short, I just cheated and created a local version of the function and had it go after the remote table inside of it.

    I feel dirty, but it works.

    Thanks all for your valuable time and input!
    aka "Paul"
    Non est ei similis.

    I just read "100 Things To Do Before You Die". I was surprised that "Yell for help!!" wasn't one of them

  15. #15
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Found something for SQL 2000

    http://support.microsoft.com/kb/319138

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •