Get list of tables with column value as a condition - sql

I have need to get list of tables which have a column locn with value 'cba' from differnet schemas
EX :
select table_name where all_tab_columns where column_name='locn';
--- getting list of tables
now i need to get list of tables where locn column having value as 'cba'
Please advice

SELECT
*
FROM
INFORMATION_SCHEMA.COLUMNS C
WHERE
COLUMN_NAME LIKE '%xxxxx%'
Replace xxxx with the column name or partial name.
This will tell you everything.

If you require only table and columns then use
Select object_name(c.object_id) 'Table',c.name,ROW_NUMBER() over(order by c.name) 'R' into #temp
from sys.tables t
join sys.columns c
on c.object_id = t.object_id
where c.name like '%col%'
If you require even data also then use the following link
http://gallery.technet.microsoft.com/scriptcenter/c0c57332-8624-48c0-b4c3-5b31fe641c58

Related

Passing select result to like operator in SQL

I have results of name in a customer table
I want to find whether this name is present in log table column
My log table changedate column will record name and also id, I want to find only name
select top 100 *
from LogDataTable
where ChangeData like '%'+'select name from customer'+ '%'
But I'm not getting results with this query, any changes I have to do? Can it done?
You can use a join to compare data the way you are asking e.g.
select top 100 L.*
from dbo.LogDataTable L
inner join dbo.Customer C on L.ChangeData like '%' + C.[Name] + '%';
Its not going to perform very well though, as it has to do a table scan of Customer for every row in LogDataTable.
If you were able to ensure that the Customer Name was always the first part of the ChangeData you could use like C.[Name] + '%' and then benefit from indexes.
To solve your collation error, re-collate of one of the columns in your query. I recommend changing C.[Name] since that is already the subject of a table scan. But it depends whether you need an "Accent Sensitive" compare or not e.g.
select top 100 L.*
from dbo.LogDataTable L
inner join dbo.Customer C on L.ChangeData like '%' + C.[Name] collate SQL_Latin1_General_CP1_CI_AS + '%';

Get all Column Names being referenced in one particular Table only

I have 1000+ tables, SPs, and Views in my database.
How do I get a list of all "Column Names" across whole database that are being referenced in one particular table only; and not in any other tables or SPs or Views in my database?
i.e. I have Database A. which has Tables: B, C, D; Views: E, F, G; and SPs: H, I, J. Table B has 500 columns; and for example 250 columns are being referenced in other Views or SPs but 250 are not being used anywhere else. How do I get the list of Columns Names that are being used in Database B only and not anywhere else?
This is an interesting question. This will show you all fields in all tables.
SELECT T.name AS Table_Name ,
C.name AS Column_Name ,
P.name AS Data_Type ,
P.max_length AS Size ,
CAST(P.precision AS VARCHAR) + '/' + CAST(P.scale AS VARCHAR) AS Precision_Scale
FROM sys.objects AS T
JOIN sys.columns AS C ON T.object_id = C.object_id
JOIN sys.types AS P ON C.system_type_id = P.system_type_id
WHERE T.type_desc = 'USER_TABLE';
This will show you all field names in a specific view.
SELECT *
FROM sys.views
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.View_1')
Can you start with that? Post back if you have additional questions.

How to get Oracle SQL query to sort by column name

How can I write an SQL query such that the columns are alphabetically ordered from left to right?
If I were to use select alpha, bravo, charlie, delta from .. then the column names would be sorted from left to right because I explicitly put them in order. How can I do this without explicitly writing out all of the column names?
You can create a view with the help of this SQL in your schema and then select from that view
SELECT 'create or replace view tasks_alp as select '
|| Listagg(column_name, ',')
within GROUP (ORDER BY column_name)
||' from tasks'
FROM user_tab_cols
WHERE table_name = 'TASKS'
AND column_name NOT LIKE 'SYS%$';
The Sample output is
create or replace view tasks_alp as select ID,SEQ,STATE from tasks
Then you can select the columns using
select * from tasks_alp;
-- FOR MS SQL
select c.name from sys.tables t
inner join sys.columns c on t.object_id = c.object_id
where t.name = 'TableName'
order by c.name

How do I find the which table it is belong when I'm only given column name?

Hi I am working on SQL query, a total novice.
So I only have column name, and trying to find which table it is belong to.
How do I find that in toad? Can someone help?
Most databases have tables that describe the contents of the databases. If you are using toad, then I might surmise that you are using Oracle.
If so, you can use:
select *
from syscolumns
where columnname = <whatever you are looking for>
And then lookup the referenceid in systables.
In many other databases, you can use:
select *
from INFORMATION_SCHEMA.columns
where column_name = <whatever you are looking for>
Something like this should work, it will return the column name you search and the associated table
SELECT c.name as columnname, t.name as tablename from sys.columns c
join sys.tables t on c.object_id = T.object_id
where c.name =' put the column you want to find here'

Calculate the maximum storage size of table record?

Is there a way to determine what the maximum size of a record would be in SQL Server past doing it by hand? For example:
CREATE TABLE test (
id INT PRIMARY KEY IDENTITY(1, 1),
name VARCHAR(256),
test_date DATETIME
)
so, if I'm not mistaken, when calculating that by hand that record could be a maximum of 272 bytes. However, I have a table with a lot more columns than that, and I need to do this for more than one table, so I wanted to know if I could do this with a simple query.
I can't find any information in INFORMATION_SCHEMA.TABLES or even INFORMATION_SCHEMA.COLUMNS where I figured I could do a simple SUM for example. Further, sysobjects and syscolumns don't seem to have the necessary information. The syscolumns table does have a length field but that's not the actual storage size.
Thanks all!
Try this:
Select schema_name(T.schema_id) As SchemaName,
T.Name As TableName,
Sum(C.max_length) As RowSize
From sys.tables T
Inner Join sys.columns C
ON T.object_id = C.Object_ID
INNER JOIN sys.types S
On C.system_type_id = S.system_type_Id
Group By schema_name(T.schema_id),
T.Name
Order By schema_name(T.schema_id),
T.Name