Frage

I am trying to check whether a table exists or not in the database using AssertObjectExists. Actually I have 10 tables to check whether those tables exists or not. Since the test is verification of existence of tables. I want to put together in one test.

When I keep all assertions in one test, if any of the object assertion fails, the remaining assertions are not executing.

My goal is to check whether the tables are present from a set say 10 tables. And report the list of tables which doesn't exists. I am pasting the sample code below.

ALTER PROCEDURE [Test Tracker].[test TablesExists_01]
AS
BEGIN

-- Verify the existance of each table

EXEC tSQLt.AssertObjectExists @ObjectName = 'auth_user', 
   @Message = 'Unable to find auth_user Table' 

EXEC tSQLt.AssertObjectExists @ObjectName = 'auth_permissions',
   @Message = 'Unable to find auth_permissions Table' 

EXEC tSQLt.AssertObjectExists @ObjectName = 'auth_groups',
   @Message = 'Unable to find auth_groups Table' 


END;

Can someone redirect me in right path.

Edit: Solution Given by Brian

IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME = 'auth_user' AND TABLE_SCHEMA = @schema))
    SET @errorMessage = @errorMessage + 'Unable to find auth_user' + CHAR(10)

IF (NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME = 'auth_group' AND TABLE_SCHEMA = @schema))
    SET @errorMessage = @errorMessage + 'Unable to find auth_group' + CHAR(10)

IF LEN(@errorMessage) = 0
    PRINT 'All the Tables in Authentication exists'
ELSE
    EXEC tsqlt.Fail @Message = @errorMessage

In the above code CHAR(10) is new line code. I just modified it for having a nice console output

War es hilfreich?

Lösung

You might try this:

Declare @tableName as varchar(100)
set @tableName = 'auth_user'

IF (EXISTS (SELECT * 
                FROM INFORMATION_SCHEMA.TABLES 
                WHERE TABLE_SCHEMA = 'dbo' 
               AND  TABLE_NAME = @tableName ))
BEGIN
    --Do Stuff
END

set @tableName = 'auth_permissions'
...

Then just iterate through the rest of the table names. To make it very easy, make this a stored procedure that takes a single string as a parameter that is delimited like :

'auth_user|auth_permission|etc.' 

Then you could use a Split function to separate each inbound name into a virtual table you could then cursor through and get the answers to whether the table exist. Thus your stored procedure would be useful in any situation where you wanted to check the exist of 1 to many tables.

Andere Tipps

I would suggest a similar approach to Brian, but perhaps you can declare a table variable (@Expected) with a single column of the expected table names, then your test can be to select into a second table variable (@Actual) all those entries from INFORMATION_SCHEMA.tables inner joined to @Expected (specifiying schema, etc. in the where clause).

Then, you can use tSQLt.AssetEqualsTable to compare the contents of @Expected with @Actual - if they are the same (all objects exist) then your test will pass, but if not then the test will fail, and all mismatched rows (each indicating a missing object) will show up in the failure message.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top