SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query by Column Name
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Dorffius
Starting Member

Canada
36 Posts

Posted - 03/01/2002 :  11:48:33  Show Profile  Send Dorffius an ICQ Message  Reply with Quote
I have one recordset that returns all of the column names from a table. I want to be able to base the next recordset on the value of the fields that reside under that column name for a certain record(CSS#).

The first recordset works fine using INFORMATION_SCHEMA.COLUMNS


"SELECT * FROM ETATEMP WHERE Recordset2.Fields.Item("COLUMN_NAME").Value = ????????? AND CCS = '" & target & "'"

Thanks in advance.

rrb
SQLTeam Poet Laureate

Australia
1479 Posts

Posted - 03/20/2002 :  17:52:49  Show Profile  Reply with Quote
I'm not sure I've quite understood, but I think this might help:

It's an sp which searches ALL NVARCHAR FIELDS of a given table. Sorry I don't have time to generalise it to all field types...

create procedure spSearchAllFields
@sTableName nvarchar(50),
@sSearchPattern nvarchar(50)
as
set nocount on

declare @sSQL nvarchar(1000)

--get column names
create table #b (colname nvarchar(50),collist nvarchar(1000))
insert into #b
select syscolumns.name,NULL
from sysColumns inner join sysobjects
on syscolumns.id = sysobjects.id
where sysobjects.name = @sTableName
and syscolumns.xusertype = 231 --nvarchar only

--build csv column name list where clause
declare @sWhereClause nvarchar(1000)
set @sWhereClause = ''
update #b
set @sWhereClause = collist = @sWhereClause + 'or ' + colname + ' like ''' + @sSearchPattern + ''' '

--strip off leading 'or '
set @sWhereClause = substring(@sWhereClause, 4,len(@sWhereClause)-3)

--collate SQL
set @sSQL = 'select * from ' + @sTableName + ' where ' + @sWhereClause

exec (@sSQL)

drop table #b
go


as they say in the classics - HTH

(PS I stole the idea from graz - http://www.sqlteam.com/item.asp?ItemID=765 )

--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000