Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

HOW TO FIND A VALUE IN ALL COLUMN OF ALL TABLE IN A db. Expand / Collapse
Author
Message
Posted Monday, January 09, 2012 8:46 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:31 PM
Points: 11,949, Visits: 10,982
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 Moden's 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)
Post #1232494
Posted Monday, January 09, 2012 8:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 4:55 AM
Points: 2,403, Visits: 7,310
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




Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1232496
Posted Monday, January 09, 2012 8:52 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 21, 2014 4:26 AM
Points: 121, Visits: 432
because in our db there are table with wrong datatype.So i wan to find it by this means to complete a task
Post #1232506
Posted Monday, January 09, 2012 8:53 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 21, 2014 4:26 AM
Points: 121, Visits: 432
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
Post #1232509
Posted Monday, January 09, 2012 9:57 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, January 30, 2014 12:01 AM
Points: 2,013, Visits: 1,581
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.


Dev

Devendra Shirbad | DBA / Data Architect | Ex-Microsoft CSS (SQL 3T)
LinkedIn: http://www.linkedin.com/in/devendrashirbad
Post #1232608
Posted Monday, January 09, 2012 10:42 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 21, 2014 4:26 AM
Points: 121, Visits: 432
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.
Post #1232655
Posted Monday, January 09, 2012 10:47 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 21, 2014 4:26 AM
Points: 121, Visits: 432
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.
Post #1232664
Posted Monday, January 09, 2012 11:20 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 1:49 PM
Points: 32,768, Visits: 14,929
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
Post #1232688
Posted Monday, January 09, 2012 11:36 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, March 21, 2014 4:26 AM
Points: 121, Visits: 432
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 .



Post #1232697
Posted Monday, January 09, 2012 11:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:31 PM
Points: 11,949, Visits: 10,982
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 Moden's 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)
Post #1232715
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse