Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Search entire database

Author  Topic 

Mesktomten
Starting Member

8 Posts

Posted - 2006-02-20 : 05:47:09
Hi!

I need to search a database that I did not build for values that I know exist. The problem is that I don't know in what table the value is.

Is there any way to search the entire database for two values that should exist on the same row (ex: customerid and companyname) and find out in what table they are?

Thanks!
/Martin

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-20 : 06:36:36
See if this helps
http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Mesktomten
Starting Member

8 Posts

Posted - 2006-02-20 : 06:58:24
Hmmm...

That is almost it!

Though this procedure allows me to also include numeric colums I really need to search for two parameters that are supposed to be "together"... I know I used a stupid example when I used the two tables above! What I need is to find two numeric fields (more like articleid and quantity) where I can find the exact match of articleid = 140 and quantity = 30. To make it even harder I just know "140" & "30" should be together... I don't know the tablename and I don't know the columnname!

Thanks anyway, I'll try to be more specific next time!
/Martin
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-20 : 07:42:03
Run this in Query Analyser

select 'Select * from ' +name+' where articleid = 140 and quantity = 30' from sysobjects
where xtype='u'

Copy the result and paste it in Query Analyser
Run the queries one by one

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Mesktomten
Starting Member

8 Posts

Posted - 2006-02-20 : 07:50:35
Thanks!

I'm a bit closer... The problem now, as I wrote in my last post, is that I don't know the columnnames. Is there anyway to "convert" this query to look for 140 & 30 without knowing the names..?

/Martin
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-21 : 01:37:50
You may try the link I posted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Mesktomten
Starting Member

8 Posts

Posted - 2006-02-21 : 04:06:53
Do you know if there's anyway to "extend" the procedure to only give me results where 140 & 30 is on the same row (not the same column though)?
Since I'm working with a database that holds many millions of values, both 140 and 30 exists a couple of thousand times each... It would take years to "manually" look for the exact match!

/Martin
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-21 : 05:48:03
Modified from the original. It is not perfect yet but it should get you closer to what you want
if exists (select * from sysobjects where name = 'SearchAllTables')
begin
DROP PROCEDURE SearchAllTables
end
go

CREATE PROCEDURE SearchAllTables
(
@SearchVal1 int,
@SearchVal2 int
--@SearchStr nvarchar(100)
)
AS
BEGIN

-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
-- Purpose: To search all columns of all tables for a given search string
-- Written by: Narayana Vyas Kondreddi
-- Site: http://vyaskn.tripod.com
-- Tested on: SQL Server 7.0 and SQL Server 2000
-- Date modified: 28th July 2002 22:50 GMT

--CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
CREATE TABLE #Results (TableName nvarchar(500), ColumnName nvarchar(370), ColumnValue int)

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SELECT @TableName = ''
--SELECT @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
--AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND DATA_TYPE IN ('int', 'bigint', 'tinyint', 'numeric', 'decimal')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results(TableName, ColumnName, ColumnValue)
EXEC
(
'SELECT ''' + @TableName + '''' + ', ''' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630)
FROM ' + @TableName + ' (NOLOCK) ' +
--' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
' WHERE ' + @ColumnName + ' = ' + @SearchVal1 +
' OR ' + @ColumnName + ' = ' + @SearchVal2
)
END
END
END

SELECT R.TableName, R.ColumnName, R.ColumnValue
FROM #Results R
INNER JOIN
(
SELECT TableName
FROM #Results
GROUP BY TableName
HAVING COUNT(*) > 1
) AS D
ON R.TableName = D.TableName
END

GO

exec SearchAllTables 140, 30



----------------------------------
'KH'

It is inevitable
Go to Top of Page
   

- Advertisement -