Microsoft SQL Server 2008游标

时间:2011-09-06 13:53:03

标签: sql-server sql-server-2008 variables if-statement database-cursor

我有一个#temptable,其中包含使用游标读取的地址信息。光标效果很好。游标中的if语句对#table中的第一条记录起作用100%,但对于第二条记录,它似乎总是返回false。

if语句的想法是检查db中的地址表中是否存在该值,然后是否只获取id。如果没有,则插入行并获取id。单独使用if语句100%工作。我将它添加到光标中的那一刻就会中断。它几乎就像变量以某种方式被覆盖了一样。

因此,如果我的#table中包含相同的行,则该行仍会在address_table中插入两次,而不是仅插入一次。第一次插入地址(如果不存在),第二次应该恢复表中地址的id

我已经取出了我执行 convert()的打印语句,但它似乎没有什么区别。

这是单独的声明。

DECLARE @COMP_NUM AS VARCHAR(100) = '4'
DECLARE @COMP_NAME AS VARCHAR(100) = 'TAGASTE'
DECLARE @STREET_NUM AS VARCHAR(100) = '150'
DECLARE @STREET_NAME AS VARCHAR(100) = 'WILLSON'
DECLARE @STREET_TYPE AS INT = 1
DECLARE @SUB AS VARCHAR(100) = 'FAIRLANDS'
DECLARE @CITY AS VARCHAR(100) = 'JOHANNESBURG'
DECLARE @HOMEPHONE AS VARCHAR(100) = '0112355566'
DECLARE @EXISTS AS INT
DECLARE @ADD_ID AS INT

SET @EXISTS = (SELECT COUNT(ID) FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE=@HOMEPHONE 
AND COMPLEX_NUMBER = @COMP_NUM
AND COMPLEX_NAME = @COMP_NAME
AND STREET_NUMBER = @STREET_NUM
AND STREET_NAME = @STREET_NAME
AND STREET_TYPE = @STREET_TYPE
AND SUBURB = @SUB
AND CITY = @CITY)
PRINT ('COUNT ROWS: ' + CONVERT(CHAR(6),@EXISTS))

IF (CONVERT(INT,@EXISTS) > 0)               
BEGIN
    PRINT ('RECORD EXISTS')
    SET @ADD_ID = (SELECT ID FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE 
                    AND COMPLEX_NUMBER = @COMP_NUM
                    AND COMPLEX_NAME = @COMP_NAME
                    AND STREET_NUMBER = @STREET_NUM
                    AND STREET_NAME = @STREET_NAME
                    AND STREET_TYPE = @STREET_TYPE
                    AND SUBURB = @SUB
                    AND CITY = @CITY) 
    PRINT ('ADDRESS ID: ' + CONVERT(CHAR(6),@ADD_ID))
END
ELSE
BEGIN
    PRINT ('RECORD DOES NOT EXIST')
    INSERT INTO CARETEAMZ..ADDRESS_BOOK (HOME_PHONE, COMPLEX_NUMBER, COMPLEX_NAME, STREET_NUMBER, STREET_NAME,STREET_TYPE, SUBURB, CITY)
    VALUES (@HOMEPHONE, @COMP_NUM,@COMP_NAME, @STREET_NUM,@STREET_NAME,@STREET_TYPE,@SUB,@CITY)

    SET @ADD_ID = (SELECT ID FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE 
                    AND COMPLEX_NUMBER = @COMP_NUM
                    AND COMPLEX_NAME = @COMP_NAME
                    AND STREET_NUMBER = @STREET_NUM
                    AND STREET_NAME = @STREET_NAME
                    AND STREET_TYPE = @STREET_TYPE
                    AND SUBURB = @SUB
                    AND CITY = @CITY) 
    PRINT ('ADDRESS ID: ' + CONVERT(char(6),@ADD_ID))
END

这是我的完整光标

DECLARE @COMP AS VARCHAR(500)
DECLARE @COMP_LENGTH AS INT
DECLARE @COMP_INDEX AS INT
DECLARE @COMP_NUM AS VARCHAR(50)
DECLARE @COMP_NAME AS VARCHAR(100)
DECLARE @STREET AS VARCHAR(500)
DECLARE @STREET_LENGTH AS INT
DECLARE @STREET_INDEX AS INT
DECLARE @STREET_NUM AS VARCHAR(50)
DECLARE @STREET_NAME AS VARCHAR(100)
DECLARE @STREET_VALUE AS VARCHAR(200)
DECLARE @STREET_TYPE AS INT
DECLARE @SUB AS VARCHAR(100)
DECLARE @CITY AS VARCHAR(100)
DECLARE @HOMEPHONE AS VARCHAR(100)
SET @CITY = 'JOHANNESBURG'

