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
 General SQL Server Forums
 New to SQL Server Programming
 General search

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-10-05 : 05:07:40
Hi,

is there a search function in MS SQL server 2005 to search for a name of a table or a name of field or entry in a field?

what/how should be the fastest way to do it?

thank you

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-05 : 05:10:26
For name of table:
Select * from information_schema,tables where table_name ='foo'


For name of column:
Select * from information_schema,columns where column_name ='foo'


For entry in field:
Select * from table_name where col1 ='foo'


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-10-05 : 05:21:05
oo lovely....except for the last one. it seems that i can't find the proper definition.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2007-10-05 : 05:44:25
This script has followed me for quite some time now:
SET nocount ON

DECLARE @string VARCHAR(1000)

SET @string = ''

DECLARE
@errnum INT,
@errors CHAR(1),
@rowcnt INT,
@output VARCHAR(255)

SELECT
@errnum = 0,
@errors = 'N',
@rowcnt = 0,
@output = ''

DECLARE @Results TABLE (
Name VARCHAR(55),
Type VARCHAR(12),
DateCreated DATETIME,
ProcLine VARCHAR(4000)
)


INSERT
INTO @Results
SELECT DISTINCT
'Name' = CONVERT(VARCHAR(55), SO.name),
'Type' = SO.type,
crdate,
''
FROM
sysobjects SO
JOIN syscomments SC
ON SC.id = SO.id
WHERE
SC.text LIKE '%' + @string + '%'
UNION
SELECT DISTINCT
'Name' = CONVERT(VARCHAR(55), SO.name),
'Type' = SO.type,
crdate,
''
FROM
sysobjects SO
WHERE
SO.name LIKE '%' + @string + '%'
UNION
SELECT DISTINCT
'Name' = CONVERT(VARCHAR(55), SO.name),
'Type' = SO.type,
crdate,
''
FROM
sysobjects SO
JOIN syscolumns SC
ON SC.id = SO.ID
WHERE
SC.name LIKE '%' + @string + '%'
ORDER BY
2,
1

SELECT
Name,
'Type' = CASE (Type)
WHEN 'P' THEN 'Procedure'
WHEN 'TR' THEN 'Trigger'
WHEN 'X' THEN 'Xtended Proc'
WHEN 'U' THEN 'Table'
WHEN 'C' THEN 'Check Constraint'
WHEN 'D' THEN 'Default'
WHEN 'F' THEN 'Foreign Key'
WHEN 'K' THEN 'Primary Key'
WHEN 'V' THEN 'View'
ELSE Type
END,
DateCreated
FROM
@Results
ORDER BY
2,
1


--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2007-10-05 : 07:39:33
any other faster/easier way to do it? maybe with information_schema.
Go to Top of Page
   

- Advertisement -