复杂连接和不同操作不会输出正确的信息

时间:2015-01-14 14:38:07

标签: sql sql-server sql-server-2008

我已将存储过程创建为

ALTER PROCEDURE [dbo].[prLineOfAuthorityFetchForAgentLicence]
    @AgentId int,
    @Jurisdiction nvarchar(max)
As
    Declare @individual varchar(20) = null;

    Create table #Split (LineOfAuthority nvarchar(max))

    Insert into #Split 
        select LineOfAuthority 
        from tbLicence 
        where AgentId = @AgentId

    Select * from #Split;

    DECLARE @products VARCHAR(MAX)='';

    SELECT @products += Isnull(LineOfAuthority, '') + ',' FROM   #Split;

    SELECT right(@products,len(@products)-1); 

    Create table #TempLineOfAuthority
    (
    LineOfAuthorityX nvarchar(max)
    );

   Insert into #TempLineOfAuthority
   SELECT 
   Split.a.value('.', 'VARCHAR(MAX)') AS TempLineOfAuthority
   FROM  
   (
    SELECT CAST ('<M>' + REPLACE(@products, ',', '</M><M>') + '</M>' AS XML) AS CVS 
    ) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a);

   Select * from #TempLineOfAuthority;

   Select distinct * from tbLineOfAuthority join #TempLineOfAuthority on (tbLineOfAuthority.LineOfAuthorityX collate SQL_Latin1_General_CP1_CI_AS = #TempLineOfAuthority.LineOfAuthorityX);

我在数据库中有两个表,如屏幕I&amp; II

enter image description here

enter image description here

执行查询后,输出屏幕就像

enter image description here

请注意&#34;非居民互惠&#34;在屏幕III的最后一个表中缺少。

我也在尝试使用

Where tbLicence.Jurisdiction Like @Jurisdiction 

实际上我正试图根据像#A; Alabama&#34;这样的管辖权取得权限。在@Jurisdiction参数...我无法写这样的连接查询...请帮助我!!!

0 个答案:

没有答案