# LInked Server in Reports

**URL:** <https://forum.inductiveautomation.com/t/linked-server-in-reports/30740>\
**Category:** Ignition\
**Created:** [December 16, 2019, 8:04pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740 "2019-12-16T20:04:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajrowe](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ajrowe](https://forum.inductiveautomation.com/u/ajrowe)\
**Post date:** [December 16, 2019, 8:04pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/1 "2019-12-16T20:04:52Z")

</div>

I have a complex query that uses a reference to a linked server and database. I successfully use the query in standard table object on a display.

I’m trying now to make a report using the same query and am having problems.

The query is in the form:

**EXEC(’ use Database;**  
**long/complex query code . . .**  
**’) at [LinkedServer]**

If I hard-code real names for **Database** and **LinkedServer** , the report works fine. But that’s not an option when deploying the report.

In the original query, I use client tags to substitute **Database** and **LinkedServer**.

My 1st try in Reports was to use a SQL Query for the data and use parameters for **Database** and **LinkedServer**. The system would recognized it and would create a parameter input when I replaced **Database** with a question mark. It would never recognize and create a parameter input when I replaced **LinkedServer** with a question mark.

So, next I tried using a Basic SQL Query for the data, because it supports references directly in the query using the brace characters { }.

Using the Basic SQL Query, I copied in the exact query (with the curly-braced tag references) into the query definition. I don’t get any data in the preview and get the following error: **WARN: Simple Query could not be completed. Could not find server ‘null’ in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp\_addlinkedserver to add the server to sys.servers.**

Is there a particular method I need to use to be able to reference a linked server and database in the Report module?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 16, 2019, 8:55pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/2 "2019-12-16T20:55:02Z")

</div>

> [@ajrowe](#):
>
> I use client tags

That's the root of the problem. Reports execute on the gateway - so while using a Basic SQL Query _is_ the correct way to specify your dynamic query, you _cannot_ use client tags as the source of the values. Use parameters, and wherever you're calling the query from, either manually specify the values, or use "real" tags to store persistent parameter values.

---

<div class="post-metadata">

**Author:** ![ajrowe](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ajrowe](https://forum.inductiveautomation.com/u/ajrowe)\
**Post date:** [December 16, 2019, 9:10pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/3 "2019-12-16T21:10:56Z")

</div>

OK, thanks. That makes sense.

I can’t use the 2nd method I tried, which leaves me with my original problem of how do I get it to recognize the 2nd question mark when I create the query so that this:  
EXEC(’ use **Database;**  
long/complex query code . .  
’) at [**LinkedServer]**

becomes this:  
EXEC(’ use **?** ;  
long/complex query code . . .  
’) at [**?**]

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 16, 2019, 9:16pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/4 "2019-12-16T21:16:08Z")

</div>

You can’t. Parameterized statements are strictly defined (to prevent SQL injection, for one reason) and so you can’t replace the ‘structure’ of a query with a parameter. Using a ‘Basic SQL Query’ _is_ the right way to do this; I suspect the warning you were getting about null being invalid is because of the client tag issue. Try the same approach with real system level tags, even with fixed values, to see if you get the same behavior.

---

<div class="post-metadata">

**Author:** ![ajrowe](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ajrowe](https://forum.inductiveautomation.com/u/ajrowe)\
**Post date:** [December 16, 2019, 9:40pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/5 "2019-12-16T21:40:27Z")

</div>

This is a shared enterprise application, whose information is made specific to a user at a particular location with client tags. I don’t have a connection to each database directly, I have a connection to a central server that has linked servers to each location’s database. if I can’t use client tags, then multiple concurrent users would step on each other’s reports.

It sounds like to me that without making individual reports for each location, I’m not able to use reports in this application.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 16, 2019, 10:06pm UTC](https://forum.inductiveautomation.com/t/linked-server-in-reports/30740/6 "2019-12-16T22:06:22Z")

</div>

Where are you executing these reports from? You can pass whatever values you want to use as report parameters via scripting function [https://docs.inductiveautomation.com/display/DOC80/system.report.executeAndDistribute](https://docs.inductiveautomation.com/display/DOC80/system.report.executeAndDistribute). If you need to run a scheduled report, then you could maintain a mapping of usernames : databases and run through that list, passing them through the scripting function.
