79367693

Date: 2025-01-18 17:57:41
Score: 1
Natty:
Report link

You can get size of table (total no of bytes reserved by table data) using below query:

DECLARE @TableSchema VARCHAR(50) = 'dbo', @TableName VARCHAR(50) = 'Student', @Qry VARCHAR(MAX)

SELECT 
    @Qry = CONCAT('SELECT ', STRING_AGG(CONCAT('SUM(DATALENGTH(',[COLUMN_NAME],'))'), ' + '), ' [Total Bytes] FROM [', @TableSchema, '].[', @TableName,']')
FROM 
    INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = @TableSchema
        AND TABLE_NAME = @TableName

EXEC(@Qry)

Just replace value of @TableSchema and @TableName variables for which you want to get size.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @TableSchema
  • User mentioned (0): @TableName
  • Low reputation (1):
Posted by: Harsh Varde