Sql-server – SQL Server Row Length

sql-serverstored-procedures

I'm attempting to determine the row length in bytes of a table by executing the following stored procedure:

CREATE TABLE #tmp 
(
  [ID] int, 
  Column_name varchar(640), 
  Type varchar(640), 
  Computed varchar(640), 
  Length int, 
  Prec int, 
  Scale int, 
  Nullable varchar(640), 
  TrimTrailingBlanks varchar(640), 
  FixedLenNullInSource varchar(640), 
  Collation varchar(256)
)
INSERT INTO #tmp exec sp_help MyTable
SELECT SUM(Length) FROM #tmp
DROP TABLE #tmp

The problem is that I don't know the table definition (data types, etc..) of the table returned by 'sp_help.'

I get the following error:

Insert Error: Column name or number of supplied values does not match table definition.

Looking at the sp_help stored procedure does not give me any clues.

What is the proper CREATE TABLE statement to insert the results of a sp_help?

Best Solution

How doing it this way instead?

CREATE TABLE tblShowContig
(
    ObjectName CHAR (255),
    ObjectId INT,
    IndexName CHAR (255),
    IndexId INT,
    Lvl INT,
    CountPages INT,
    CountRows INT,
    MinRecSize INT,
    MaxRecSize INT,
    AvgRecSize INT,
    ForRecCount INT,
    Extents INT,
    ExtentSwitches INT,
    AvgFreeBytes INT,
    AvgPageDensity INT,
    ScanDensity DECIMAL,
    BestCount INT,
    ActualCount INT,
    LogicalFrag DECIMAL,
    ExtentFrag DECIMAL
)
GO

INSERT tblShowContig
EXEC ('DBCC SHOWCONTIG WITH TABLERESULTS')
GO

SELECT * from tblShowContig WHERE ObjectName = 'MyTable'
GO