sys.syscomments.name length > 4,000 - sql

The code below displays, among other things, the source code for all the triggers in a database
SELECT so.name AS trigger_name,
s.name AS table_schema,
t.name AS table_name,
LEN (sc.[text]) as len,
sc.[text] AS trigger_content
FROM [GDI-193-DEV].dbo.sysobjects so
INNER JOIN [GDI-193-DEV].sys.tables t ON so.parent_obj = t.object_id
INNER JOIN [GDI-193-DEV].sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN [GDI-193-DEV].sys.syscomments sc ON so.id = sc.id
WHERE so.type = 'TR'
The problem is if the length of the trigger source code is more than 4,000 characters it ends up spanning two records in sys.syscomments.
So, for example, if the trigger source is 4,700 characters it will be spread across 2 records in sys.syscomments. sys.syscomments.TEXT in the first record will have a length of 4,000 while the remaining 700 characters will be put into another record.
This is causing problems for me as I am trying to compare the complete trigger source code between two databases.
This seem like some strange behavior. Am I missing something? Is there a different way of getting the source code that avoids this problem?
Thanks in advance,

I had the same problem, and found out that there's another way to get untrimmed definitions. Try if entire code of your trigger shows, and then you can work your way to joinning this to other system tables/views:
select top 100
*
from sys.sql_modules
where 1=1
and definition like '%trigger%'

Try using object_definition like this:
SELECT so.name AS trigger_name,
s.name AS table_schema,
t.name AS table_name,
object_definition(so.object_id) AS trigger_content
FROM [GDI-193-DEV].dbo.sysobjects so
INNER JOIN [GDI-193-DEV].sys.tables t ON so.parent_obj = t.object_id
INNER JOIN [GDI-193-DEV].sys.schemas s ON t.schema_id = s.schema_id
WHERE so.type = 'TR'

Related

How to get table row count in SQL Server?

There's a requirement where I need to get the table row count information without using count function. Currently I have used the below code to achieve it:
SELECT
CONCAT(schemas.name, '.', tables.name) as tableName,
partitions.rows AS tableRowCount
FROM
sys.partitions
JOIN
sys.tables ON tables.object_id = partitions.object_id
JOIN
sys.schemas ON tables.schema_id = schemas.schema_id
But for some reason, this query is not working properly as expected. Having said that, in some cases tableRowCount is different if the same is checked using the count function.
Is there a way to overcome this challenge?
Please note I tried using sys.dm_db_partition_stats table as well.
However, I get this error:
Msg 104385, Level 16, State 1, Line 9
Catalog view 'dm_db_partition_stats' is not supported in this version.
Please help.
This should be much faster than using COUNT.
SELECT SUM(p.rows) FROM sys.partitions AS p
INNER JOIN sys.tables AS t ON p.[object_id] = t.[object_id]
INNER JOIN sys.schemas AS s ON s.[schema_id] = t.[schema_id]
WHERE t.name = N'(yourtable)' AND s.name = N'dbo' AND p.index_id IN (0,1);
Note that this approach is in no way portable between RDBMSs.

SQL : How to search column name in DB (within set of tables)

If my database has large set of tables then -
Is there any way / query which will search - which table contains particular column name ?
Eg. I want to know the name of table which contains column par_token.
How can I achieve this? - I am using SQL Management Studio 2014.
You can use the sys objects:
SELECT s.[name] AS SchemaName,
t.[name] AS TableName
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.[name] = N'par_token';
I recommend using INFORMATION_SCHEMA.COLUMNS. The INFORMATION_SCHEMA views are (relatively) standard views available across databases. And, you don't need any joins:
select c.*
from information_schema.columns c
where c.column_name = 'par_token';

Find all Tables that contain Column Name; Filter Empties

I've been using the SQL found here:
Find all tables containing column with specified name
to great success. It allows me to find all tables that contain a certain column. My issue is that the database I'm working on seems to have a lot of empty tables (maybe around half of my results are empties). I was wondering if there was a way to modify the code in the link such that empty rows/columns are not presented.Below is the code from the link:
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 LIKE '%MyName%'
ORDER BY TableName
,ColumnName;
Thank you,
Something like this may work without huge effort:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
,p.rows
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
INNER JOIN sys.partitions p
on t.object_id = p.object_id
WHERE c.name LIKE '%p%'
AND p.rows > 0
ORDER BY TableName
,ColumnName;
Just note that sys.partitions isn't guaranteed to be accurate about row counts; sys.dm_db_index_physical_stats may be better (see https://dba.stackexchange.com/questions/55124/how-accurate-is-the-sys-partition-rows-column). This might give you a better count but may have more locking issues if you're using AlwaysOn:
SELECT c.name AS 'ColumnName'
,t.name AS 'TableName'
,ips.record_count
FROM sys.columns c
INNER JOIN sys.tables t
ON c.object_id = t.object_id
CROSS APPLY sys.dm_db_index_physical_stats(DB_ID(), t.object_id, null, null, 'DETAILED') ips
WHERE c.name LIKE '%p%'
AND ips.record_count > 0
ORDER BY TableName
,ColumnName;
If you really need 100% reliability on this, you'd have actually do a COUNT(*) on each table (using Dynamic SQL) you want to check, but this is probably good enough.

How to determine if a specific set of tables in a database are empty

