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


HOW TO FIND A VALUE IN ALL COLUMN OF ALL TABLE IN A db.


HOW TO FIND A VALUE IN ALL COLUMN OF ALL TABLE IN A db.

Author
Message
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27158 Visits: 17557
What have you tried? It looks like the script posted above would be a pretty good starting point. That could be modified pretty easily to accomplish what you are after.

I have to ask, this is a rather strange request. What is the reason behind finding such information?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Cadavre
Cadavre
SSCarpal Tunnel
SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)

Group: General Forum Members
Points: 4028 Visits: 8472
Untested, but something like this should work
BEGIN TRAN

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = COALESCE(@SQL,'') + 'SELECT ' + c.name + ', '''+c.name+''' AS columnName FROM ' + o.name + ' WHERE LEN('+c.name+')=9;' + CHAR(10)
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
WHERE o.type = 'U' AND
t.name IN ('tinyint','smallint','int','real','money','float','decimal','numeric','smallmoney','bigint')

EXEC (@SQL)

ROLLBACK




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Ivan Mohapatra
Ivan Mohapatra
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 495
because in our db there are table with wrong datatype.So i wan to find it by this means to complete a task
Ivan Mohapatra
Ivan Mohapatra
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 495
Cadavre (1/9/2012)
Untested, but something like this should work
BEGIN TRAN

DECLARE @SQL AS VARCHAR(MAX)

SELECT @SQL = COALESCE(@SQL,'') + 'SELECT ' + c.name + ', '''+c.name+''' AS columnName FROM ' + o.name + ' WHERE LEN('+c.name+')=9;' + CHAR(10)
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
WHERE o.type = 'U' AND
t.name IN ('tinyint','smallint','int','real','money','float','decimal','numeric','smallmoney','bigint')

EXEC (@SQL)

ROLLBACK



Let me try this but not sure it will work
Dev
Dev
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3656 Visits: 1602
Agree with Sean. It’s not like regular requirements that we get from management. For instance, if a table has 2 rows and one of the columns have 123456789 and 12345678 values. Would you consider this table and column as desired output?

What do you mean by wrong data type? Oversized columns? Don’t have data model of your database to figure it out? It would be a nice option, if you have.
Ivan Mohapatra
Ivan Mohapatra
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 495
HI all,

Well the point of discussion is to have a script by which i can get all the column name and table name in a db where the data length is 9 digit and the datatype should be varchar,int.AS because we have a ID column with datatype as varchar & Int in different table.

So as this is a rarer request in any IT forum IT will be a nice one for all to learn it from the giants how it is done.

So kindly help me out for which i will be very thankful to u all.
Ivan Mohapatra
Ivan Mohapatra
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 495
Dev (1/9/2012)
Agree with Sean. It’s not like regular requirements that we get from management. For instance, if a table has 2 rows and one of the columns have 123456789 and 12345678 values. Would you consider this table and column as desired output?

What do you mean by wrong data type? Oversized columns? Don’t have data model of your database to figure it out? It would be a nice option, if you have.


hope it is you ( DEV PINAL) I Am a great Fan Of Your SKill.

yes I would consider that as desired out put.Because we don't have such data So As ID column has 9 digit only in my entire DB.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: Administrators
Points: 65777 Visits: 19118
Ivan Mohapatra (1/9/2012)

Well the point of discussion is to have a script by which i can get all the column name and table name in a db where the data length is 9 digit


data length or data type?

and the datatype should be varchar,int.AS because we have a ID column with datatype as varchar & Int in different table.


The sentence (or fragment) above doesn't make any sense. What is "varchar,int.as"? Yo need to slow down and write a description that makes sense.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Ivan Mohapatra
Ivan Mohapatra
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 495
Steve Jones - SSC Editor (1/9/2012)
Ivan Mohapatra (1/9/2012)

Well the point of discussion is to have a script by which i can get all the column name and table name in a db where the data length is 9 digit


data length or data type? -Data length

and the datatype should be varchar,int.AS because we have a ID column with datatype as varchar & Int in different table.


The sentence (or fragment) above doesn't make any sense. What is "varchar,int.as"? Yo need to slow down and write a description that makes sense.

Below is a Example what i mean to say and my kind request kindly read my request seriously and understand it before u all reply.

Well there are 60 table in 1 Db there is a ID column in where there are 9 digit data.like 999999999,888888888

well in this 60 table there is around 20 column where 9 digit data.like 999999999,888888888 exits AND when i found there is two kind of datatype is been used 1 is varchar and another is INT for this ID column in different tables. i mean in one table the datatype for ID column is varchar and in another table ID column is INT.

So What i want is a script by which i can get all the table name and column name in this DB
where len of Data is 9 digit data.like 999999999,888888888 .
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27158 Visits: 17557
OK we are now 20 posts into your request and the details of what you want is still changing with each post. We can't see over your shoulder, we are not familiar with your system or what you want to retrieve out of it. There have been a number of people trying to help. You have to help us help you. Give us a detailed explanation of what you want.


So What i want is a script by which i can get all the table name and column name in this DB
where len of Data is 9 digit data.like 999999999,888888888 .


What does that mean? Do you want only varchar fields that a numeric value that has a length of 9? Does it count as a match if the column has other data?

Taking the first script posted you could change the where clause find what you are looking for.

pure pseudocode but...

 
where datalength(column) = 9 and isnumeric(coliumn) = 1



If you explain clearly (yes we know it is clear to you) in a way that somebody else can understand it, we will be happy to help you.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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