SoftTree Technologies SoftTree Technologies
Technical Support Forums
RegisterSearchFAQMemberlistUsergroupsLog in
Feature: Join to self

 
Reply to topic    SoftTree Technologies Forum Index » SQL Assistant View previous topic
View next topic
Feature: Join to self
Author Message
gemisigo



Joined: 11 Mar 2010
Posts: 2109

Post Feature: Join to self Reply with quote
While the popup shows foreign key - primary key pairs for almost all tables that could be joined to the one in the query after typing JOIN it does not show the most trivial one : itself. Would it be possible to include that in the popup?
Fri Dec 10, 2010 8:51 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7849

Post Reply with quote
I think you can customize the Joins query in the Options to have SQL Assistant return the base table too. This will make the popup to show self-joins
Mon Dec 13, 2010 11:35 pm View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2109

Post Reply with quote
Though it was a bit tricky, I managed to get it. Thanks for the tip.
Tue Dec 14, 2010 8:05 am View user's profile Send private message
judahr



Joined: 09 Mar 2007
Posts: 319
Country: United States

Post Reply with quote
Would you mind posting your query so others can reuse?
Wed Dec 15, 2010 11:54 am View user's profile Send private message
gemisigo



Joined: 11 Mar 2010
Posts: 2109

Post Reply with quote
Sure, I should have done that without asking, sorry. I edited the Joins section for MSSQL under DB Queries on DB Options tab. I only made it available for MSSQL 2005/2008 because I don't want to use 2000 or MSDE anymore but I think it can be adapted for that one just as easily (if it needs modification at all).
In case you haven't modified that part yet you can just copy paste it over the old one. After creating a backup, of course :)
So far it seems to be working. If you find any errors in it, let me know, please.

Code:

if @@version like 'Microsoft SQL Server 2005%'
or @@version like 'Microsoft SQL Server 2008%'

select
   (select name from /*db.*/dbo.sysobjects where id = f1.fkeyid),
   (select name from /*db.*/dbo.syscolumns where id = f1.fkeyid and colid = f1.fkey),
   (select name from /*db.*/dbo.syscolumns where id = f1.rkeyid and colid = f1.rkey),   
   (select s.name from /*db.*/sys.all_objects o, /*db.*/sys.schemas s where s.schema_id = o.schema_id and o.object_id = f1.fkeyid),
   'P:' + CAST(constid AS VARCHAR)
from
   /*db.*/dbo.sysforeignkeys f1
where
   rkeyid = :OBJECT_ID
union all
select
   (select name from /*db.*/dbo.sysobjects where id = f2.rkeyid),   
   (select name from /*db.*/dbo.syscolumns where id = f2.rkeyid and colid = f2.rkey),
   (select name from /*db.*/dbo.syscolumns where id = f2.fkeyid and colid = f2.fkey),
   (select s.name from /*db.*/sys.all_objects o, /*db.*/sys.schemas s where s.schema_id = o.schema_id and o.object_id = f2.rkeyid),
   'F:' + CAST(constid AS VARCHAR)
from
   /*db.*/dbo.sysforeignkeys f2
where
   fkeyid = :OBJECT_ID

/*self join extension starts*/   
UNION ALL
SELECT
   so.[name]
   ,ac.[name]
   ,ac.[name]
   ,(select s.name from /*db.*/sys.all_objects o, /*db.*/sys.schemas s where s.schema_id = o.schema_id and o.object_id = so.id)
   ,'P:' + CAST(so.id AS VARCHAR)
FROM
   dbo.sysobjects AS so
   INNER JOIN
   sys.all_columns AS ac
      ON   ac.[object_id] = so.id
   INNER JOIN
   /*db.*/sys.indexes i
      ON i.[object_id] = ac.[object_id]
   AND i.is_primary_key != 0
   INNER JOIN
   /*db.*/sys.index_columns k
      ON   k.[object_id] = i.[object_id]
         AND   k.index_id = i.index_id
         AND k.[object_id] = ac.[object_id]
         AND k.column_id = ac.column_id
WHERE   so.id = :OBJECT_ID
/*self join extension ends*/
order by 1

else

select
  (select name from /*db.*/dbo.sysobjects where id = f1.fkeyid),
  (select name from /*db.*/dbo.syscolumns where id = f1.fkeyid and colid = f1.fkey),
  (select name from /*db.*/dbo.syscolumns where id = f1.rkeyid and colid = f1.rkey),
  (select u.name from /*db.*/dbo.sysusers u, /*db.*/dbo.sysobjects o where u.uid = o.uid and o.id = f1.fkeyid),
  'P:' + CAST(constid AS VARCHAR)
from
  /*db.*/dbo.sysforeignkeys f1
where
  rkeyid = :OBJECT_ID
union all
select
  (select name from /*db.*/dbo.sysobjects where id = f2.rkeyid),   
  (select name from /*db.*/dbo.syscolumns where id = f2.rkeyid and colid = f2.rkey),
  (select name from /*db.*/dbo.syscolumns where id = f2.fkeyid and colid = f2.fkey),
  (select u.name from /*db.*/dbo.sysusers u, /*db.*/dbo.sysobjects o where u.uid = o.uid and o.id = f2.rkeyid),
  'F:' + CAST(constid AS VARCHAR)
from
  /*db.*/dbo.sysforeignkeys f2
where
  fkeyid = :OBJECT_ID
order by 1



EDIT: sorry, there is no formatting applied.
Wed Dec 15, 2010 12:04 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7849

Post Reply with quote
Thank you very much for sharing this tip!
Thu Dec 16, 2010 9:43 am View user's profile Send private message
Display posts from previous:    
Reply to topic    SoftTree Technologies Forum Index » SQL Assistant All times are GMT - 4 Hours
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


 

 

Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.