Sql server存储过程通过两个表之间的比较来切割记录

时间:2014-05-26 09:59:51

标签: mysql sql sql-server stored-procedures

我有两个表我应该在这两个表之间进行比较,第一个表有一列,这个列是完整的URL而另一个表有两列,第一列是URLCategory和另一个是the number我应该在其他表格列/

之前删除的URL

第一个表是

                          URL
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://217.146.8.6/din.aspx?s=11575802&client=DynGate&p=10002926
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://10.6.2.26/ERP/HRServices/WorkflowService.asmx
http://195.170.180.170/SADAD/PaymentNotificationService.asmx
http://www.google.com/

第二个表格可与

比较
  URL                              CUT_BEFORE
http://10.6.2.26                  3
http://217.146.8.6                1
http://195.170.180.170            2

我应该将第二列与第一列之间进行比较

  URL                        
http://10.6.2.26/ERP/HRServices
http://195.170.180.170/SADAD
http://10.6.2.26/ERP/HRServices
http://10.6.2.26/ERP/HRServices
http://10.6.2.26/ERP/HRServices
http://217.146.8.6
http://195.170.180.170/SADAD
http://10.6.2.26/ERP/HRServices
http://195.170.180.170/SADAD
http://www.google.com/

在while循环中使用SQLServer

执行类似操作的过程脚本是什么

等待你的回答

由于

1 个答案:

答案 0 :(得分:0)

请参阅以下代码。

--------------------------------------------------------------------------------
DECLARE @URL TABLE (ID INT IDENTITY(1,1), URL VARCHAR(200))

INSERT INTO @URL VALUES
('http://10.6.2.26/ERP/HRServices/WorkflowService.asmx'),
('http://195.170.180.170/SADAD/PaymentNotificationService.asmx'),
('http://10.6.2.26/ERP/HRServices/WorkflowService.asmx'),
('http://10.6.2.26/ERP/HRServices/WorkflowService.asmx'),
('http://10.6.2.26/ERP/HRServices/WorkflowService.asmx'),
('http://217.146.8.6/din.aspx?s=11575802&client=DynGate&p=10002926'),
('http://195.170.180.170/SADAD/PaymentNotificationService.asmx'),
('http://10.6.2.26/ERP/HRServices/WorkflowService.asmx'),
('http://195.170.180.170/SADAD/PaymentNotificationService.asmx'),
('http://www.google.com/')

DECLARE @MAP TABLE (ID INT IDENTITY(1,1), URL VARCHAR(200),CUT INT)
INSERT INTO @MAP VALUES
('http://10.6.2.26',3),
('http://217.146.8.6',1),  
('http://195.170.180.170',2)
--------------------------------------------------------------------------------

请参阅此处的代码

DECLARE @DEST TABLE 
(ID INT, URL VARCHAR(200),URLREF VARCHAR(200),CUT INT,RESULT VARCHAR(200))
INSERT  INTO @DEST (ID,URL,URLREF,CUT)
SELECT  A.ID,A.URL,B.URL URLREF,B.CUT
FROM    @URL A,@MAP B   
WHERE   LEFT(A.URL,LEN(B.URL)) = B.URL

DECLARE     @A INT = 1,
            @B INT,
            @C VARCHAR(200),
            @D VARCHAR(200),
            @E INT,
            @F INT,
            @G INT,
            @H VARCHAR(200)

SET     @B = (SELECT COUNT(*) FROM @DEST)

WHILE @A<= @B
BEGIN
    SET @C = (  SELECT SUBSTRING(URL,LEN(URLREF)+2,LEN(URL)) 
                FROM @DEST WHERE ID = @A)

    SET @H = ''
    SET @G = 0
    SET @F = 1
    SET @E = LEN(@C)
    WHILE @F <= @E
        BEGIN
            SET @D = SUBSTRING(@C,@F,1)
            IF @D = '/'
                BEGIN
                SET @G = @G + 1
                IF @G = (SELECT CUT-1 FROM @DEST WHERE ID = @A)
                    BEGIN
                    SET @F = @E
                    END
                END
        SET @H = @H + @D
        SET @F = @F + 1
        END

        UPDATE @DEST 
        SET RESULT = URLREF +'/'+ LEFT(@H,LEN(@H)-1) 
        WHERE ID = @A
SET @A = @A + 1
END

UPDATE  @DEST
SET     RESULT = URLREF
WHERE   CUT = 1

SELECT RESULT FROM @DEST

<强>结果:

enter image description here

添加评论
如果我们在SSMS窗口中使用上面两组代码,我们就不会收到错误。 我在@URL和@MAP中尝试了空值。我仍然没有得到错误。 如果要将其设置为varchar(max),请确保在任何地方进行更改。