2015-03-31 43 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%'); 
相關問題