I am trying to join these statements:
select min(len(Password)) as min,
max(len(Password)) as max,
avg(len(Password)) as avg
FROM Customer.dbo.Password
UNION ALL
select min(len(password2)) as min,
max(len(password2)) as max,
avg(len(password2)) as avg
FROM website.dbo.password2
To these:
SELECT c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'website' db FROM website.sys.all_columns as c INNER JOIN website.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U'
UNION
SELECT c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'Customer' db FROM Customer.sys.all_columns as c INNER JOIN Customer.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U'
They have no columns in common. The first two SELECT statements produce three columns of the min, max and avg of password length and second two produce colName, tableName and db name of where the password is held.
I’d like to display both result sets in one table purely for visual reasons, to get the corresponding the min, max, avg to the appropriate colName, tableName and db.
I tried doing it this way:
DECLARE @numberTable TABLE (link nvarchar(500), max int, min int, avg int)
INSERT INTO @numberTable
select 'link', min(len(Password)) as min,
max(len(Password)) as max,
avg(len(Password)) as avg
FROM Customer.dbo.Password UNION ALL
select 'link', min(len(password2)) as min,
max(len(password2)) as max,
avg(len(password2)) as avg
FROM website.dbo.password2
SELECT * FROM @numberTable ORDER BY max
DECLARE @myTable TABLE (link nvarchar(500), colName nvarchar(500), tableName nvarchar(500), db nvarchar(500))
INSERT INTO @myTable
SELECT 'link', c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'website' db FROM website.sys.all_columns as c INNER JOIN website.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U' UNION
SELECT 'link', c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName, o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName, 'Customer' db FROM Customer.sys.all_columns as c INNER JOIN Customer.sys.all_objects o ON c.object_id = o.object_id WHERE c.name like '%password%' AND type = 'U'
SELECT * FROM @myTable ORDER BY db
SELECT link
FROM @myTable
LEFT OUTER JOIN @numberTable
ON @myTable.link = @numberTable.link
Unfortunately this didn’t work.
I’m using Microsoft SQL Server 2008.
Hope what I’m after makes sense.
You can use
cross jointo join two unrelated tables: