SQL Server Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Tue, 19 Dec 2023 05:34:58 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 SQL Server Archives - Tech Insights 32 32 230003556 How to Get the size of all Tables in a Database https://reactconf.org/sql-server-get-size-of-all-tables-in-a-database/ https://reactconf.org/sql-server-get-size-of-all-tables-in-a-database/#respond Tue, 19 Dec 2023 05:34:58 +0000 http://www.sqlneed.com/?p=534 In this guide, we will learn How to Get the size of all Tables in a Database using SQL Server. There are multiple ways, let’s take a look at them …

The post How to Get the size of all Tables in a Database appeared first on Tech Insights.

]]>
In this guide, we will learn How to Get the size of all Tables in a Database using SQL Server. There are multiple ways, let’s take a look at them one by one.

#Solution-1: SQL Query

The query joins information from the system catalog views such as “sys.tables”, “sys.indexes”, ”sys.partitions”, and “sys.allocation_units” to calculate the size of each table in KB(kilobytes) and as well as MB(megabytes).

It provides a detailed overview of all tables in the specified database, including the table name row count, total size, used size, and unused side. The results are ordered by total size in descending order, so you can easily identify the largest tables.

SELECT 
    t.name AS TableName,
    s.name AS SchemaName,
    p.rows,
    SUM(a.total_pages) * 8 AS TotalSpaceInKB, 
    CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceInMB,
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceInMB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceInKB,
    CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceInMB
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
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.name NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.object_id > 255 
GROUP BY 
    t.name, s.name, p.rows
ORDER BY 
    TotalSpaceInMB DESC, t.name;

Results:

How to Get size of all Tables in a Database

#Solution-2:CTE Query

with CTE as (  
  SELECT  
  t.name as TableName,  
  SUM (s.used_page_count) as used_pages_count,  
  SUM (CASE  
              WHEN (i.index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)  
              ELSE lob_used_page_count + row_overflow_used_page_count  
          END) as pages  
  FROM sys.dm_db_partition_stats  AS s   
  JOIN sys.tables AS t ON s.object_id = t.object_id  
  JOIN sys.indexes AS i ON i.[object_id] = t.[object_id] AND s.index_id = i.index_id  
  GROUP BY t.name  
  )  
  ,cte2 as(select  
      cte.TableName,   
      (cte.pages * 8.) as TableSizeInKB,   
      ((CASE WHEN cte.used_pages_count > cte.pages   
                  THEN cte.used_pages_count - cte.pages  
                  ELSE 0   
            END) * 8.) as IndexSizeInKB  
  from CTE  
 )  
 select TableName,TableSizeInKB,IndexSizeInKB,  
 case when (TableSizeInKB+IndexSizeInKB)>1024*1024   
 then cast((TableSizeInKB+IndexSizeInKB)/1024*1024 as varchar)+'GB'  
 when (TableSizeInKB+IndexSizeInKB)>1024   
 then cast((TableSizeInKB+IndexSizeInKB)/1024 as varchar)+'MB'  
 else cast((TableSizeInKB+IndexSizeInKB) as varchar)+'KB' end [TableSizeIn+IndexSizeIn]  
 from CTE2  
 order by 2 desc

Result:

How to Get size of all Tables in a Database

#Solution-3:T-SQL |Stored Procedure

-- Using T-SQL
DECLARE @table_name sysname 
DECLARE table_list_cursor 
CURSOR FOR SELECT TABLE_NAME from INFORMATION_SCHEMA.TABLES 
where TABLE_TYPE='BASE TABLE' 
IF EXISTS ( SELECT * FROM    tempdb.INFORMATION_SCHEMA.COLUMNS WHERE    table_name = '##TABLE_RES')
BEGIN    
DROP TABLE ##TABLE_RES END CREATE TABLE ##TABLE_RES( Name nvarchar(255), 
Rows int, reserved varchar(18), Data varchar(18), index_size varchar(18),
Unused varchar(18)) OPEN table_list_cursor 
FETCH NEXT FROM table_list_cursor INTO @table_name 
INSERT INTO ##TABLE_RES exec sp_spaceused @table_name
WHILE @@FETCH_STATUS = 0 
BEGIN    FETCH NEXT FROM table_list_cursor INTO @table_name
INSERT INTO ##TABLE_RES exec sp_spaceused @table_name 
END CLOSE table_list_cursor 
DEALLOCATE table_list_cursor 
SELECT * from ##TABLE_RES order by rows desc
How to Get size of all Tables in a Database

