I started to take a look at the following question: https://stackoverflow.com/questions/8616458/sql-query-like-clarification
Unfortunately it appeared that the original question was somewhat unclear however I decided to interpret this as him trying to only run his query when certain columns in the schema matched his like condition. It’s very possible that this wasn’t what was being asked however I wanted to challenge myself to do this (posted as a new question since the original was unclear and likely to be deleted and very possible nothing related to this).
What I got to was the following:
select
[OrderID]
,[ProductID]
,[UnitPrice]
,[Quantity]
,[Discount]
from [Northwind].[dbo].[Order Details]
where
(
select top 1
COLUMN_NAME
from INFORMATION_SCHEMA.columns
where COLUMN_NAME like '%ProductID'
) like '%ProductID'
I’m aware how syntactically ridiculous this is however I put it here to give you an idea where my thought process was headed and was also hoping that based on this I could get feedback as to a better method of only running a query when there’s a column in the database that matches a condition that you specify? I don’t see any purpose for it anywhere – I’m just curious and think I could learn from this.
If I understand you correctly you can do it using system view sys.columns: