需要帮助从 SQL 2005 查询中删除过时的 =* 连接运算符
Need assistance to remove outdated =* join operator from SQL 2005 query
以下查询不再兼容 SQL2012 及更高版本。需要协助转换。
select distinct po.name , sc.name, sc2.name
from sysobjects fo, sysobjects po, sysforeignkeys fk, sysobjects oo
, syscolumns sc, sysreferences ref, syscolumns sc2
where fo.xtype = 'F' and oo.name = @tab_name
and po.id = fo.parent_obj and fo.id = fk.constid and oo.id = fk.rkeyid
and sc.id = po.id and ref.constid = fk.constid and sc.colid = ref.fkey1
and sc2.id =* po.id and sc2.colid =* ref.fkey2;
试试这个查询 --
SELECT DISTINCT po.NAME
,sc.NAME
,sc2.NAME
FROM sysobjects fo
,sysobjects po
,sysforeignkeys fk
,sysobjects oo
,syscolumns sc
,sysreferences ref
,syscolumns sc2
WHERE fo.xtype = 'F'
AND oo.NAME = @tab_name
AND po.id = fo.parent_obj
AND fo.id = fk.constid
AND oo.id = fk.rkeyid
AND sc.id = po.id
AND ref.constid = fk.constid
AND sc.colid = ref.fkey1
AND sc2.id = Null -- sc2.id = * po.id
AND sc2.colid = Null -- sc2.colid = * ref.fkey2
因为,
*= Left Outer Join
=* Right Outer Join
并且在上面的查询中试图通过搜索 Null 的 ID 来实现相同的目的。
希望有用。
=*
在这里的重点是即使找不到 sc2
的记录也能给出结果。因此,这绝对需要变成 left join
:
select distinct po.name , sc.name, sc2.name
from (sysobjects fo, sysobjects po, sysforeignkeys fk, sysobjects oo
, syscolumns sc, sysreferences ref)
left join syscolumns sc2
on sc2.id = po.id and sc2.colid = ref.fkey2
where fo.xtype = 'F' and oo.name = @tab_name
and po.id = fo.parent_obj and fo.id = fk.constid and oo.id = fk.rkeyid
and sc.id = po.id and ref.constid = fk.constid and sc.colid = ref.fkey1;
为了可读性,更喜欢将其余的旧式交叉连接重写为内部连接。您已经看到旧式语法太难理解了。
以下查询不再兼容 SQL2012 及更高版本。需要协助转换。
select distinct po.name , sc.name, sc2.name
from sysobjects fo, sysobjects po, sysforeignkeys fk, sysobjects oo
, syscolumns sc, sysreferences ref, syscolumns sc2
where fo.xtype = 'F' and oo.name = @tab_name
and po.id = fo.parent_obj and fo.id = fk.constid and oo.id = fk.rkeyid
and sc.id = po.id and ref.constid = fk.constid and sc.colid = ref.fkey1
and sc2.id =* po.id and sc2.colid =* ref.fkey2;
试试这个查询 --
SELECT DISTINCT po.NAME
,sc.NAME
,sc2.NAME
FROM sysobjects fo
,sysobjects po
,sysforeignkeys fk
,sysobjects oo
,syscolumns sc
,sysreferences ref
,syscolumns sc2
WHERE fo.xtype = 'F'
AND oo.NAME = @tab_name
AND po.id = fo.parent_obj
AND fo.id = fk.constid
AND oo.id = fk.rkeyid
AND sc.id = po.id
AND ref.constid = fk.constid
AND sc.colid = ref.fkey1
AND sc2.id = Null -- sc2.id = * po.id
AND sc2.colid = Null -- sc2.colid = * ref.fkey2
因为,
*= Left Outer Join
=* Right Outer Join
并且在上面的查询中试图通过搜索 Null 的 ID 来实现相同的目的。
希望有用。
=*
在这里的重点是即使找不到 sc2
的记录也能给出结果。因此,这绝对需要变成 left join
:
select distinct po.name , sc.name, sc2.name
from (sysobjects fo, sysobjects po, sysforeignkeys fk, sysobjects oo
, syscolumns sc, sysreferences ref)
left join syscolumns sc2
on sc2.id = po.id and sc2.colid = ref.fkey2
where fo.xtype = 'F' and oo.name = @tab_name
and po.id = fo.parent_obj and fo.id = fk.constid and oo.id = fk.rkeyid
and sc.id = po.id and ref.constid = fk.constid and sc.colid = ref.fkey1;
为了可读性,更喜欢将其余的旧式交叉连接重写为内部连接。您已经看到旧式语法太难理解了。