views:

126

answers:

3

I want to run a query similar to this one on a 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, running this results in the error message:

Duplicated parameter names are not allowed. [ Parameter name = @Param ]

A workaround is of course to define @Param1 and @Param2 and assign them the same value, but this feels a bit dirty to me. Is there a cleaner workaround for this problem?

+1  A: 

Add the parameter only once to the parameter collection. You can use it how may times you like in the query.

Guffa
Although I use the parameter twice, I add it only once, but I still get the error message.
Daan
A: 

I've never used SQL CE, but maybe this will work:

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
Grzegorz Gierlik
Nice try :) Unfortunately, this only shifts the problem to `@P`, which is now the invalid duplicate parameter.
Daan
A: 
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
Dewfy
While this works fine on SQL Server, this won't run on SqlCE...
Daan
Thinking in the same way - create view or table-function (with 1 param) from UNION and use it as simple query.
Dewfy