SQL Data Compare
Latest version: 10.0
Knowledge Base
Using a filter on a column on related (joined) tables
Category: How do I?
Date: 19 Nov 2008
Product: SQL Data Compare
It can be useful to filter records by a column's relationship with a related table - but the "'WHERE clause" option in SQL Data Compare "Project Configuration" does not offer direct access to columns in two "related" tables. In this article we examine how a view can be used instead.
Consider two tables, Sales and LineItems. Lets say a record in LineItems belongs to a district in the Sales table. I want to only compare Lineitems records that belong in a certain district "Suffolk", and ignore all the other districts.
To run the example, create two databases, TEST and TEST2. Both have these databases have tables LineItems, Sales and a view View_1:
CREATE TABLE [dbo].[LineItems](
[ID] [smallint] NOT NULL,
[description] [nvarchar](50) NULL,
[SalesID] [smallint] NULL,
CONSTRAINT [PK_LineItems] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Sales](
[SalesID] [smallint] NOT NULL,
[SalesDescription] [nvarchar](50) NULL,
CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
(
[SalesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Create VIEW VIEW_1 AS
SELECT dbo.LineItems.ID, dbo.LineItems.description, dbo.LineItems.SalesID, dbo.Sales.SalesDescription
FROM dbo.LineItems INNER JOIN
dbo.Sales ON dbo.LineItems.SalesID = dbo.Sales.SalesID
WHERE (dbo.Sales.SalesDescription = 'Suffolk')
--Populate TEST with the following:
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (1, N'Fred ', 1)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (2, N'Chrs ', 1)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (3, N'Steve ', 1)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (4, N'Greg ', 2)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (5, N'Clare ', 2)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (6, N'David ', 2)
INSERT INTO [dbo].[LineItems] ([ID], [description], [salesID]) VALUES (7, N'Linda ', 3)
--And ...
INSERT INTO [dbo].[Sales] ([SalesID], [SalesDescription]) VALUES (1, N'Norfolk')
INSERT INTO [dbo].[Sales] ([SalesID], [SalesDescription]) VALUES (2, N'Suffolk')
INSERT INTO [dbo].[Sales] ([SalesID], [SalesDescription]) VALUES (3, N'Hampshire')
INSERT INTO [dbo].[Sales] ([SalesID], [SalesDescription]) VALUES (4, N'Essex')
Start SQL Data Compare - set the Project Configuration Options / Include Views to ticked on.
Sychronize TEST and TEST2. Now when clicking on VIEW_1 row the inserts created by the update script are:
INSERT INTO [dbo].[View_1] ([ID], [salesID], [SalesDescription], [description]) VALUES (4, 2, N'Suffolk', N'Greg ')
INSERT INTO [dbo].[View_1] ([ID], [salesID], [SalesDescription], [description]) VALUES (5, 2, N'Suffolk', N'Clare ')
INSERT INTO [dbo].[View_1] ([ID], [salesID], [SalesDescription], [description]) VALUES (6, 2, N'Suffolk', N'David ')
We are therefore limiting the update SQL script to only the "Suffolk". In this way we can see how a simple view can control the comparison output by linking related tables.
Was this article helpful?
SQL Data Compare
- Logging and log files
- Internal Connection Fatal Error in versions 4 and 5
- NULL textptr passed to UPDATETEXT function when running synchronization
- How much free hard disk space is required?
- Comparing the data of two tables in the same database
- Suggestions to combat comparison and synchronization performance issues
- SQL comparison and synchronization automation capabilities
- SqlNullValueException occurring in synchronization wizard
- SQL Data Compare command-line XML argument file examples
- Using Windows authentication logons between domains
- Using a filter on a column on related (joined) tables
- Determining minimum database permissions for SQL Data Compare
- Tables with compound keys may not be mapped automatically
- Best practices for synchronizing data
- Cleaning up a SQL script after SQL Compare or SQL Data Compare
- Troubleshooting System.OutOfMemoryException during comparison
- Exception thrown by db reader 1
- Reseed applying "incorrect" identity values
- Running migration scripts using SqlCmd.exe
- This SQL Server has been optimized for x concurrent queries.
- SQL Data Compare showing differences in 2 identical databases
- How to force SQL Compare and SQL Data Compare to use an Encrypted Connection
- Improving the performance of SQL Data Compare
- Logging and log files
- Case-sensitive comparisons
- Tables or views that could not be compared
all SQL products
- Compatibility of Red Gate tools in 64-bit environments
- Application has encountered an error and needs to close
- Error message after installing SQL Toolbelt - The description for Event ID ( 1 ) in Source ( nview_info ) cannot be found.
- Changing the temporary directory used by the installer
- Toolbelt Installer "hanging" while "scanning volumes"
- Login failing with "trusted SQL Server connection" error when using RunAs
all products
- Some Red Gate products identified as containing a trojan by Anti-Virus software
- Activation may fail with Unknown Error -1
- Product uses web help although a CHM file is available locally
- Argument exception resulting from missing environment variable
- Check for updates may fail when used through proxies
- 'Unidentified Publisher' error when repairing or uninstalling
- Licensing activates product as standard edition
- Moving Red Gate software products to another machine
- Red Gate tools log locations
- The application UI opening slowly when there is no internet access
SQL Data Compare
- Activating your products
- Activating your products
- Getting help offline
- Getting better performance out of SQL Data Compare
- Release notes - version 10.xx
- SQL Data Compare release notes - version 7.xx
- SQL Data Compare release notes - version 8xx
- SQL Data Compare release notes - version 9.xx
all SQL products
all products
- Red Gate product acknowledgements
- Activating your products
- Activating your products
- Red Gate bundle history
- Check for updates
- Troubleshooting Check for Updates errors
- Current versions
- Deactivating your products
- Installing Red Gate products from the .msi file
- Requesting additional activations
- Serial numbers for bundles
- Reactivating using a different serial number
- Extending your trial
- Finding your serial numbers
- Moving a serial number from one computer to another
- No response received for manual activation
- Licensing and activation resources
- Licensing and activation resources
- Troubleshooting licensing and activation errors
- Licensing and activation FAQs
- Red Gate tools log file locations
- Download old versions of products
- Download product prerequisites & utilities
- Support & upgrades
- Upgrading your software
- Upgrading FAQs

Using SQL Data Compare