SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Search All Columns in all tables


Search All Columns in all tables

Author
Message
R Michael
R Michael
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1571 Visits: 275
Comments posted to this topic are about the item Search All Columns in all tables

SQL guy and Houston Magician
ShrikrishnaG
ShrikrishnaG
Valued Member
Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)Valued Member (73 reputation)

Group: General Forum Members
Points: 73 Visits: 30
The actual post is Ok for the tables which are of One word for others below is the modified query.

DECLARE @searchSQL AS VARCHAR(MAX)
DECLARE @SearchItem AS VARCHAR(MAX) ; SET @SearchItem = '%Davolio%'

SELECT @searchSQL = COALESCE(@searchSQL + '+ CAST(COALESCE(MIN(CASE WHEN IC.Ordinal_position = ' +
CAST(Ordinal_position AS VARCHAR(MAX)) +
' THEN '' OR '' + IC.Column_Name + '' LIKE ''''' + @SearchItem + ''''''' END), '''') AS NVARCHAR(max))' +
CHAR(13), 'CAST(COALESCE(MIN(CASE WHEN IC.Ordinal_position = ' +
CAST(Ordinal_position AS VARCHAR(MAX)) +
' THEN IC.Column_Name + '' LIKE ''''' + @SearchItem + ''''''' END), '''') AS NVARCHAR(max))' + CHAR(13))
FROM INFORMATION_SCHEMA.Columns
GROUP BY Ordinal_Position
ORDER BY Ordinal_Position

EXEC('SELECT REPLACE(Query, ''WHERE OR'', ''WHERE'') FROM
(SELECT ''SELECT '''''' + CAST(TABLE_NAME AS VARCHAR(MAX)) + '''''' as tbl, * FROM ['' +
CAST(TABLE_SCHEMA AS VARCHAR(MAX)) + '']. ['' + CAST(TABLE_NAME AS VARCHAR(MAX)) + ''] WHERE '' + ' + @searchSQL + ' AS query
FROM INFORMATION_SCHEMA.Columns IC
WHERE DATA_TYPE IN (''char'', ''varchar'', ''nchar'', ''nvarchar'')
GROUP BY TABLE_NAME, TABLE_SCHEMA) S')
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search