Hi everyone,
I'm learning T-SQL and I have created a copy of the pubs database called
pubs2.
I both the original pubs database and pubs2 I have created an identical view
called TableFields.
In the database in which it resides this view retrieves: Every table in the
database as WS_TABLE, and Every field in the database as WS_FIELD (it also
retrieves the datatype and length of the field but that isn't important
right now).
I have been creating and dropping tables in pubs2 and also adding and
deleting fields.
I have written a SQL statement that compares the two databases and lists the
tables that are in pubs but not in pubs2
SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM pubs..TableFields DB1 LEFT JOIN pubs2..TableFields DB2 ON DB1.WS_TABLE
= DB2.WS_TABLE
WHERE DB2.WS_FIELD IS NULL
I would like to turn this statement into a stored procedure called
sfp_DB_Table_Compare which accepts the databases to be compared as
variables.
Ideally the syntax would be something like:
EXECUTE sfp_DB_Table_Compare pubs, pubs2
My failed attempt is as follows: -
CREATE PROCEDURE sfp_Db_Table_Compare
(@DBase1 varchar(20), @DBase2 varchar(20))
SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM DB1..TableFields DB1 LEFT JOIN DB2..TableFields DB2 ON DB1.WS_TABLE =
DB2.WS_TABLE
WHERE DB1 = @DBase1
AND DB2 = @Dbase2
AND DB2.WS_FIELD IS NULL
RETURN
Can anybody help me please?