I’ve created a few table types in my database to be used as stored procedure parameters. These correspond to real database tables, so if they’re out of sync there’ll be a problem. I’d like to add a unit test that looks at the two and fails if they are different, but I’m not sure where to start.
I don’t know if there’s a recommended way to do this – I was going to try to somehow pull out the column information, loop through it and fail the test if they’re different, but it seems a bit fiddly.
Is there a better way?
For SQL Server 2008, take a look at the
sys.tables,sys.table_typesandsys.columnssystem tables.In one of my databases I have a table type called
candidateRoutesand a physical (real) table calledRouteAreaThe following two queries:
return:
and
so you could perhaps do something like this:
but including
scale, collation_name, is_nullable, etc., to find all columns that do not match. In my case, I get:If no rows are returned, the type and the table are the same.