In my sql in i am trying to get table name from another table in SELECT query but it seems to be not working,
NOTE: declaring variables in not allowed in the platform where i am writing this SQL
I have tried this but no luck
`SELECT * from (select de_name from ENT.DISTINCT_DE_TABLE) WHERE email = 'abc@yahoo.com'`
I have tried this as well, still no luck
With TableVariable As (
SELECT DE_NAME FROM DISTINCT_DE_TABLE
)
SELECT * FROM from mytable where mytable = (SELECT DE_NAME FROM With TableVariable) and email = 'abc@yahoo.com'
another approach - no luck
SELECT x.*
FROM
(
SELECT de_name
FROM ENT.DISTINCT_DE_TABLE
) AS x
WHERE email = 'abc@yahoo.com'
Any advise on how to go about it?
NOTE: email field does not exist in ENT.DISTINCT_DE_TABLE, it exists in the table that will be pulled from ENT.DISTINCT_DE_TABLE as DE_NAME.
You would need dynamic SQL to do this - which requires using a variable to construct the query to be executed and passing that string to EXECUTE or sp_ExecuteSql.
If you need some type of macro substitution - then you are going to need to figure out how to do that in the platform you are using, or you need to create a stored procedure and execute the procedure. Either way - you need to be very careful with this approach and make sure you are fully aware of SQL injection issues and build your code appropriately.