Results 1 to 14 of 14

Thread: SELECT...? SubQuery..? JOIN..? EXEC..?

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    SELECT...? SubQuery..? JOIN..? EXEC..?

    Code:
    	SELECT 
    		Guid
    	FROM
    		ContactsPermissions
    	WHERE ContactsPermissions.ContactGuid = @ContactGuid
    	AND @ZoneName = (SELECT 
    						SecurityZones.Name 
    					FROM 
    						SecurityZones 
    					WHERE 
    						SecurityZones.Guid = ContactsPermissions.SecurityZoneGuid)
    ^ Does not work.

    Code:
    SET @ZoneGuid = (SELECT Guid FROM SecurityZones WHERE SecurityZones.Name = @ZoneName)
    
    SELECT Guid FROM ContactsPermissions 
    WHERE ContactsPermission.ContactGuid = @ContactGuid
    AND @ZoneGuid = ContactsPermissions.SecurtyZoneGuid
    ^ Will not save.

    I'm trying to load a contact permission based on the contact guid and the name of the security zone. The name is unique and returns one result when you pull it by name. The Permissins pulls all permissions fine by ContactGuid.

    ContactPermissions
    ---------------------------------------------------------------------------------
    3 Guid uniqueidentifier 16 0
    0 ContactGuid uniqueidentifier 16 0
    0 SecurityZoneGuid uniqueidentifier 16 0

    SecurityZones
    ---------------------------------------------------------------------------------
    3 Guid uniqueidentifier 16 0
    0 Name varchar 300 0
    0 Description text 16 1

    Code:
    	SELECT     ContactsPermissions.Guid
    	FROM         ContactsPermissions INNER JOIN
    	                      SecurityZones ON ContactsPermissions.SecurityZoneGuid = SecurityZones.Guid
    	WHERE  (ContactsPermissions.ContactGuid = @ContactGuid)
    	AND    (SecurityZones.Name = @ZoneName)
    ^ Returns nothing even when passed valid param.

    So basicly I know the contact, I know the Zone name and I need to get the Permission guid.

    Thanks to anyone who can help.
    Magiaus

    If I helped give me some points.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The last query should work, providing all your parameters values are correct. Is your SQL Server Case Sensitive?

    The only thing I can suggest is to place square brackets around the Name field. Name is a reserved/keyword and may be causing problems.

    AND (SecurityZones.[Name] = @ZoneName)

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Code:
    SELECT 
    		Guid
    	FROM
    		ContactsPermissions
    	WHERE ContactsPermissions.ContactGuid = @ContactGuid
    	AND @ZoneName = (SELECT 
    						SecurityZones.Name 
    					FROM 
    						SecurityZones 
    					WHERE 
    						SecurityZones.Guid = ContactsPermissions.SecurityZoneGuid)
    To my untrained Sql Server eye this won't work because the second select might bring back more than one result. So I'd re-think your joins. Poss:
    Code:
    SELECT [Guid]
    FROM [ContactsPermissions] Left join [SecurityZones] on
    [SecurityZones].[Guid] = [ContactsPermissions].[SecurityZoneGuid]
    WHERE [ContactsPermissions].[ContactGuid] = @ContactGuid
    AND [SecurityZones].[name] = @ZoneName
    I've gone mental with the square brackets, but it should work fine. As Bruce mentioned, its best to put field names in square brackets at least, especially if they happen to be functions.

    The other sql statements I dunno. I don't even know if the one above will work right (the way you want) as people used where clauses to make joins... Confusing

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    AHHHHHHHHHHH

    Code:
    	(
    		@ContactGuid uniqueidentifier,
    		@ZoneName varchar(300)
    	)
    AS	
    	SELECT     [ContactsPermissions].[Guid]
    	FROM         ContactsPermissions INNER JOIN
    	                      SecurityZones ON [ContactsPermissions].[SecurityZoneGuid] = [SecurityZones].[Guid]
    	WHERE  ([ContactsPermissions].[ContactGuid] = @ContactGuid)
    	AND    ([SecurityZones].[Name] = @ZoneName)	RETURN
    I tried that. No records. The Zones table is constrained to have a unique name. I also tried a distinct with the subquery variation it doesn't work either. This is really messing with me. Any ideas?
    Magiaus

    If I helped give me some points.

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    ok... lets take it back a step.

    Leave the join in but take out hte zones part of the where clause and see if you get any records back.

    If you do, then you need to check that the value put into @Zones is the same as the one you are filtering on. Maybe its filtering out the records (case sensitive?) even though you know its right.

    If however you are not getting any records at all, I'd change inner join to left join and see whether that helps.

    If this fails... then you/I/we'd better think up a new approach


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Orginal query:
    Code:
    	SELECT 
    		Guid
    	FROM
    		ContactsPermissions
    	WHERE ContactsPermissions.ContactGuid = @ContactGuid
    	AND @ZoneName = (SELECT 
    						SecurityZones.Name 
    					FROM 
    						SecurityZones 
    					WHERE 
    						SecurityZones.Guid = ContactsPermissions.SecurityZoneGuid)
    "Updated" Query:
    Code:
    	SELECT 
    		Guid
    	FROM
    		ContactsPermissions
    	WHERE ContactsPermissions.ContactGuid = @ContactGuid
    	AND @ZoneName IN (SELECT 
    						SecurityZones.Name 
    					FROM 
    						SecurityZones 
    					WHERE 
    						SecurityZones.Guid = ContactsPermissions.SecurityZoneGuid)
    Don't blink.... might miss it....

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I tried th IN version as well. I am getting recodrs if I remove the @ZoneName I also get records if I select using just the @ZoneName.

    I am not sure what exactly what could be the problem.

    This is my current attempt

    Code:
    	(
    		@ContactGuid uniqueidentifier,
    		@ZoneName varchar(300)
    	)
    AS	
    	SELECT     [ContactsPermissions].[Guid]
    	FROM         [ContactsPermissions] INNER JOIN
    	                      [SecurityZones] ON [ContactsPermissions].[SecurityZoneGuid] = [SecurityZones].[Guid]
    	WHERE  ([ContactsPermissions].[ContactGuid] = @ContactGuid)
    	AND    ([SecurityZones].[Name] = @ZoneName)
    	RETURN
    I going to try using the left join in a bit as soon as I wrap up some other things I'm working on.
    Magiaus

    If I helped give me some points.

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    This query should be simple.

    As a test include the zonename in the selection list.

    SELECT [ContactsPermissions].[Guid], ([SecurityZones].[Name]
    FROM [ContactsPermissions]
    INNER JOIN [SecurityZones] ON [ContactsPermissions].[SecurityZoneGuid] = [SecurityZones].[Guid]
    WHERE ([ContactsPermissions].[ContactGuid] = @ContactGuid)

    Run it and manually check if there is a match with your parameter.

    Alternatively, if you have Enterprise Manager, create the query using the GUI and use the SQL statement it generates.

  9. #9

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    These are the results

    Code:
    {D241529A-ECE9-42A7-99CB-66699AE07DC Employee Production Agent                                                                                                                                                                                                                                        
    {82EAAF2D-03DC-4867-90F6-2B7453C6B9A Employee Products Agent                                                                                                                                                                                                                                          
    {F5C2C45A-392A-409A-90AF-645A7A573DB Employee Production Reviewer                                                                                                                                                                                                                                     
    {2550287D-E025-4ECA-A5ED-681309AC26F Employee Administrator                                                                                                                                                                                                                                           
    {DFF1AE94-4E2D-43F7-B340-8880DFAA226 Employee                                                                                                                                                                                                                                                         
    {297F6446-E99C-49C5-9D20-EB57E1AA776 Employee Order Agent                                                                                                                                                                                                                                             
    {B8FA1A0D-829D-4105-BC03-EC4935C8F80 Contact                                                                                                                                                                                                                                                          
    {E967E1E1-8833-407F-B06D-FF3978F3988 Employee Manufacturer Admin                                                                                                                                                                                                                                      
    {25745C59-CD37-4963-9271-C78F1D77DBE Employee Distributor Admin                                                                                                                                                                                                                                       
    {458B472C-5181-423B-B808-62DCE27AA98 Employee Source Agent                                                                                                                                                                                                                                            
    {E73EA957-5838-487B-A001-648CBDB4A0F Administrator                                                                                                                                                                                                                                                    
    {DE027E1B-804C-4BFD-8B4E-21F39A8BBB7 Employee Source Reviewer
    I ran this query with a value copied directly from those results.

    Code:
    (
    		@ContactGuid uniqueidentifier,
    		@ZoneName varchar(300)
    	)
    AS	
    	SELECT     [ContactsPermissions].[Guid], [SecurityZones].[Name]
    	FROM         [ContactsPermissions] INNER JOIN
    	                      [SecurityZones] ON [ContactsPermissions].[SecurityZoneGuid] = [SecurityZones].[Guid]
    	WHERE  ([ContactsPermissions].[ContactGuid] = @ContactGuid)
    	AND ([SecurityZones].[Name] = @ZoneName)
    	RETURN

    These are results from a query on SecurityZones using only the Name

    66042BA8-5957-4E90-900E-E68FF47ED10

    so as you see it pulls a distinct record

    using

    Code:
    	(
    		@ZoneName varchar(300)
    	)
    AS
    	SELECT Guid FROM SecurityZones 
    	WHERE SecurityZones.Name = @ZoneName
    	RETURN
    I have tried to execute that procedure inside the one that isn't working but I have never used execute before.....

    I really appreciate you guys helping me out. If you have any ideas please post them, any ideas. I'm about to go to pulling everything and just doing a loop or using a cursor and doing the loop in SQL Server.
    Magiaus

    If I helped give me some points.

  10. #10

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I just tried using the IN with the sub query again and it returns 0 records
    Magiaus

    If I helped give me some points.

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    It has to be a problem with the parameter. The query is correct.

    What is the value of @ZoneName that you are using?
    Is your database Case Sensitive?
    Is the datatype of SecurityZones.Name a varchar like the parameter declaration?

    Again, create the query using the GUI interface in EM? What is the SQL statement that it generates?

  12. #12

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Running dbo."CP_SZ_Test" ( @ContactGuid = {6591B544-6A37-41EB-9D37-8CCFE7D05DC4}, @ZoneName = Administrator ).

    Guid
    ------------------------------------
    {E73EA957-5838-487B-A001-648CBDB4A0F
    No more results.
    (1 row(s) returned)
    @RETURN_VALUE = 0
    Finished running dbo."CP_SZ_Test".

    Code:
    ALTER PROCEDURE dbo.CP_SZ_Test
    	(
    		@ContactGuid uniqueidentifier,
    		@ZoneName varchar(300)
    	)
    AS
    	SELECT     ContactsPermissions.Guid
    	FROM         ContactsPermissions INNER JOIN
    	                      SecurityZones ON ContactsPermissions.SecurityZoneGuid = SecurityZones.Guid
    	WHERE     (ContactsPermissions.ContactGuid = @ContactGuid) AND (SecurityZones.Name = @ZoneName)
    	RETURN
    Magiaus

    If I helped give me some points.

  13. #13

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    That works. Can some one tell me why? It is identical and I didn't do anything except let the UI generate it.
    Magiaus

    If I helped give me some points.

  14. #14

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Thumbs up Thanks

    Thanks for the help you guys.
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width