Systems Engineering and RDBMS

Archive for October 12th, 2007

Bulk collect and Rowcount (SQL%BULK_ROWCOUNT)

Posted by decipherinfosys on October 12, 2007

In one of our previous blog post, we talked about how to handle NO_DATA_FOUND exception when we are using BULK COLLECT.  In yet another blog post, we also discussed about handling multiple exceptions in BULK COLLECT scenario.

We know that, to loop through records collected using bulk collect, we have to use the FORALL clause.  This blog post, we will see the mechanism to know the number of rows affected by the FORALL statement using SQL%BULK_ROWCOUNT attribute.  SQL cursor has one more attribute %BULK_ROWCOUNT on top of its regular attributes SQL%ISOPEN, SQL%FOUND, SQL%ROWCOUNT etc. While later attributes give information about last executed DML statement, Bulk attribute is used with FORALL statement and SQL%BULK_ROWCOUNT(i) gives the number of rows processed by ith iteration of any DML statement. This is because FORALL statement and SQL%BULK_ROWCOUNT attribute has the same number of elements or subscripts. So if we are bulk collecting 100 records, FORALL will loop through 1 to 100 and so is SQL%BULK_ROWCOUNT attribute. Let us go through an example.

Connect to SQL*Plus with proper credentials and run following query to create the table and populate it with some data.
CREATE TABLE TEST
(
Test_ID NUMBER(9) NOT NULL PRIMARY KEY,
Test_Desc VARCHAR(50),
CREATE_DATE DATE
)
/

INSERT INTO TEST(TEST_ID, TEST_DESC)
SELECT ROWNUM,OBJECT_TYPE
FROM USER_OBJECTS
/

COMMIT
/

10 rows created.

Run following PL/SQL block to see the rowcount affected by FORALL statement for an update.

DECLARE

TYPE ga_Test_DESC IS TABLE OF TEST.TEST_DESC%TYPE;

va_Test_DESC ga_Test_DESC;

BEGIN

SELECT DISTINCT Test_DESC
BULK COLLECT INTO va_Test_DESC
FROM Test;

FORALL i IN 1..VA_TEST_DESC.COUNT
UPDATE TEST
SET CREATE_DATE = SYSDATE
WHERE TEST_DESC = VA_TEST_DESC(i);

FOR i IN 1..VA_TEST_DESC.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE(’Records updated = ‘ || SQL%BULK_ROWCOUNT(i));
END LOOP;

END;
/

Here is the result set.

Records updated = 1
Records updated = 1
Records updated = 4
Records updated = 4

In our table, we have 4 records each with test_desc value as table and index and 1 record each for trigger and procedure. Since we are updating record using TEST_DESC column, output shows rows updated by an individual iteration. One drawback we can think of is that we know about rows affected by each iteration only after FORALL execution is complete which may not be the desired behavior for certain applications.

Posted in Oracle | No Comments »

Network Protocols Configuration in SQL Server Express

Posted by decipherinfosys on October 12, 2007

SQL Server Express Edition is typically used in those applications which access data locally i.e. the SQL Server Express Edition is installed on the same machine where the application is running. Because of this, the “Shared Memory” protocol is the only one that is enabled by default. However, if you have an application that needs to connect to the SQL Server Express Edition Instance from outside of that machine, you would need to enable the other protocols namely: TCP/IP and/or Named Pipes. VIA (Virtual Interface Architecture) is not supported for this edition.

If you know before-hand at the time of the installation that you would need this support, you can run setup.exe from the command line and use the parameter “DisableNetworkProtocols=0″ to enable the TCP/IP and the Named Pipes network protocols. If you need to do it after the install is done, then you can do so from SQL Server Configuration Manager.

sql_express_1.jpg

sql_express_2.jpg

In the two images shown above, you can see that the TCP/IP and the Named Pipes protocols are disabled by default. You can right click and choose properties and enable it and do further configuration if you need to. Re-start the services and you will be in business. And you should also start up the SQL Server Browser Service. You can do so either from the services panel (start/run and then services.msc and then enter) and then look for SQL Server Browser service or you can start it using the SQL Server Configuration Manager by using the SQL Server 2005 Services panel. We will cover the importance of the SQL Server Browser service in one of the upcoming blog posts.

Posted in SQL Server | No Comments »