Get all Column Names being referenced in one particular Table only - sql

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.

Related

SQL to find the number of rows per table in a database

My goal is to create a spreadsheet of all the tables in the current database that will display the table name and the number of rows in each table. A bonus would be to also be able to display when anything was last entered or changed in the table. I'm not sure if the last part is possible though.
This is what I have so far... its nothing much but I just can't beat my head around how to add the per table row count to it.
SELECT name, type, type_desc
FROM [scheme].sys.all_objects
WHERE type = 'U'
You can use this query against the system catalog views:
SELECT
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, p.Rows
ORDER BY
t.Name
to get the desired result - all tables in the current database, and the number of rows in each.
CREATE TABLE #T
(TABLENAME VARCHAR(100) ,
COUNT INT)
INSERT INTO #T
EXEC SP_MSFOREACHTABLE 'SELECT ''?'' ,COUNT(*) FROM ?'
SELECT * FROM #T
DROP TABLE #T

Get list of tables with column value as a condition

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

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'

How do I check if one table data is being referred in other processes

How do I check if one table data is being referred in other processes / procedure / is being used in other tables in sybase?
Try this
SELECT DISTINCT o.name, o.xtype
FROM dbo.syscomments c
INNER JOIN dbo.sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE 'Table_Name_here'
-- and xtype = 'P' --un-comment if you want only procedures

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