Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

Int and VarChar types in where clause

hi,

I have the below Stored proc which passes in the field name and values to filter on. It works great. However, some of the columns are of type int and others are varchar. The varchar columns need the values in quotes, but the int columns do not. Therefore the below only works for varchars.

Is there any easy fix? Thanks
CREATE PROC test
@columName  varchar(30),
@value  varchar(30)
AS
BEGIN
    EXECUTE(
    'SELECT     D.id_desk,
                D.id_region           
    FROM        DESK D,
                INSTRUMENT I
    WHERE '      + @columName + ' LIKE ''' + @value +'''
    AND         I.id_desk = D.id_desk' )
END
go

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of IncisiveOne
IncisiveOne
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Er, that's for discussion/progress, obviously the code will not work without the execute.
SOLUTION
Avatar of alpmoon
alpmoon
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tried it? Is it useful for your end?
I think my solution is more flexible and efficient. If table structure is changed, the procedure doesn't need to be modified.

I suggest split between me and IncisiveOne
I think my solution is more flexible and efficient. If table structure is changed, the procedure doesn't need to be modified.

I suggest split between me and IncisiveOne