#Solution-4: To Get the size of all tables in the SQL Server Database

In SQL Server, it is an easier way to get the size of all tables in the database is to use the Standard Report feature available in SQL Server Management Studio.

  • Open the SSMS (SQL Server Management Studio)
  • Now, Right Click on the Selected Database.
  • Select Reports
  • Next, Select Standard Reports
  • Then Click on Disk Usage by Table
How to Get size of all Tables in a Database using SQL Server

Output:

See Also:

The post How to Get the size of all Tables in a Database appeared first on Tech Insights.

]]>
https://reactconf.org/sql-server-get-size-of-all-tables-in-a-database/feed/ 0 534
How to Find All Identity Columns in the Database https://reactconf.org/find-all-identity-columns-in-the-database/ https://reactconf.org/find-all-identity-columns-in-the-database/#respond Sat, 04 Nov 2023 09:16:06 +0000 http://www.sqlneed.com/?p=573 In this article, we will learn How to Find all Identity Columns in the Database. You can typically query the system catalog or information schema, to find identity columns in …

The post How to Find All Identity Columns in the Database appeared first on Tech Insights.

]]>
In this article, we will learn How to Find all Identity Columns in the Database. You can typically query the system catalog or information schema, to find identity columns in different database management systems.

Here’s a Query to find all identity columns in a database.

Using SQL Server to Find All Identity Columns in the Database

SELECT 
	OBJECT_SCHEMA_NAME(tables.object_id, db_id())
	AS SchemaName,
	tables.name As TableName,
	columns.name as ColumnName
FROM sys.tables tables 
	JOIN sys.columns columns 
ON tables.object_id=columns.object_id
WHERE columns.is_identity=1

This query retrieves the table and column names of identity columns in a Database.

Find All identity columns in the database

Another way to find all identity columns in the Database. It contains details of all identity columns in a database along with their seed value, increment value, and other information. The below query for the same is given.

SELECT 
	OBJECT_SCHEMA_NAME(tables.object_id, db_id())
	AS SchemaName,
	tables.name As TableName,
	identity_columns.name as ColumnName,
	identity_columns.seed_value,
	identity_columns.increment_value,
	identity_columns.last_value
FROM sys.tables tables 
	JOIN sys.identity_columns identity_columns 
ON tables.object_id=identity_columns.object_id
Find All Identity Columns in the Database

Read Also:

The post How to Find All Identity Columns in the Database appeared first on Tech Insights.

]]>
https://reactconf.org/find-all-identity-columns-in-the-database/feed/ 0 573
Top 15 SQL Server Management Studio Keyboard Shortcuts and Tips https://reactconf.org/top-15-sql-server-management-studio-keyboard-shortcuts-and-tips/ https://reactconf.org/top-15-sql-server-management-studio-keyboard-shortcuts-and-tips/#respond Wed, 26 Apr 2023 03:45:43 +0000 http://www.sqlneed.com/?p=418 Are you weary of navigating SQL Server Management Studio’s (SSMS) menus and choices? You may save a lot of time and effort by using keyboard shortcuts. We’ll go over the …

The post Top 15 SQL Server Management Studio Keyboard Shortcuts and Tips appeared first on Tech Insights.

]]>
Are you weary of navigating SQL Server Management Studio’s (SSMS) menus and choices? You may save a lot of time and effort by using keyboard shortcuts. We’ll go over the Top 15 SQL Server Management Studio Keyboard Shortcuts and Tips in this article to increase your productivity.

An effective tool for administering SQL Server databases is called SQL Server Management Studio (SSMS). Users can communicate with SQL Server via its graphical user interface (GUI) without having to write any code. However, navigating SSMS with the mouse can be cumbersome and ineffective. Tasks can be completed more quickly and conveniently by using keyboard shortcuts.

 SQL Server Management Studio (SSMS) – what is it?

