在线等待各位答复,怎么写目录名称是变量的bcp语句呢?
declare @user varchar(100)
declare @pw varchar(100)
set @user = 'sa '
set @pw = '16898758 '
declare @qushi varchar(20)
declare @sheng varchar(20)
declare @sql varchar(1000)
declare cur_qushi scroll cursor for select distinct 区市 from 手机话段移动话段 for read only
open cur_qushi
fetch next from cur_qushi into @qushi
while @@fetch_status = 0
begin
set @sql = 'select Col001 from mobile.dbo.blacklist where left(Col001, 7) in(select 号段 from mobile.dbo.手机话段移动话段 where 区市 = ' ' ' + @qushi + ' ' ') '
exec outsheng @qushi1=@qushi, @sheng1=@sheng output
set @sql = 'bcp " ' + @sql + ' " queryout D:\numbers\ '+@sheng+ '\ ' + @qushi + '.txt -c -U ' + @user + ' -P ' + @pw
exec master..xp_cmdshell @sql
fetch next from cur_qushi into @qushi
end
close cur_qushi
deallocate cur_qushi
请大家指正,我的错误是:SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver]无法打开 BCP 主数据文件
NULL
我的手机话段移动话段表格式为:
区号 省份 区市 号段
0510江苏无锡1340000
0510江苏无锡1340001
0510江苏无锡1340002
0510江苏无锡1340003
0510江苏无锡1340004
025江苏南京1340005
025江苏南京1340006
025江苏南京1340007
0511江苏镇江1340008
我的blacklist表的格式为:
Col001
13709499546
13999045904
13566036035
13608334588
13575856588
13883743004
13977378668
[解决办法]
--搞定了
--BCP不能帮你建立目录
declare @user varchar(100)
declare @pw varchar(100)
set @user = 'sa '
set @pw = '16898758 '
declare @qushi varchar(20)
declare @sheng varchar(20)
declare @mdsql varchar(1000)
declare @sql varchar(1000)
declare cur_qushi scroll cursor for select distinct 区市 from 手机话段移动话段 for read only
open cur_qushi
fetch next from cur_qushi into @qushi
while @@fetch_status = 0
begin
set @sql = 'select Col001 from mobile.dbo.blacklist where left(Col001, 7) in(select 号段 from mobile.dbo.手机话段移动话段 where 区市 = ' ' ' + @qushi + ' ' ') '
exec outsheng @qushi1=@qushi, @sheng1=@sheng output
set @mdsql = 'if not exist D:\numbers\ '+@sheng+ ' md D:\numbers\ '+@sheng
--print(@mdsql)
exec master..xp_cmdshell @mdsql
set @sql = 'bcp " ' + @sql + ' " queryout D:\numbers\ '+@sheng+ '\ ' + @qushi + '.txt -c -U ' + @user + ' -P ' + @pw
--print(@sql)
exec master..xp_cmdshell @sql
fetch next from cur_qushi into @qushi
end
close cur_qushi
deallocate cur_qushi