Question : size of data returned by a query

how can you find the size of the data that a query brings back:
select col1,col2....col12 from tablea
how can you know how many bytes of data is being returned by the above query?

statistics io does not give this info..

thanks

Answer : size of data returned by a query

Maybe something like this (this won't work on system tables, just user tables). This is from AdventureWorks sample db. Take a look at the last column in the image

declare @exec varchar(max)
declare @table sysname
declare @schema sysname

set @table = 'Employee'
set @schema = 'HumanResources'
SELECT @exec = REPLACE(REPLACE(REPLACE('select *, DataLen=@calc FROM [@schema].[@table]',
                 '@calc', dbo.GetDataLenList(@table)),
                 '@table', @table),
                 '@schema', @schema)

print @exec
exec (@exec)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
CREATE FUNCTION [GetDataLenList] (
  @TableName SYSNAME
) RETURNS VARCHAR(MAX) AS BEGIN

  DECLARE @count INT
  DECLARE @ctr   INT
  DECLARE @ColName SYSNAME
  DECLARE @Schema  SYSNAME
  DECLARE @sql     VARCHAR(MAX)
  DECLARE @sqlExec VARCHAR(MAX)

  DECLARE @Cols TABLE (
    ID         INT IDENTITY(1, 1),
    ColumnName NVARCHAR(128),
    SchemaName NVARCHAR(128)
  )
  
  INSERT INTO @Cols 
  SELECT COLUMN_NAME, TABLE_SCHEMA
    FROM INFORMATION_SCHEMA.COLUMNS 
   WHERE TABLE_NAME = @TableName 

  SET @sql = ''
  SELECT @count = COUNT(*) FROM @Cols
  SET @ctr = 1
  WHILE @ctr <= @count BEGIN
    SELECT @ColName  = ColumnName, @Schema = SchemaName
     FROM @Cols
     WHERE ID = @ctr
     IF @ctr > 1 BEGIN
       SET @sql = @sql + ' + '
     END
     SET @sql = @sql + REPLACE('ISNULL(DATALENGTH(@@colName@@), 0)', '@@colName@@', QuoteName(@colName))
    SET @ctr = @ctr + 1
  END
  RETURN @sql
END
Random Solutions  
 
programming4us programming4us