Stored Procedures for batch data deletion under. Net (using dynamic SQL)

Source: Internet
Author: User

SQL statement execution is normal

Public bool deleteuser (string userid)

{
String strsql = "delete from tb_users where userid in (" + userid + ")";
Return dbbase. executenonquery (false, strsql, null );

}

If a problem occurs during execution of the stored procedure, the string type cannot be converted to int32. The problem lies in the stored procedure:

Public static int deleteuser (string userid)
{
Int ret = 0;
String spname = "ty_deleteuser ";
Object [] para = new object [] {userid };
Ret = dataaccess. executenonquery (spname, false, para );
Return ret;

}

Incorrect Stored Procedure Syntax:

Create proc ty_deleteuser
(@ Userid INT)
As
Delete from tb_users where userid in (@ userid)

 

Correct Writing of stored procedures (dynamic SQL)

Create proc ty_deleteuser

(@ Userid nvarchar (250 ))
As

Exec ('delete from tb_users where userid in ('+ @ userid + ')')

 

The above is a cainiao-level problem. You don't need to read it if you know it. If you don't know it, you can read it. Thank you .....

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.