SQL Server Equivalent of Bulk Collect in Oracle

时光总嘲笑我的痴心妄想 提交于 2020-01-07 04:44:24

问题


I want to replicate the following code in SQL Server. I've had a look around and it seems like the only option is to just use a straight cursor, but is there a better way:

DECLARE

    CURSOR cursor1 IS
    SELECT *
    FROM table1;

    TYPE cursor_aat IS TABLE OF cursor1%ROWTYPE;        

    l_cursor cursor_aat;
BEGIN        
    OPEN cursor1;

    LOOP

        FETCH cursor1
        BULK COLLECT INTO l_cursor LIMIT 200;

        FOR INDX IN 1 .. l_cursor.COUNT LOOP
            IF (CheckSomeData() = 0) THEN           
                INSERT INTO new_table
                (col1, col2)
                values
                (l_cursor(INDX).col1, l_cursor(INDX).col2);

                INSERT INTO new_table2
                ...

            end if

        END LOOP;

        EXIT WHEN l_cursor.COUNT < 200;
...

回答1:


You could use a temporary table and a cursor if you want to do 200 rows at once.



来源:https://stackoverflow.com/questions/12533802/sql-server-equivalent-of-bulk-collect-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!