I am using the accepted answer from this question, but for multiple tables.
$result = mssql_query("SELECT CAST(
CASE WHEN
EXISTS(select 1 from codes where code = '".$generatedCode."') OR
EXISTS(select 1 from pakete where code = '".$generatedCode."') OR
EXISTS(select 1 from kunden where code = '".$generatedCode."') OR
EXISTS(select 1 from formulare where code = '".$generatedCode."') OR
EXISTS(select 1 from berater where code = '".$generatedCode."')
THEN 1
ELSE 0
END
AS BIT) as 'exists'";
$row = mssql_fetch_assoc($result);
if ($row['exists']==0)
Theoretically it looks good, practically it works too. But it happens from time to time, that the query (i think) wents wrong and returns 1 even if there is a "code" in one of the tables.
Maybe someone knows a better solution for checking across tables, where it could happen that the searched varchar exists in one or two tables only.
fyi: generatedcode is a nine digit alphanumeric unique string.
Regards, Markus
UPDATE: The Query works as expected, the failure was a wrong named variable after return, so the original generatedcode (the duplicate one) was used instead. Thanks for your help and for simplifying the query.