SQL search entire db for a value [duplicate] - sql

This question already has answers here:
Find a value anywhere in a database
(18 answers)
Search all tables, all columns for a specific value SQL Server [duplicate]
(4 answers)
Closed 8 years ago.
I have "read" access to a database back end however the tables and columns are named oddly and I am unable to find the information I am looking for. (there are also a lot of tables and lots of data)
I have been using the following method:
Run query:
SELECT *
FROM information_schema.columns
WHERE TABLE_SCHEMA = 'dbname'
AND DATA_TYPE = 'varchar'
AND TABLE_NAME IN (SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_TYPE = 'BASE TABLE')
This gives me a list of columns which contain varchar values...
Then for each result, I run the following:
select top 1 [column name]
from [tablename]
where [column name] like 'value I'm searching for'
Is there a better way to do this? Or a way to combine these two queries together (as running the query on each result is a slow method)?
Thanks in advance

Related

Find list of tables from list of cols [duplicate]

This question already has answers here:
Find all tables containing column with specified name - MS SQL Server
(35 answers)
Closed 2 years ago.
I did google this a bit but my biggest problem is not knowing what to search. I felt like no matter what I searched it wasn't my problem so if you have a link to another question do to my search inability feel free to link it and be done with it.
Say I have like 250 different fields (columns) and they are all from 1 of 10 tables but I need to figure out which field comes from which table. Maybe i could do a massive select statement but there are possible duplicate column names and that sound like a headache
looking at each column name and the scouring through all these tables to find each one seems too difficult? Any thoughts? Does my question make sense? Let me know if you need more information.
you can use information_schema.columns to get information
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME in ('col1', 'col2', ...)
For SQL Server you can find metadata information from the system base tables
SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
JOIN sys.columns AS c ON t.object_id = c.object_id
WHERE c.name IN('col1','col2') --give the columns names here.
Now, this code is specific to SQL Server and not a portable code.
But few cases to be noted that instead of using INFORMATION_SCHEMA which doesn't always have all the information like if the column has an IDENTITY property as it is proprietary to SQL Server.
You can read more about this here

How can I retrieve the table structure from a sql database? [duplicate]

This question already has answers here:
Describe table structure
(13 answers)
Closed 3 years ago.
So basically I want a SQL Command that return every column in the table and the datatype that is in that column and whether is nullable.
You can use information schema.
SELECT
COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'yourTableName'

How do I return a list of all column titles in a table in SQLite? [duplicate]

This question already has answers here:
How can I get the list of a columns in a table for a SQLite database?
(9 answers)
Closed 9 years ago.
I have a 20-or-so column table 'Contacts' in a Contacts.sqlite file. I use the Mac Terminal to interact with it, and am writing a program that will perform certain actions based on the number of columns in the table, as well as the name of each column.
Thus, I need to perform some sort of query that will return a list of all my column titles.
I have found various recommendations that look like some version of this:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'Schema' AND table_name = 'Table_Name'
but it almost always returns an error saying there is "no such table: information_scheme.columns".
Looks like SQLite does not support information_schema. You can retrieve the list of table names from sqlite_master. But there is no table with columns.
Instead, SQLite supports the proprietary pragma table_info():
pragma table_info(table_name);

How can I SELECT that lists all the field names and their types for a given Db Table? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server: How do you return the column names from a table?
Assume that I have Data base table: LunchMenuItems
For the MS-SQL Server, how can I write a TSQL query that :
Lists all the column names and their types in LunchMenuItems table?
Description
This Information can be found in information_schema.columns.
Sample
select column_name, DATA_TYPE from information_schema.columns
where table_name = 'LunchMenuItems'
More Information
COLUMNS (Transact-SQL)

Finding the column in tables? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server 2008: find all tables containing column with specified name
Hi all,
I have 30 tables in my database, in some of tables i have a common column, assume 'eno'. How can i find the tables of 'eno'. Thank you...
You could use the INFORMATION_SCHEMA.COLUMNS system view:
SELECT DISTINCT table_name
FROM information_schema.columns
WHERE column_name = 'eno'