My goal is to get row counts from the temp tables using code similar to below and quickly validate that the temp tables are working without typing each of them individually. I can see all of the local temp tables from the session using this code:
SELECT *
FROM tempdb.sys.tables
WHERE name LIKE '#%'
The results I want is something like:
name
row_count
#temp1
2000
#temp2
0
#temp3
0
Thank you for your time.
The names are not going to match exactly, but if you're using this to eyeball things, that shouldn't matter too much.
SELECT t.name, p.rows
FROM tempdb.sys.tables as t
join tempdb.sys.partitions as p
on p.object_id = t.object_id
and p.index_id in (0, 1)
WHERE name LIKE '#%';
When writing SQL, I constantly find myself checking the structure of a table / view or selecting the top 100 rows to get a preview of the data.
For instance, when I have something like this (and I have forgotten the structure of table2)...
SELECT
FROM table1 INNER JOIN table2 ON table1.fieldX = table2.???
I would like to highlight a table / view name and then pass it to a SQL query by passing a parameter, say table2 to either of the following 2 queries
SELECT TOP 100 * FROM table2
or
SELECT o.Type, o.Type_Desc, o.name AS ObjectName, c.name AS ColumnName
FROM sys.objects AS o INNER JOIN sys.columns AS c ON o.object_id = c.object_id
WHERE o.name = N'table2' AND o.Type IN (N'U', N'V')
ORDER BY o.name,c.column_id,c.name
I use SSMS Boost (it's my favorite SSMS addin) but can't figure out how to use functionality (Auto Replacement, Shortcut, Macros) in order to accomplish what I want here.
Has anyone ever set this up?
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'
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
I created a table on development environment for testing purpose and there are few sp's which are refreing this table. Now I have have to drop this table as well as identify all sp's which are referring this table. I am facing difficulty to find list of all sp's. Please suggest some query by assuming that the table name is 'x' and database is sql server 2005.
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%TableNameOrWhatever%'
BTW -- here is a handy resource for this type of question: Querying the SQL Server System Catalog FAQ
The following works on SQL2008 and above. Provides a list of both stored procedures and functions.
select distinct [Table Name] = o.Name, [Found In] = sp.Name, sp.type_desc
from sys.objects o inner join sys.sql_expression_dependencies sd on o.object_id = sd.referenced_id
inner join sys.objects sp on sd.referencing_id = sp.object_id
and sp.type in ('P', 'FN')
where o.name = 'YourTableName'
order by sp.Name
sometimes above queries will not give correct result, there is built in stored procedure available to get the table dependencies as:
EXEC sp_depends #objname = N'TableName';
A non-query way would be to use the Sql Server Management Studio.
Locate the table, right click and choose "View dependencies".
EDIT
But, as the commenters said, it is not very reliable.
In management studio you can just right click to table and click to 'View Dependencies'
than you can see a list of Objects that have dependencies with your table :
The following query will fetch all Stored Procedure names and the corresponding definition of those SP's
select
so.name,
text
from
sysobjects so,
syscomments sc
where
so.id = sc.id
and UPPER(text) like '%<TABLE NAME>%'
SELECT
o.name
FROM
sys.sql_modules sm
INNER JOIN sys.objects o ON
o.object_id = sm.object_id
WHERE
sm.definition LIKE '%<table name>%'
Just keep in mind that this will also turn up SPs where the table name is in the comments or where the table name is a substring of another table name that is being used. For example, if you have tables named "test" and "test_2" and you try to search for SPs with "test" then you'll get results for both.
The query below works only when searching for dependencies on a table and not those on a column:
EXEC sp_depends #objname = N'TableName';
However, the following query is the best option if you want to search for all sorts of dependencies, it does not miss any thing. It actually gives more information than required.
select distinct
so.name
--, text
from
sysobjects so,
syscomments sc
where
so.id = sc.id
and lower(text) like '%organizationtypeid%'
order by so.name
SELECT DISTINCT OBJECT_NAME(OBJECT_ID),
object_definition(OBJECT_ID)
FROM sys.Procedures
WHERE object_definition(OBJECT_ID) LIKE '%' + 'table_name' + '%'
GO
This will work if you have to mention the table name.
You have basically 2 options:
----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'
These 2 queries will get you all the stored procedures that are referring the table you desire. This query relies on 2 sys tables which are sysobjects and syscomments. The sysobjects is where all of your DB object names are stored this includes the stored procedures.
The syscomments contains the text for all of your procedures.
If you query:
SELECT * FROM syscomments
You'll have a table containing the id which is the mapping to the sysobjects table with the text contained in the stored procedures as the last column.
This useful query also works if you are using Azure SQL / Synapse Anlaytics
SELECT DISTINCT o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE m.definition Like '%Table_Name%'
Try This
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%your table name%'
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'