syntax error get data from linked server

  • This query runs but doesn't get any info. I think it's something in the syntax, but can't find the issue. If I query a table using Linked Server I can see the data I need.

    DECLARE @SQL VARCHAR(MAX) = ''
    ;
    SELECT @SQL += REPLACE(REPLACE('
    RAISERROR("-----------------------------------",0,0) WITH NOWAIT;
    RAISERROR("Processing <<TableName>>",0,0) WITH NOWAIT;
    INSERT INTO BC_Data_Capture_Staging
    ([timestamp],equipid,speed)
    SELECT
    [timestamp] = stamp_time
    ,equipid = SUBSTRING(REPLACE("<<TableName>>","line",""),2,128)
    ,speed

    FROM linksrv.DB.dbo.[<<TableName>>],
    LastDataCaptured
    where stamp_time > BC_Last_Capture_Ifacts3
    ORDER BY Stamp_time DESC
    ;' ,'"','''')
    ,'<<TableName>>',obj.name)
    FROM sys.objects obj
    WHERE obj.name LIKE 'F[0-9][0-9][0-9]%'
    AND type = 'U'
    AND schema_id = 1
    ;

    EXEC(@SQL)
    ;
  • Assuming dbo.BC_Data_Capture_Staging exists in the database in which you execute this statement:

    1. Comment out or remove the INSERT lines so you are just selecting & debug:

      1. print instead of executing
      2. copy & paste the sql & see if it parses
      3. If it parses, execute & see interpret what you get.

    2. If that works, uncomment/reinsert the INSERT lines & repeat.
  • I tried those steps, but the Print didn't give me any output.

     

     

  • If print didn't return anything, that tells you you're getting a null when concatenating. Since you're only concatenating obj.name, that seems to imply that the following returns nothing in the server/database in which you are trying to execute that script. Does this return anything?

    SELECT *
    FROM sys.objects obj
    WHERE obj.name LIKE 'F[0-9][0-9][0-9]%'
    AND type = 'U'
    AND schema_id = 1

     

  • yes when i run on the linked server it returns the 90 tables i'm trying to query.

  • But your original query isn't querying  sys.objects on the linked server, it's querying it on the server trying to use that linked server remotely.

    If those tables you need in sys.objects are on the linked server and not the querying server where you're trying to insert, then you need to reference sys.objects on the linked server in your dynamic sql.

     

     

  • ahh good catch

    Thanks!!

  • This was removed by the editor as SPAM

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply