0

I need to check if columns given in a user populated scalar exist in a temporary table.

example:

create table #ResultSet 
(
    ClaimId Id_t            not null,
    AdjustmentVersion       int null,
    ClaimNumber             ClaimNumber_t   null,
    FormType                Code_t  null,
    FormTypeName            Name_t  null, 
    FormSubtype             Code_t  null,
    FormSubtypeName         Name_t  null, 
    ClaimType               Code_t  null,
    ServiceDateFrom         Date_t  null,
    ServiceDateTo           Date_t  null
)

declare @CompareFields varchar(max)

select @CompareFields = 'ClaimType,ClaimNumber'

I have tried the following:

IF not EXISTS(SELECT 1 FROM tempdb.sys.columns 
                      WHERE Name = @CompareFields
                      AND Object_ID = Object_ID(N'tempdb..#ResultSet'))
        begin
            select @UserMsg='Given Columns did not match the Result Set.'
            goto BusinessErrorExit
        end

But this is only valid for a single column name.

Is there anyway to check for Multiple columns without looping?

H22
  • 121
  • 6

0 Answers0