DECLARE ADD_CURSOR CURSOR FOR SELECT [HOME],[COMPLEX], [STREET] FROM #TEMPADD
OPEN ADD_CURSOR
FETCH NEXT FROM ADD_CURSOR INTO @HOMEPHONE,@COMP,@STREET;
    WHILE @@FETCH_STATUS = 0   
        BEGIN
            --COMPLEX DETAILS
            PRINT('-- COMPLEX DETAILS --')
            PRINT(@COMP) 
            SET @COMP_INDEX = CHARINDEX(',',@COMP)
            PRINT(@COMP_INDEX)
            IF (@COMP_INDEX > 0)
            BEGIN
                    PRINT('COMPLEX TRUE')
                    SET @COMP_NUM = LTRIM(RTRIM((SUBSTRING(@COMP,0,@COMP_INDEX))))
                    SET @COMP_NAME = LTRIM(RTRIM((SUBSTRING(@COMP,@COMP_INDEX+1,LEN(@COMP)))))                  
            END
            ELSE 
            BEGIN 
                    PRINT('COMPLEX FALSE')
                    SET @COMP_NUM = NULL
                    SET @COMP_NAME = NULL
            END
            PRINT('COMPLEX NUMBER: ' + CONVERT(VARCHAR(50),@COMP_NUM))
            PRINT('COMPLEX NAME: ' + @COMP_NAME)

            -- STREET ADDRESS
            PRINT('-- STREET NAME --')
            PRINT(@STREET) 
            SET @STREET_INDEX = CHARINDEX(',',@STREET)
            PRINT(@STREET_INDEX)
            IF (@STREET_INDEX > 0)
            BEGIN
                    PRINT('STREET TRUE')
                    SET @STREET_NUM = LTRIM(RTRIM((SUBSTRING(@STREET,0,@STREET_INDEX))))
                    SET @STREET = (SUBSTRING(@STREET,@STREET_INDEX+1,LEN(@STREET)))
                    PRINT(@STREET)
                    SET @STREET_INDEX = CHARINDEX(',',@STREET)
                    SET @STREET_NAME = LTRIM(RTRIM((SUBSTRING(@STREET,0,CHARINDEX(' ',@STREET)))))
                    SET @SUB = LTRIM(RTRIM((SUBSTRING(@STREET,@STREET_INDEX+1,LEN(@STREET)))))
                    SET @STREET_VALUE = SUBSTRING(@STREET,0,CHARINDEX(',',@STREET))
                    SET @STREET_TYPE = (SELECT ID FROM CARETEAMZ..STREET_TYPE WHERE STREET_TYPE = RTRIM(LTRIM((SUBSTRING(@STREET_VALUE,LEN(@STREET_VALUE)-CHARINDEX(' ',REVERSE(@STREET_VALUE))+2,LEN(@STREET_VALUE)-CHARINDEX(',',@STREET_VALUE))))))                  
            END
            ELSE 
            BEGIN 
                    PRINT('STREET FALSE')
                    SET @STREET_NUM = NULL
                    SET @STREET_NAME = NULL
                    SET @STREET_TYPE = NULL
                    SET @SUB = NULL
            END
            PRINT('STREET NUMBER: ' + @STREET_NUM)
            PRINT('STREET NAME: ' + @STREET_NAME)
            PRINT('STREET TYPE: ' + CONVERT(VARCHAR(10),@STREET_TYPE))
            PRINT('SUBURB: ' + @SUB)

            --CHECK IF THE ADDRESS / PHONE NUMBER ALREADY EXISTS IN THE DB
            DECLARE @ADD_ID AS INT
            DECLARE @EXISTS AS INT

            SET @EXISTS = (SELECT COUNT(ID) FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE 
            AND COMPLEX_NUMBER = @COMP_NUM
            AND COMPLEX_NAME = @COMP_NAME
            AND STREET_NUMBER = @STREET_NUM
            AND STREET_NAME = @STREET_NAME
            AND STREET_TYPE = @STREET_TYPE
            AND SUBURB = @SUB
            AND CITY = @CITY)
            PRINT ('COUNT ROWS: ' + CONVERT(CHAR(6),@EXISTS))
            IF (@EXISTS > 0)                
            BEGIN
                PRINT ('RECORD EXISTS')
                SET @ADD_ID = (SELECT ID FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE 
                                AND COMPLEX_NUMBER = @COMP_NUM
                                AND COMPLEX_NAME = @COMP_NAME
                                AND STREET_NUMBER = @STREET_NUM
                                AND STREET_NAME = @STREET_NAME
                                AND STREET_TYPE = @STREET_TYPE
                                AND SUBURB = @SUB
                                AND CITY = @CITY)

                PRINT ('ADDRESS ID: ' + CONVERT(CHAR(6),@ADD_ID))
            END
            ELSE
            BEGIN
                PRINT ('RECORD DOES NOT EXIST')
                INSERT INTO CARETEAMZ..ADDRESS_BOOK (HOME_PHONE, COMPLEX_NUMBER, COMPLEX_NAME, STREET_NUMBER, STREET_NAME,STREET_TYPE, SUBURB, CITY)
                VALUES (@HOMEPHONE, @COMP_NUM,@COMP_NAME, @STREET_NUM,@STREET_NAME,@STREET_TYPE,@SUB,@CITY)

                SET @ADD_ID = (SELECT ID FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE 
                                AND COMPLEX_NUMBER = @COMP_NUM
                                AND COMPLEX_NAME = @COMP_NAME
                                AND STREET_NUMBER = @STREET_NUM
                                AND STREET_NAME = @STREET_NAME
                                AND STREET_TYPE = @STREET_TYPE
                                AND SUBURB = @SUB
                                AND CITY = @CITY) 

                PRINT ('ADDRESS ID: ' + CONVERT(char(6),@ADD_ID))

            END

            --FETCH NEXT
            FETCH NEXT FROM ADD_CURSOR INTO @HOMEPHONE,@COMP, @STREET;
        END