Users can administer SQL Server databases using the free Microsoft application SQL Server Management Studio (SSMS). For activities like building databases, tables, and stored procedures as well as running queries and managing users, it offers a user-friendly interface.

Why should you utilize keyboard shortcuts?

Keyboard shortcuts enable users to complete activities quickly and efficiently by avoiding the need to travel through menus and options. This saves time and effort, which is especially beneficial for recurring jobs. Keyboard shortcuts also lessen the possibility of errors by requiring less mouse movement and clicking.

More Related Post Calculate Date & Time in SQL Server

Top 15 SSMS Keyboard Shortcuts and Tips

Keyboard shortcuts for opening windows

  • Ctrl+T – Opens a new query window.
  • Ctrl+Shift+N – Opens a new solution.
  • Ctrl+Shift+O – Opens a file.

Keyboard shortcuts for managing tabs

  • Ctrl+Tab – Switches between open tabs.
  • Ctrl+F4 – Closes the current tab.
  • Ctrl+Shift+T – Reopens the last closed tab.

Keyboard shortcuts for executing queries

  • F5 – Executes the query.
  • Ctrl+Shift+M – Executes the query and includes the actual execution plan.
  • Ctrl+R – Toggles the display of the query results pane.

Keyboard shortcuts for formatting code

  • Ctrl+K, Ctrl+F – Formats the entire document.
  • Ctrl+K, Ctrl+D – Formats the current selection.
  • Ctrl+K, Ctrl+C – Comments on the current selection.
  • Ctrl+K, Ctrl+U – Uncomments the current selection.

Keyboard shortcuts for debugging

  • F11 – Toggles a breakpoint on the current line.
  • F5 – Starts debugging.
  • Shift+F5 – Stops debugging.
  • F10 – Steps over the current line.
  • F11 – Steps into the current line.

Keyboard shortcuts for searching and replacing

  • Ctrl+F – Opens the Find dialog.
  • Ctrl+Shift+F – Opens the Find and Replace dialog.
  • F3 – Finds the next occurrence of the current search term.
  • Shift+F3 – Finds the previous occurrence of the current search term.

Keyboard shortcuts for commenting and uncommenting code

  • Ctrl+K, Ctrl+C – Comments on the current selection.
  • Ctrl+K, Ctrl+U – Uncomments the current selection.

Keyboard shortcuts for displaying the execution plan

  • Ctrl+M – Toggles the display of the actual execution plan.

Keyboard shortcuts for managing connections

  • Ctrl+Alt+C – Connects to a database.
  • Ctrl+Alt+D – Disconnects from a database.

Keyboard shortcuts for managing objects

  • F7 – Opens the Object Explorer.
  • F8 – Toggles the display of the Object Explorer Details.

Keyboard shortcuts for managing indexes

  • Alt+F1 – Displays the index details.

Keyboard shortcuts for managing databases

  • Ctrl+D – Opens the Database Properties dialog.
  • Ctrl+Alt+G – Generates a script for a database.

Keyboard shortcuts for managing tables

  • Alt+F1 – Displays the table details.

Keyboard shortcuts for managing views

  • Alt+F1 – Displays the view details.

Keyboard shortcuts for managing stored procedures

  • Ctrl+Shift+M – Executes a stored procedure.
  • Alt+F1 – Displays the stored procedure details.

Conclusion

SSMS keyboard shortcuts can boost your productivity by allowing you to complete activities more quickly and efficiently. We covered the top 15 SSMS keyboard shortcuts and tips in this article, including keyboard shortcuts for opening windows, managing tabs, executing queries, formatting code, debugging, searching and replacing, commenting and uncommenting code, displaying the execution plan, and managing connections, objects, indexes, databases, tables, views, and stored procedures.

The post Top 15 SQL Server Management Studio Keyboard Shortcuts and Tips appeared first on Tech Insights.

]]>
https://reactconf.org/top-15-sql-server-management-studio-keyboard-shortcuts-and-tips/feed/ 0 418