--生成数据处理应答文件 set @m_tbname=’[##temp’+cast(newid() as varchar(40))+’]’ set @sql=’select * into ’+@m_tbname+’ from( select null as 类型 union all select 0 as 前缀 union all select ’+@fsize+’ as 长度 union all select null as 结束 union all select null as 格式 ) a’ exec(@sql) select @fname_in=@fname+’_temp’ ,@sql=’bcp "’+@m_tbname+’" out "’+@fname_in +’" /S"’+@servename +case when isnull(@username,’’)=’’ then ’’ else ’" /U"’+@username end +’" /P"’+isnull(@password,’’)+’" /c’ exec master..xp_cmdshell @sql --删除临时表 set @sql=’drop table ’+@m_tbname exec(@sql) if @isout=1 begin set @sql=’bcp "select top 1 ’+@fdname+’ from ’ +@tbname+case isnull(@tj,’’) when ’’ then ’’ else ’ where ’+@tj end +’" queryout "’+@fname +’" /S"’+@servename +case when isnull(@username,’’)=’’ then ’’ else ’" /U"’+@username end +’" /P"’+isnull(@password,’’) +’" /i"’+@fname_in+’"’ exec master..xp_cmdshell @sql end else begin --为数据导入准备临时表 set @sql=’select top 0 ’+@fdname+’ into ’ +@m_tbname+’ from ’ +@tbname exec(@sql) --将数据导入到临时表 set @sql=’bcp "’+@m_tbname+’" in "’+@fname +’" /S"’+@servename +case when isnull(@username,’’)=’’ then ’’ else ’" /U"’+@username end +’" /P"’+isnull(@password,’’) +’" /i"’+@fname_in+’"’ exec master..xp_cmdshell @sql --将数据导入到正式表中 set @sql=’update ’+@tbname +’ set ’+@fdname+’=b.’+@fdname +’ from ’+@tbname+’ a,’ +@m_tbname+’ b’ +case isnull(@tj,’’) when ’’ then ’’ else ’ where ’+@tj end exec(@sql) --删除数据处理临时表 set @sql=’drop table ’+@m_tbname end --删除数据处理应答文件 set @sql=’del ’+@fname_in exec master..xp_cmdshell @sql go /** 导入文本文件 EXEC master..xp_cmdshell ’bcp "dbname..tablename" in c:\DT.txt -c -Sservername -Usa -Ppassword’
改为如下,不需引号 EXEC master..xp_cmdshell ’bcp dbname..tablename in c:\DT.txt -c -Sservername -Usa -Ppassword’ /** 导出文本文件 EXEC master..xp_cmdshell ’bcp "dbname..tablename" out c:\DT.txt -c -Sservername -Usa -Ppassword’ 此句需加引号
|