CLOSE ADD_CURSOR;
DEALLOCATE ADD_CURSOR;

好的,所以我看看@HLGEM& @kuru kuru pa的答案。 @HLGEM这只是更大光标的一小部分,遗憾的是,MERGER SQL无法满足我当前的需求。我不担心性能,因为这只是将数据导入我的数据库。

@kuru kuru pa的答案很好但不幸的是我似乎仍然会再次遇到同样的问题。我第一次运行光标它运行100%。插入所有值,第二次它应该只返回ID但id不返回任何内容并插入重复项。

下面是带有一些虚拟数据的2个表创建语句。

TEMP表

CREATE TABLE #TEMPADD
(
    HOME VARCHAR(100) NULL,
    COMPLEX VARCHAR(100) NULL,
    STREET VARCHAR(100) NULL,
)

INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 6787',  '32,Tagaste',   '150,Willson Street, Land')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 1909',  NULL,   '29,Bunkara Street, Rio')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 689 2630',  NULL,   '275,Kings Lynne Road, Glen')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    NULL,   '275,Kings Lynne Road, Glen')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 799 5917',  '5,The Vineyard',   '45,Hilary Road, Ridge')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '5,The Vineyard',   '45,Hilary Road, Ridge')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 5857',  NULL,   '11A,Alexandra Street, Florida')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 5857',  NULL,   '11A,Alexandra Street, Florida')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 3225',  NULL,   '752, Without Avenue, Weltevreden')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 8909',  NULL,   '18,Smit Street,Land')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES ('011 679 8909',  '512,Athenian View',    '158,Smit Street, Fairland')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    '741,Eagle Trace Landing',  'Eagle Canyon Golf Estate, Honey')
INSERT INTO #TEMPADD (HOME,COMPLEX,STREET) VALUES (NULL,    NULL,   '106A,3rd Avenue, Land')

ADDRESS TABLE

CREATE TABLE [dbo].[ADDRESS_BOOK](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [PoBox] [varchar](50) NULL,
    [Complex_Number] [varchar](50) NULL,
    [Complex_Name] [varchar](100) NULL,
    [Street_Number] [varchar](50) NULL,
    [Street_Name] [varchar](100) NULL,
    [Street_Type] [int] NULL,
    [Suburb] [varchar](100) NULL,
    [City] [varchar](50) NULL,
    [Code] [varchar](50) NULL,
    [Home_Phone] [varchar](50) NULL
) ON [PRIMARY]

2 个答案:

答案 0 :(得分:1)

为什么你会用光标做这个?建议你阅读这篇文章,看看使用基于集合的技术做起来有多容易(以及更快):

http://wiki.lessthandot.com/index.php/Cursors_and_How_to_Avoid_Them

答案 1 :(得分:0)

这是需要阅读的大量代码...

首先观察(不是您的问题的答案,但FWIW)是您可能正在使用错误的工具来完成工作。这里不确定是否需要光标。总之...

现在只需进行现场检查......(除非您想添加创建表脚本@martin建议...(如果您这样做,我会给您一个更全面的答案)

您对@Exists的使用是多余的。基本上,您正在使用@Exists告诉您@Add_ID是否存在。这是间接/冗余的方法。

If @Exists exists, then @Add_ID exists.

你可以使用更无聊的重言式(直接方法):

If @Add_ID exists, then @Add_ID exists.

所以你的代码可能就是这样:

SET @Exists = ...
If @Exits > 0...

SET @ADD_ID = (SELECT ID FROM CARETEAMZ..ADDRESS_BOOK WHERE HOME_PHONE = @HOMEPHONE AND ...)
IF @ADD_ID IS NOT NULL
BEGIN
   PRINT(...)
END
ELSE...