1

I have an existing database that I'm trying to set up as a database project. I'm not able to get the following statement to build:

CREATE view [dbo].[VW_SOMEVIEW] as 
SELECT * FROM OPENQUERY ([SERVER2],'
SELECT SESSION_NAME,
 SESSION_ROWS,
 ACTUAL_START
FROM [DATABASE2].[dbo].[TABLE2] (NOLOCK)
WHERE SUBJECT_AREA = ''XYZ''
order by ACTUAL_START '
)

I'm getting the error:

SQL71501: View [dbo].[VW_SOMEVIEW] has an unresolved reference to object [SERVER2].

When I use the variable name for the server instead of the actual server name, I get:

SQL46010: Incorrect syntax near $(LinkedServer).

Both the server containing the view and the server being referenced are on SQL Server 2016, and I'm using Visual Studio 2017.

I've added the .dacpac file from SERVER2 in References, and I've tried using variables for the server and database name, but nothing works. What am I missing here?

MDCCL
8,5303 gold badges32 silver badges63 bronze badges
asked Oct 9, 2018 at 16:46
1
  • I have both master and the .dacpac for the referenced database listed in the database project references. I am referencing the master .dacpac that was supplied in Visual Studio. I added this to the project by right-clicking on References -> Add Database References -> System Database -> master. Do I need to add a reference for the master database of the server being referenced? Commented Oct 9, 2018 at 17:28

1 Answer 1

1

Have you tried this?

CREATE VIEW dbo.VW_SOMEVIEW AS
SELECT *
FROM OPENQUERY (
 [$(LinkedServer)] /*we need the [] brachets to make this parse ok in VS*/
 ,'SELECT SESSION_NAME,SESSION_ROWS,ACTUAL_START
 FROM DATABASE2.dbo.TABLE2 (NOLOCK)
 WHERE SUBJECT_AREA = ''XYZ''
 ORDER BY ACTUAL_START'
 )
answered May 4, 2021 at 16:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.