How to use the same parameter more than once in a request

I want to run a query similar to this in the SqlCE database:

SELECT t.Field1, t.Field2
FROM MyTable t
WHERE t.Field1 = @Param
UNION ALL
SELECT t2.Field1, t2.Field2
FROM MyOtherTable t2
WHERE t2.Field1 = @Param

However, executing this result results in an error message:

Duplicate parameter names are not allowed. [Parameter Name = @Param]

The workaround is, of course, defining @Param1and @Param2assigning them the same value, but for me it is a little dirty. Is there a more difficult solution to this problem?

+3
source share
3 answers
SELECT * FROM (
SELECT t.Field1, t.Field2
FROM MyTable t
UNION ALL
SELECT t2.Field1, t2.Field2
FROM MyOtherTable t2
) sub
WHERE sub.Field1 = @Param
+1
source

Add a parameter only once to the parameter collection. You can use it as you like in the request.

0
source

SQL CE, , , :

DECLARE @P int

SET @P = @Param

SELECT t.Field1, t.Field2
FROM MyTable t
WHERE t.Field1 = @P
UNION ALL
SELECT t2.Field1, t2.Field2
FROM MyOtherTable t2
WHERE t2.Field1 = @P
0

Source: https://habr.com/ru/post/1714485/


All Articles