Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:49:45+00:00 2026-06-13T09:49:45+00:00

This has been killing me all day =D. Please help! Scenario 1: Two DB’s

  • 0

This has been killing me all day =D. Please help!

Scenario 1: Two DB’s on the same server (A, B) and A has three tables. Query is executed from B.

Scenario 2: Two DB’s one server is linked to the other. (B has link A) and A has three tables. Query is executed from B.

In scenario 1 (non linked server):

SET @val = ''
SELECT @val = @val + 'Hello, my name is ' + [name] + '!' + CHAR(10) + CHAR(13)
FROM A.sys.tables

Returns:
Hello, my name is Table1!
Hello, my name is Table2!
Hello, my name is Table3!

In scenario 2 (linked server):

SET @val = ''
SELECT @val = @val + 'Hello, my name is ' + [name] + '!' + CHAR(10) + CHAR(13)
FROM LINKED.A.sys.tables

Returns:
Hello, my name is Table3!

Why are these different? If I use openquery() on the linked server the results are the same as scenario 1. I’m trying to avoid using openquery() if possible. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:49:46+00:00Added an answer on June 13, 2026 at 9:49 am

    Unfortunately, this is an unreliable method of string concatenation in SQL Server. I would avoid it in all but the most trivial of cases. There is some more information in this KB: Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location.

    That said, I was able to both duplicate your problem and provide a workaround in my environment:

    SET @val = ''
    SELECT @val = @val + 'Hello, my name is ' + replace([name], '', '') + '!' + CHAR(10) + CHAR(13)
    FROM LINKED.A.sys.tables
    

    Notice that I’ve added an empty replace function to the expression. Though it should do nothing to the output, it does add a local “compute scalar” step to the query plan. This seems to pull back all of the data from the name column to then be processed locally rather than just letting the remote query return what it thinks is needed.

    I’m not sure if there’s a better function to use other than a replace with empty arguments. Perhaps a double reverse or something. Just be sure to cast to a max datatype if necessary as the documentation states.

    UPDATE

    Simply declaring @var as varchar(max) rather than nvarchar(max) clears up the problem, as it then brings back the entire name column (type sysname — or nvarchar(128) — I believe) for local processing just like the replace function did. I cannot pretend to know which combination of linked server settings and implicit casting causes this to come up. Hopefully someone with more knowledge in this area can chime-in!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been killing me all day, but I am not sure how to
This has been killing me for two days now. I have a main Activity
This has been killing me for two days now. I have a main Activity
This problem has been killing me for the entire day. I have a client
Thanks for the help in advance... this one has been killing me for the
This has been bugging me for more than two days now, so i thought
This has been a head-banger for me. I have looked at all of the
Sorry for the simple question, but this has been killing me and I can't
This has been really killing me for the last couple of days now. I
This problem has been KILLING me. I've been working on this app for 8

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.