我正在尝试加入这些陈述:
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
对这些:
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'
它们没有共同的列。前两个 SELECT 语句生成密码长度的 min、max 和 avg 三列,后两个生成密码所在位置的 colName、tableName 和 db 名称。
纯粹出于视觉原因,我想在一个表中显示两个结果集,以获取与适当的 colName、tableName 和 db 相对应的 min、max、avg。
我试过这样做:
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
不幸的是,这没有用。
我正在使用 Microsoft SQL Server 2008。
希望我所追求的有意义。