Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Fetching data from two tables Expand / Collapse
Author
Message
Posted Tuesday, October 23, 2012 12:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7, Visits: 13
Hi All,

I'm having 2 tables named "Folders" and "Permissions"

I want my result to be all the folders from "Folders" table and permissions from "Permissions" table. if the folder does not exists in "Permissions" table then also folder should be appear in my result table with none permission

Please help me writing query for this.

Folder (table structure with data)
Folder ID Folder Name
1 Folder1
2 Folder2
3 Folder3
4 Folder4
5 Folder5
6 Folder6
7 Folder7

Permissions (table structure and data)
Folder id Folder name permission
1 Folder1 Read
2 Folder2 Admin
3 Folder3 None
4 Folder4 admin

my result should be as below table:
Folder name permission
Folder1 Read
Folder2 Admin
Folder3 None
Folder4 admin
Folder5 None
Folder6 None
Folder7 none

Thanks in Advance
Post #1375855
Posted Tuesday, October 23, 2012 1:03 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 955, Visits: 2,634
You need to do an Outer Join, something like

Select FolderName
,COALESCE(Permission,'None') Permission
--,IsNull(Permission,'None') Permission --Alternate to COALESCE
From Folder f
LEFT OUTER JOIN [Permissions] p
on f.FolderId=p.FolderId





_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1375862
Posted Tuesday, October 23, 2012 1:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7, Visits: 13
Hi,

Thanks for your reply I tried your query but i'm not getting the result as expected.

I have 20 folder names in folders table and 15 folder names in permissions table.
I'm still getting only 15 folder name rows which are equal to permissions table rows.
Post #1375879
Posted Tuesday, October 23, 2012 1:52 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 955, Visits: 2,634
Curious,

Create Table #folder (FolderId int, FolderName varchar(100))
Create Table #Permissions (FolderId int, FolderName varchar(100),Permission varchar(100))


Insert into #Folder
Select 1 ,'Folder1'
Union Select 2 ,'Folder2'
Union Select 3 ,'Folder3'
Union Select 4 ,'Folder4'
Union Select 5 ,'Folder5'
Union Select 6 ,'Folder6'
Union Select 7 ,'Folder7'

insert into #Permissions
Select 1,'Folder1' ,'Read'
Union Select 2 ,'Folder2' ,'Admin'
Union Select 3 ,'Folder3' ,'None'
Union Select 4 ,'Folder4' ,'admin'

Select f.FolderName
,COALESCE(Permission,'None') Permission
--,IsNull(Permission,'None') Permission --Alternate to COALESCE
From #Folder f
LEFT OUTER JOIN #Permissions p
on f.FolderId=p.FolderId

Heres my test rig with data you provided, and i get 7 rows returned.

Try doing a Full outer Join, instead of the LEFT OUTER, that should pring back the missing on both sides.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1375883
Posted Tuesday, October 23, 2012 7:28 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, April 24, 2014 9:12 AM
Points: 285, Visits: 504
Jason's script should work. Did you run it as is or add to it? Post the actual script that you ran.

---------------------------------------------------------------
Mike Hahn - Future MCM 2025
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar
I want a personal webpage
I want to win the lotto
I want a gf like Tiffa
Post #1376037
Posted Wednesday, October 24, 2012 11:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7, Visits: 13
Hi,
I just tried the script and it works. but I think there is some problem with my tables data so I'm not able to fetch records. let you know in short what my problem is. I tried same select query on my tables.

Thank you
Post #1376787
Posted Thursday, October 25, 2012 12:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7, Visits: 13
Hi,

Finally I got it...

Thanks everyone...
Post #1376793
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse