Error when copying data from linked (read-only, remote) MS SQL server to local database using SQL Server Express.

I need to create a table from a remote db to a local db. To create the table on my local database, I need to aggregate and copy data from a remote server. I have read-only permission to the remote server. To connect to the remote server, I added a linked server to my local db. This is the query I’m using, but it is throwing an error. I think it can be one of two things:
1.      I’m not calling the server right. Wrong syntax, perhaps.
2.      SQL Server Express does not support copying data from a linked server.

Please advise.

SELECT ManagerName, string_agg (username, ',') AS Team
INTO mylocaldb.dbo.mytable
FROM [].remotedbname.dbo.remotetablename
GROUP BY ManagerName

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Máté FarkasDatabase Developer and AdministratorCommented:
Try this:

SELECT ManagerName, STRING_AGG (UserName, ',') AS Team
FROM OPENROWSET('SQLNCLI', ';Trusted_Connection=Yes;Database=remotedbname', 'SELECT ManagerName, UserName FROM dbo.remotetablename') Q
GROUP BY ManagerName

Open in new window

You may need to enable distributed queries first:
exec sp_configure 'show advanced options', 1
exec sp_configure 'Ad Hoc Distributed Queries', 1

Open in new window

Mark WillsTopic AdvisorCommented:
Have you created the Linked Server ?

It would seem that you are trying to dynamically link to "[]"

But you need to actually tell SQL Server how it is going to connect - did you use SSMS or T-SQL to create the linked server first ?

You can use some of Máté Farkas code above to create the linked server

And need to read :

Now the second part is doing the SELECT ... INTO mylocaldb.dbo.mytable FROM ...

If you are doing a SELECT INTO then the target table must not already exist, and I dont think that is what you really want to do.

To add rows to an existing table, use INSERT and set your localdb first ( ie use mylocaldb)

INSERT mytable (ManagerName, Team)                                              -- create table mytable before you use
SELECT ManagerName, string_agg (username, ',') AS Team
FROM YourLinkedServer.remotedbname.dbo.remotetablename
GROUP BY ManagerName

Notice the 4-part naming convention when referring to a linked server and "YourLinkedServer" is the name given to the linked server when you create the linked server. Using [] as a linked server name just doesnt look right....

I would expect to see something more like a ServerName\InstanceName

But easy to test. Try  SELECT name FROM [].master.sys.databases

Now... While you can use STRING_AGG() function, it is only available from SQL2017

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Máté FarkasDatabase Developer and AdministratorCommented:
My code is only for test.
If you see that it works than you have to run this instead:
SELECT ManagerName, Team
INTO mylocaldb.dbo.mytable
        'SELECT ManagerName, STRING_AGG(UserName, ',') AS Team FROM dbo.remotetablename GROUP BY ManagerName'
) Q

Open in new window

This does not load all rows only aggregated result and it is more optimized. But you have to make sure that your remote server ( also runs SQL Server 2017 (because STRING_AGG aggregate function introduced in 2017).
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Vitor MontalvãoMSSQL Senior EngineerCommented:
The error is about the local database and not the remote one.
Confirm that you are working in the correct database. Also confirm that local table exists.

By the way, if local database is SQL Azure then you cannot use the database name. You must use only schema.tablename or just the tablename if you are working with the default schema.
Mark WillsTopic AdvisorCommented:
Hello calyx_teren
Are you still with us ?
Mark WillsTopic AdvisorCommented:
Well, you cannot create a linked server in Azure either, so, maybe running the query in the wrong window ?

Just for reference, you can copy across Azure DB, just very fiddly :

So, gentlemen - a three way close ?

There is good info in here, so worthwhile keeping.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.