I have database A which contains a table (CoreTables) that stores a list of active tables within database B that the organization's users are sending data to.
I would like to be able to have a set-based query that can output a list of only those tables within CoreTables that are populated with data.
Dynamically, I normally would do something like:
For each row in CoreTables
Get the table name
If table is empty
Do nothing
Else
Print table name
Is there a way to do this without a cursor or other dynamic methods? Thanks for any assistance...
Probably the most efficient option is:
SELECT c.name
FROM dbo.CoreTables AS c
WHERE EXISTS
(
SELECT 1
FROM sys.partitions
WHERE index_id IN (0,1)
AND rows > 0
AND [object_id] = OBJECT_ID(c.name)
);
Just note that the count in sys.sysindexes, sys.partitions and sys.dm_db_partition_stats are not guaranteed to be completely in sync due to in-flight transactions.
While you could just run this query in the context of the database, you could do this for a different database as follows (again assuming that CoreTables does not include schema in the name):
SELECT c.name
FROM DatabaseA.CoreTables AS c
WHERE EXISTS
(
SELECT 1
FROM DatabaseB.sys.partitions AS p
INNER JOIN DatabaseB.sys.tables AS t
ON p.[object_id] = t.object_id
WHERE t.name = c.name
AND p.rows > 0
);
If you need to do this for multiple databases that all contain the same schema (or at least overlapping schema that you're capturing in aggregate in a central CoreTables table), you might want to construct a view, such as:
CREATE VIEW dbo.CoreTableCounts
AS
SELECT db = 'DatabaseB', t.name, MAX(p.rows)
FROM DatabaseB.sys.partitions AS p
INNER JOIN DatabaseB.sys.tables AS t
ON p.[object_id] = t.[object_id]
INNER JOIN DatabaseA.dbo.CoreTables AS ct
ON t.name = ct.name
WHERE p.index_id IN (0,1)
GROUP BY t.name
UNION ALL
SELECT db = 'DatabaseC', t.name, rows = MAX(p.rows)
FROM DatabaseC.sys.partitions AS p
INNER JOIN DatabaseC.sys.tables AS t
ON p.[object_id] = t.[object_id]
INNER JOIN DatabaseA.dbo.CoreTables AS ct
ON t.name = ct.name
WHERE p.index_id IN (0,1)
GROUP BY t.name
-- ...
GO
Now your query isn't going to be quite as efficient, but doesn't need to hard-code database names as object prefixes, instead it can be:
SELECT name
FROM dbo.CoreTableCounts
WHERE db = 'DatabaseB'
AND rows > 0;
If that is painful to execute you could create a view for each database instead.
In SQL Server, you can do something like:
SELECT o.name, st.row_count
FROM sys.dm_db_partition_stats st join
sys.objects o
on st.object_id = o.object_id
WHERE index_id < 2 and st.row_count > 0
By the way, this specifically does not use OBJECT_ID() or OBJECT_NAME() because these are evaluated in the current database. The above code continues to work for another database, using 3-part naming. This version also takes into account multiple partitions:
SELECT o.name, sum(st.row_count)
FROM <dbname>.sys.dm_db_partition_stats st join
<dbname>.sys.objects o
on st.object_id = o.object_id
WHERE index_id < 2
group by o.name
having sum(st.row_count) > 0
something like this?
//
foreach (System.Data.DataTable dt in yourDataSet.Tables)
{
if (dt.Rows.Count != 0) { PrintYourTableName(dt.TableName); }
}
//
This is a way you can do it, that relies on system tables, so be AWARE it may not always work in future versions of SQL. With that strong caveat in mind.
select distinct OBJECT_NAME(id) as tabName,rowcnt
from sys.sysindexes si
join sys.objects so on si.id=si.id
where indid=1 and so.type='U'
You would add to the where clause the tables you are interested in and rowcnt <1

List of Stored Procedure from Table

I have a huge database with 100's of tables and stored procedures. Using SQL Server 2005, how can I get a list of stored procedures that are doing an insert or update operation on a given table.
sys.sql_dependencies has a list of entities with dependencies, including tables and columns that a sproc includes in queries. See this post for an example of a query that gets out dependencies. The code snippet below will get a list of table/column dependencies by stored procedure
select sp.name as sproc_name
,t.name as table_name
,c.name as column_name
from sys.sql_dependencies d
join sys.objects t
on t.object_id = d.referenced_major_id
join sys.objects sp
on sp.object_id = d.object_id
join sys.columns c
on c.object_id = t.object_id
and c.column_id = d.referenced_minor_id
where sp.type = 'P'
select
so.name,
sc.text
from
sysobjects so inner join syscomments sc on so.id = sc.id
where
sc.text like '%INSERT INTO xyz%'
or sc.text like '%UPDATE xyz%'
This will give you a list of all stored procedure contents with INSERT or UPDATE in them for a particular table (you can obviously tweak the query to suit). Also longer procedures will be broken across multiple rows in the returned recordset so you may need to do a bit of manual sifting through the results.
Edit: Tweaked query to return SP name as well. Also, note the above query will return any UDFs as well as SPs.
Use sys.dm_sql_referencing_entities
Note that sp_depends is obsoleted.
MSDN Reference
You could try exporting all of your stored procedures into a text file and then use a simple search.
A more advanced technique would be to use a regexp search to find all SELECT FROM and INSERT FROM entries.
If you download sp_search_code from Vyaskn's website it will allow you to find any text within your database objects.
http://vyaskn.tripod.com/sql_server_search_stored_procedure_code.htm
This seems to work:
select
so.name as [proc],
so2.name as [table],
sd.is_updated
from sysobjects so
inner join sys.sql_dependencies sd on so.id = sd.object_id
inner join sysobjects so2 on sd.referenced_major_id = so2.id
where so.xtype = 'p' -- procedure
and is_updated = 1 -- proc updates table, or at least, I think that's what this means
SELECT Distinct SO.Name
FROM sysobjects SO (NOLOCK)
INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
AND SO.Type = 'P'
AND (SC.Text LIKE '%UPDATE%' OR SC.Text LIKE '%INSERT%')
ORDER BY SO.Name
This link was used as a resource for the SP search.