First: This isn't easy to implement and it's going to break very easily (by which I mean performance is going to be horrible, and it's a nice little route for SQL injection attacks if you're not very very careful). I strongly advise you to re-think what you're doing because there has to be a better way.
Second: The question as it stands doesn't actually make sense - what condition are you checking on the columns?
NOT NULL? Also, do you want to
OR the conditions together?
To actually answer the question, though, to do this you'll need to write some dynamic SQL:
syscolumns, filter by the table name and the fields you're interested in.
- Build a executable SQL statement using a cursor, then execute it.
Example below is, for each column in the
Users table that starts with either 'A' or 'B', return all rows in the
Users table that have a non-null value in any of those columns.
Note: I don't have a Sybase install handy to test this. Chances of it actually working are very low, but it should hopefully be enough to make you realise what a bad idea this is.
DECLARE @colname nvarchar(255),
SET @sql = 'SELECT * FROM Users WHERE '
DECLARE colcur CURSOR FOR
FROM syscolumns sc
sysobjects so ON so.id = sc.id
WHERE so.name = 'Users'
AND so.type = 'U'
AND (sc.name LIKE 'A%' OR sc.name LIKE 'B%')
ORDER BY sc.name
FETCH colcur INTO @colname
SET @sql = @sql + @colname + ' IS NOT NULL OR '
SET @sql = LEFT(@sql, LEN(@sql) - 3) -- need to trim off the trailing 'OR'