不使用光标插入

时间:2013-11-26 11:28:17

标签: sql-server tsql sql-server-2012

您好我在MS SQL Server 2012中有一个程序,我正在执行一些插入和更新,但在某个程序中,我使用游标进行循环插入。

有人可以帮助我在不使用光标的情况下进行循环插入吗?

完整代码位于

之下
(
            @TerritoryName varchar(200),
            @TerritoryDescription varchar(2000),
            @TerritoryID int,
            @CountyIDs varchar(500),
            @ZipCodeIDs varchar(500)
            )
            AS

            BEGIN


            Declare @countyid int,@zipcodeid int,@Target_GeographyTypeID int;
            DECLARE @t TABLE(idx int primary key identity,id int)
            DECLARE @tzip TABLE(idx int primary key identity,id int)
            INSERT INTO @t
            SELECT item from fn_split(@CountyIDs,',')


            INSERT INTO @tzip
            SELECT item from fn_split(@ZipCodeIDs,',')

            IF(@TerritoryID < 1)
            BEGIN
            INSERT INTO Territories(TerritoryName,TerritoryDescription,Active,CreatedDate)
            VALUES(@TerritoryName,@TerritoryDescription,1,Getdate())

            SET @TerritoryID= @@identity

            END
            ELSE

            BEGIN
            UPDATE Territories
            SET TerritoryName=@TerritoryName,
            TerritoryDescription=@TerritoryDescription
            WHERE TerritoryID=@TerritoryID

            END


            SELECT @Target_GeographyTypeID=GeographyTypeId from GeoGraphyTypes
            WHERE GeographyTypeName='County'



            DELETE from Territory_mapping
            WHERE TerritoryID=@TerritoryID


            DECLARE db_cursor CURSOR FOR  

            SELECT id

            FROM @t

            OPEN db_cursor   

            FETCH NEXT FROM db_cursor INTO @countyid
            WHILE @@FETCH_STATUS = 0   

            BEGIN   

            if(@countyid > 0)
            BEGIN

            INSERT INTO Territory_mapping(TerritoryID,Target_GeographyTypeID,Target_GeographyID,CreatedDate)
            VALUES(@TerritoryID,@Target_GeographyTypeID,@countyid,GETDATE())

            END

            FETCH NEXT FROM db_cursor INTO @countyid

            END   



            CLOSE db_cursor   

            DEALLOCATE db_cursor 




            -------------------- Zip Code -----------------------
            SELECT @Target_GeographyTypeID=GeographyTypeId from GeoGraphyTypes
            WHERE GeographyTypeName='Zipcode'

            DECLARE db_cursor CURSOR FOR  

            SELECT id

            FROM @tzip

            OPEN db_cursor   

            FETCH NEXT FROM db_cursor INTO @zipcodeid
            WHILE @@FETCH_STATUS = 0   

            BEGIN   

            IF(@zipcodeid > 0) 

            BEGIN

            INSERT INTO Territory_mapping(TerritoryID,Target_GeographyTypeID,Target_GeographyID,CreatedDate)
            VALUES(@TerritoryID,@Target_GeographyTypeID,@zipcodeid,GETDATE())

            END

            FETCH NEXT FROM db_cursor INTO @zipcodeid

            END   



            CLOSE db_cursor   

            DEALLOCATE db_cursor 












            RETURN @TerritoryID

            END

1 个答案:

答案 0 :(得分:1)

你试过这个:

INSERT INTO Territory_mapping
  (TerritoryID,Target_GeographyTypeID,Target_GeographyID,CreatedDate)
Select @TerritoryID, @Target_Geography, id, GETDATE()
From @t
Where id > 0

请注意,这纯粹是根据您提供的内容对代码进行的返工。我不知道其中一些变量来自何处。

另外,您说countryid是逗号分隔的值列表,但这不是您的代码使用它的方式。我的样本假定(是你的)@countryid(和扩展名,id)是单个值。如果不是这种情况,请更新您的代码以显示您如何处理以逗号分隔的列表。