Oracle executes Multiple SQL statements

Source: Internet
Author: User
How.. Net executes Multiple SQL statements at a time. Generally, only one SQL statement can be executed at a time unless a stored procedure is used. For example, the following services are available: when deleting a user (assuming userid = 5), you also need to delete all posts and replies from the user on the Forum (to ensure the integrity of the reference ), assume that the user table is users, the post table is articles (with The USERID field), and the reply table is replys (with The USERID field ), generally, there are three steps: (1) Delete all replies with userid = 5 in the replys table; (2) Delete All posts with userid = 5 in the articles table; (3) delete users whose userid is 5 in the users table.
In each step above, create an oracleconnection, create an oraclecommand, executenonquery () method, then oraclecommand call dispose () method, and finally oracleconnection call the close () method. This process is executed in the above process. We know that database connection operations are time-consuming. Is there any better way? The answer is yes.
PS/SQL is used. Begin
Delete   From Replys Where Userid = 5 ; -- Delete replies
Delete   From Articles Where Userid = 5 ; -- Delete post
Delete   From Users Where Userid = 5 ; -- Delete a user
End ;

The entire process can be written as follows: Private   Void Deleteuser ( Int Userid)
{
String Deletesql =   " Begin Delete from replys where userid = {0}; delete from articles where userid = {1}; delete from users where userid = {2}; end; " ;
Deletesql = String. Format (deletesql, userid );
Oracleconnection connection =   New Oracleconnection (connectionstring );
Connection. open ();
Oraclecommand cmd =   New Oraclecommand (deletesql, connection );
Cmd. Dispose ();
Connection. Close ();
}

In this way, all the operations can be completed in one connection. Of course, thisCodeTransaction processing is not considered. In actual use, you may consider adding as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.