0

I have a SQL query where I have in my where clause a list into a IN condition.

Example of what I mean:

WHERE Id IN (11111,11112) 

And what I try to do is the following: I have a linked server query where I have an IN clause.

And I have a subquery where I use a JOIN table to cross data but I need that in the IN condition use the same Id like the main query.

I hope you guys understand what I try to do:

Here's my code:

SELECT 
Name, 
Street, 
Number,
(SELECT loginUser FROM [LinkedServer].[Database].[dbo].[Users] T1 
INNER JOIN [LinkedServer].[Database].[dbo].[General] T2
ON T2.IdUser = T1.Id
WHERE T2.Id IN (11111,11112,11113,11114,11115)
)
FROM [LinkedServer].[Database].[dbo].[General]
WHERE Id IN (11111,11112,11113,11114,11115)

i get this error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

4
  • 2
    Yes - and what's the question? You seem to have your query all worked out .... Commented Jul 10, 2014 at 10:57
  • @marc_s: i have update my question. pls check it out Commented Jul 10, 2014 at 11:00
  • Can you provide your actual query. You seem to be referencing the exact same table 3 times, which is obfuscating your real problem. Commented Jul 10, 2014 at 11:03
  • @gvee: i have update my code. take a look pls Commented Jul 10, 2014 at 11:13

3 Answers 3

1

To reuse same set of ids you could use Table Variable.

DECLARE @T TABLE(ID INT);

INSERT INTO T
SELECT 11111 UNION ALL SELECT 11112 ....

then

SELECT * FROM    
(SELECT Varchar1, 
Varchar2, 
Varchar3, loginUser FROM [LinkedServer].[Database].[dbo].[Table] T1 
INNER JOIN [LinkedServer].[Database].[dbo].[Table] T2
ON T2.INT1 = T1.INT1
WHERE T2.Id IN (select id from @T)
) AS X WHERE
X.Id IN (select id from @T)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your advice, but still get the same error
@user2232273 I think I have guessed right this time check update.
I have solved it finally. thanks for your solution. i have write an answer and have mentioned you as an author for the solution. Many thanks. +1 for the key of the solution
1

Consider using a join instead of a correlated-subquery:

SELECT General.Name
     , General.Street
     , General.Number
     , Users.loginUser
FROM   [LinkedServer].[Database].[dbo].[General]
 LEFT
  JOIN [LinkedServer].[Database].[dbo].[Users]
    ON Users.Id = General.IdUser
WHERE  General.Id IN (11111, 11112, 11113, 11114, 11115)

This will return the loginUser where possible.

Comments

0

i have solved it finally.

The Problem was that the subquery haven´t a where condition to extract a specific Id. Without a WHERE Condition the query return more than 1 value.

What i have done:

Thanks for the advice and his solution Author: "LIUFA".

With a combination of his code and some correction of my code i get the solution.

I have add one more column Id and give it an alias. In the subquery i have done the select from the Temp table using a where condition. And its Working. Perfect.

Hope that this solution can help somebody to solved a related issue.

Finally Code:

DECLARE @T TABLE(ID INT);
INSERT INTO @T
Select ReferenceId From MyBigData


SELECT,
A.Id,
Name, 
Street, 
Number,
(SELECT loginUser FROM [LinkedServer].[Database].[dbo].[Users] T1 
INNER JOIN [LinkedServer].[Database].[dbo].[General] T2
ON T2.IdUser = T1.Id
WHERE T2.Id IN (SELECT Id FROM @T WHERE Id = A.Id)
)
FROM [LinkedServer].[Database].[dbo].[General]
WHERE Id IN (SELECT Id FROM @T)

Comments

Your Answer

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.