0

我正在尝试加入这些陈述:

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。

希望我所追求的有意义。

4

2 回答 2

1

您可以使用cross join连接两个不相关的表:

SELECT  c.name COLLATE SQL_Latin1_General_CP1_CI_AS colName
,       o.name COLLATE SQL_Latin1_General_CP1_CI_AS tableName
,       'website' db 
,       pwd.[min]
,       pwd.[max]
,       pwd.[avg]
FROM    website.sys.all_columns as c 
INNER JOIN 
        website.sys.all_objects o 
ON      c.object_id = o.object_id 
CROSS JOIN
        (
        select min(len(Password)) as min, 
               max(len(Password)) as max, 
               avg(len(Password)) as avg 
        FROM   website.dbo.Password        
        ) as pwd
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 
,       pwd.[min]
,       pwd.[max]
,       pwd.[avg]
FROM    Customer.sys.all_columns as c 
INNER JOIN 
        Customer.sys.all_objects o 
ON      c.object_id = o.object_id 
CROSS JOIN
        (
        select min(len(Password)) as min, 
               max(len(Password)) as max, 
               avg(len(Password)) as avg 
        FROM    Customer.dbo.Password        
        ) as pwd
WHERE   c.name like '%password%' 
        AND type = 'U'
于 2012-10-11T08:45:24.677 回答
0

创建他们确实有共同点的任意东西并加入他们

Select * 
from
(
select 
       1 as RowID,
       min(len(Password)) as min,  
       max(len(Password)) as max,  
       avg(len(Password)) as avg  
FROM Customer.dbo.Password  

UNION ALL 
select 
       2,
       min(len(password2)) as min,  
       max(len(password2)) as max,  
       avg(len(password2)) as avg  
FROM website.dbo.password2 
) t1
inner join
(


SELECT 1 as RowID, 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 2, 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' 
) t2
    on t1.rowID = t2.rowID
于 2012-10-11T08:48:18.247 回答