This stored procedure code will do what you want.You cannot use an EXECUTE in a WHERE clause.drop proc P_CHECK_TABLE_TIMESTAMPgocreate procedure P_CHECK_TABLE_TIMESTAMP( @table_owner sysname, @table_name sysname, @column_name sysname, @timestamp_value varbinary(16), @timestamp_exists bit output)asset nocount onset @timestamp_exists = 0if not exists( select * from information_schema.columns where TABLE_SCHEMA = @table_owner and TABLE_NAME = @table_name and COLUMN_NAME = @column_name ) begin -- Return error code -1 if table or column does not exist return -1 enddeclare @sql nvarchar(4000)select @sql ='if exists( select * from ['+@table_owner+'].['+@table_name+'] where ['+@column_name+'] > @timestamp_in) begin set @was_found = 1 endelse begin set @was_found = 0 end'execute sp_executesql @sql, N'@timestamp_in varbinary(16), @was_found bit output', @timestamp_in = @timestamp_value, @was_found = @timestamp_exists outputreturn 0go
Code to execute the stored procedure:declare @retcode int, @table_owner sysname, @table_name sysname, @column_name sysname, @timestamp_value varbinary(16), @timestamp_exists bitset @table_owner = 'dbo'set @table_name = 'T_MY_TABLE'set @column_name = 'MY_TIMESTAMP_COLUMN_NAME'set @timestamp_value = 0x0000000000001A52exec @retcode = P_CHECK_TABLE_TIMESTAMP @table_owner, @table_name, @column_name, @timestamp_value, @timestamp_exists output-- Show Output Resultsselect [@retcode] = @retcode, [@timestamp_exists] = @timestamp_exists
CODO ERGO SUM