2015-03-31 69 views
1

我在执行下面的查询时遇到错误。我做错了什么?请帮我修正语法错误

Msg 512,Level 16,State 1,Line 3 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或当子查询用作表达式时,这是不允许的。

select 

    so.name 'Table Name' 
    ,so.id 'Table ID' 
    ,so.xtype 
    ,sc.name 'Column Name' 
    ,sc.id 'Column ID' 
    ,sf.constid 
    ,sf.fkeyid 'Object ID of the table with FOREIGN KEY' 
    ,sf.rkeyid 'Referenced Table ID' 
    ,(select o.name 'Referenced Table' 
    from sysforeignkeys f 
    inner join sysobjects o 
     on o.id=f.rkeyid 
     where o.xtype='U') 

    from sysobjects so 
    inner join syscolumns sc 
    on so.id=sc.id 
     inner join sysforeignkeys sf 
    on so.id=sf.fkeyid 
    where so.xtype='U' 
    and (sc.name like 'SSN' 
    OR sc.name LIKE 'ssn%'   
    OR sc.name LIKE 'ssn%'   
     OR sc.name LIKE '%_ssn%'   
    OR sc.name LIKE '_ocsecno'   
    OR sc.name LIKE 'Ssn%'); 
+1

像=“SSN”要么使用LIKE或=,既会造成概率 – 2015-03-31 18:09:14

+0

标题可以改善 – rpax 2015-03-31 20:19:05

回答

1

我不认为你的查询是正确的,因为它没有引用您的系统对象化名“所以”的方式。试试这个。另外我不认为你需要这么长的where子句。

select so.name  [Table Name] 
     ,so.id  [Table ID] 
     ,so.xtype 
     ,sc.name [Column Name] 
     ,sc.id  [Column ID] 
     ,sf.constid 
     ,sf.fkeyid [Object ID of the table with FOREIGN KEY] 
     ,sf.rkeyid [Referenced Table ID] 
     ,zz.name [Reference Table] 
from sysobjects so 
inner join syscolumns sc  on so.id = sc.id 
inner join sysforeignkeys sf on so.id = sf.fkeyid 

--Use a join here for the reference table column 
inner join sysobjects zz  on zz.id = sf.rkeyid 

where so.xtype='U' 
     AND(
      sc.name LIKE '%ssn%' 
      OR sc.name LIKE '_ocsecno' 
      ) 
+0

当我试过你的版本时,我收到了味精4104。消息4104,级别16,状态1,行1 无法绑定多部分标识符“zz.id”。 消息4104,级别16,状态1,行1 无法绑定多部分标识符“zz.name”。 – user3561219 2015-03-31 19:39:45

+0

你的SQL Server是否区分大小写? – Stephan 2015-03-31 19:42:57

+1

是的。非常感谢您的帮助。它现在有效。 – user3561219 2015-03-31 19:45:26

1
select so.name  [Table Name] 
     ,so.id  [Table ID] 
     ,so.xtype 
     ,sc.name [Column Name] 
     ,sc.id  [Column ID] 
     ,sf.constid 
     ,sf.fkeyid [Object ID of the table with FOREIGN KEY] 
     ,sf.rkeyid [Referenced Table ID] 
     ,(select TOP 1 o.name 
      from sysforeignkeys f 
      inner join sysobjects o on o.id=f.rkeyid 
      where o.xtype='U') AS [Referenced Table] 
from sysobjects so 
inner join syscolumns sc  on so.id = sc.id 
inner join sysforeignkeys sf on so.id = sf.fkeyid 
where so.xtype='U' 
    and ( sc.name like 'SSN' --<-- Two operator together "LIKE" and "=" 
     OR sc.name LIKE 'ssn%'   
     OR sc.name LIKE 'ssn%'   
     OR sc.name LIKE '%_ssn%'   
     OR sc.name LIKE '_ocsecno'   
     OR sc.name LIKE 'Ssn%'); 

重要提示

在选择子查询必须返回一个标值,添加TOP 1到您的子查询,它应该修复错误。

同样使用方括号[]作为列名而不是文字字符串。

+0

良好的通话。更正,但我仍然收到错误消息512,级别16,状态1,行3 子查询返回多个值。当子查询遵循=,!=,<, <= , >,> =或当子查询用作表达式时,这是不允许的。 – user3561219 2015-03-31 18:13:26

+1

这是一个很好的赶上阿里+1。 – Rahul 2015-03-31 18:16:37

+0

@ user3561219看看我编辑了我的答案,并解决了您现在正在收到的问题。 – 2015-03-31 18:19:04

1

的问题是在你的where条款

..... 
where so.xtype='U' 
    and (sc.name like 'SSN' -- Here you have a unwanted = or sc.name = 'SSN' 
    OR sc.name LIKE 'ssn%'   
    OR sc.name LIKE 'ssn%'   
     OR sc.name LIKE '%_ssn%'   
    OR sc.name LIKE '_ocsecno'   
    OR sc.name LIKE 'Ssn%'); 
相关问题