user2943399
user2943399

Reputation: 31

MS SQL correlated sub queries, Unable to write outer query

I need to write a sub query and it doesn’t allow me to continue. What is the wrong and how to achieve this?

Error: Incorrect syntax near 'A'.

SELECT * FROM 
(
    SELECT ID, 
        CAST([dbo].[SC_GetVersionedFieldValue](ID, '{25BED78C-4957-4165-998A-CA1B52F67497}') AS NVARCHAR(MAX)) AS '__Created',
        CAST([dbo].[SC_GetVersionedFieldValue](ID, '{D9CF14B1-FA16-4BA6-9288-E8A174D4D522}') AS NVARCHAR(MAX)) AS '__Updated'
    FROM [dbo].[Items] WHERE [TemplateID] = '{8CB33CED-3E7E-4263-AF97-71B22338D9C7}'        
) mytable A WHERE exists (SELECT ID FROM mytable B WHERE A.ID = B.ID) ;

Upvotes: 0

Views: 42

Answers (2)

Robert
Robert

Reputation: 25753

You should remove mytable and as @harsh said remove comma. Try below code.

SELECT * FROM 
(
    SELECT ID, 
        CAST([dbo].[SC_GetVersionedFieldValue](ID, '{25BED78C-4957-4165-998A-CA1B52F67497}') AS NVARCHAR(MAX)) AS '__Created',
        CAST([dbo].[SC_GetVersionedFieldValue](ID, '{D9CF14B1-FA16-4BA6-9288-E8A174D4D522}') AS NVARCHAR(MAX)) AS '__Updated'
    FROM [dbo].[Items] WHERE [TemplateID] = '{8CB33CED-3E7E-4263-AF97-71B22338D9C7}'        
) A 
    WHERE exists (SELECT ID FROM mytable B WHER A.ID = B.ID) ;

Upvotes: 2

harsh
harsh

Reputation: 7692

Remove comma before FROM, most likely this is causing the error

Upvotes: 3

Related Questions