SQL Server uses BCP to import and export large volumes of data

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

Preset conditions:

Log in to SQL Server Management Studio using the SA account, right-click Security-Login-database username properties, set server role to sysadmin.

Delete A stored procedure that already exists
String job_bcp_delete_sql = "IF EXISTS (SELECT * from sys.objects WHERE name = ' procedure_bcp_out ' and type in (n ' P ', n ' PC '= conn.preparestatement (job_bcp_delete_sql);p re.execute ();
Create a stored procedure
String sql = "Set @sql = N ' BCP ' + @tableName + ' out ' + @filePath + '-c-t-u \" ' + @userName + ' \ "-P \" ' + @password + ' \ ' s \ ' + @dbIp + ' \ ' \ n '; StringBuffer Createsqlbuf=NewStringBuffer (); Createsqlbuf.append ("CREATE PROCEDURE procedure_bcp_out \ n"). Append ("@dbIp nvarchar, \ n"). Append ("@userName nvarchar, \ n"). Append ("@password nvarchar, \ n"). Append ("@tableName nvarchar, \ n"). Append ("@filePath nvarchar \ n"). Append ("As\n"). Append ("Begin\n"). Append ("EXEC master.sys.sp_configure ' show advanced options ', 1\n"). Append ("Reconfigure\n"). Append ("EXEC master.sys.sp_configure ' xp_cmdshell ', 1\n"). Append ("Reconfigure\n"). Append ("Declare @sql nvarchar \ n"). append (SQL). Append ("EXEC Master. xp_cmdshell @sql \ n "). Append ("EXEC master.sys.sp_configure ' show advanced options ', 1\n"). Append ("Reconfigure\n"). Append ("EXEC master.sys.sp_configure ' xp_cmdshell ', 0\n"). Append ("Reconfigure\n"). Append ("END"); String Createsql=createsqlbuf.tostring ();p re=conn.preparestatement (createsql);p re.execute ();
Executing stored procedures
New StringBuffer (); Execsqlbuf.append ("EXECUTE procedure_bcp_out \ n")    . Append ("@dbIp =?,"). Append ("@userName =?,"). Append ("@password =?,").    Append ("@tableName =?,"). Append ("@filePath =?").  == conn.preparestatement (execsql);p re.setstring (1, Dbip);p re.setstring (2 , Dbusername);p re.setstring (3, Dbpassword);p re.setstring (4, TableName);p re.setstring ( 5, file);p Re.execute ();

SQL Server uses BCP to import and export large volumes of data

Related Article

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.