0

その表情をしています

  SELECT sysobjects.name AS TableName,  
  c.name AS ColumnName,  
  st.name AS TypeName,  
  "Length"  
  = case st.name  
  when 'uniqueidentifier'  then 'nvm'  
  when 'bit'  then 'nvm'  
  when 'int'  then 'nvm'
  when 'image'  then 'nvm'  
  when 'datetime'  then 'nvm'  
  when 'xml'  then 'nvm'  
  else (cast(c.length as varchar))  
  end,  
  case c.isnullable     
  when '0' then 'No'  
  when '1' then 'Yes' end AS 'Nullable'  
  FROM   
  dbo.syscolumns c  WITH (NOLOCK)   
  INNER JOIN dbo.systypes st ON st.xusertype = c.xusertype   
  INNER JOIN dbo.sysobjects  WITH (NOLOCK) ON sysobjects.id=c.id   
  WHERE sysobjects.type in ('U')  
  ORDER BY TableName    

だから私はこの出力を取得します:

  ACCREDITED_PROGRAMS   ID                  uniqueidentifier    nvm No  
  ACCREDITED_PROGRAMS   APPLICATION_FK          uniqueidentifier    nvm Yes  
  ACCREDITED_PROGRAMS   LICENCED_PROGRAM_FK uniqueidentifier    nvm Yes  

問題は、隣接する 2 つの列に、APPLICATION_FK、LICENCED_PROGRAM_FK、およびその他の FK を参照するテーブルとフィールドを示すことです。

4

1 に答える 1

1

使用できる例を次に示します。

SELECT o1.Name, c.Name, p.Name, rc.Name
FROM syscolumns c
INNER JOIN sysobjects o1 on o1.id = c.id --table
INNER JOIN sysobjects o2 on o2.parent_obj = o1.id and o2.type = 'F' --foreign key
LEFT JOIN sysreferences r on o2.id =  r.constid and c.colid = r.fkey1
LEFT JOIN sysobjects p on r.rkeyid = p.id
LEFT JOIN syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid
WHERE o1.Name = 'TableName'

結果:

Address AddressID           NULL            NULL
Address AddressLine1        NULL            NULL
Address AddressLine2        NULL            NULL
Address City                NULL            NULL
Address StateProvinceID     StateProvince   StateProvinceID
Address PostalCode          NULL            NULL
Address SpatialLocation     NULL            NULL
Address rowguid             NULL            NULL
Address ModifiedDate        NULL            NULL
于 2012-11-22T12:50:49.173 に答える