1. /*-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
*/
2. --------INSERT DATA INTO SERVER AND STORE THE TEXT FILE INTO SERVER
DECLARE
@saveas VARCHAR(2048)
,@query VARCHAR(2048)
,@bcpquery VARCHAR(2048)
,@bcpconn VARCHAR(64)
,@bcpdelim VARCHAR(2)
SET @query = 'select * from tbl_name'
SET @saveas = 'D:\tbl_name.txt'
SET @bcpdelim = '|'
SET @bcpconn = '-T' -- Trusted
SET @bcpconn = '-U sql_database -P sql_password -d database_name -S server_name' -- SQL authentication
SET @bcpquery = 'bcp "' + replace(@query, char(10), '') + '" QUERYOUT "' + @saveas + '" -c -t^' + @bcpdelim + ' ' + @bcpconn
EXEC master..xp_cmdshell @bcpquery
3. --------TAKE COPY THE TXT FILE FROM SERVER AND PAST Destination machine
BULK INSERT dbo.tbl_bulkinsert
FROM 'D:\tbl_name.txt'
WITH ( FIELDTERMINATOR ='|', FIRSTROW =1 )
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
*/
2. --------INSERT DATA INTO SERVER AND STORE THE TEXT FILE INTO SERVER
DECLARE
@saveas VARCHAR(2048)
,@query VARCHAR(2048)
,@bcpquery VARCHAR(2048)
,@bcpconn VARCHAR(64)
,@bcpdelim VARCHAR(2)
SET @query = 'select * from tbl_name'
SET @saveas = 'D:\tbl_name.txt'
SET @bcpdelim = '|'
SET @bcpconn = '-T' -- Trusted
SET @bcpconn = '-U sql_database -P sql_password -d database_name -S server_name' -- SQL authentication
SET @bcpquery = 'bcp "' + replace(@query, char(10), '') + '" QUERYOUT "' + @saveas + '" -c -t^' + @bcpdelim + ' ' + @bcpconn
EXEC master..xp_cmdshell @bcpquery
3. --------TAKE COPY THE TXT FILE FROM SERVER AND PAST Destination machine
BULK INSERT dbo.tbl_bulkinsert
FROM 'D:\tbl_name.txt'
WITH ( FIELDTERMINATOR ='|', FIRSTROW =1 )
4. SELECT * FROM tbl_bulkinsert
No comments:
Post a Comment