|
-Assuming directory D:\numbers already exists
-Establish test data
if object_id ('Mobile segment mobile segment') is not null drop table Mobile segment mobile segment
create table Mobile segment mobile segments
insert Mobile Talk Mobile Talk
select '0510', 'Jiangsu', 'Wuxi', '1340000' union all
select '0510', 'Jiangsu', 'Wuxi', '1340001'union all
select '0510', 'Jiangsu', 'Wuxi', '1340002'union all
select '0510', 'Jiangsu', 'Wuxi', '1340003'union all
select '0510', 'Jiangsu', 'Wuxi', '1340004'union all
select '025', 'Jiangsu', 'Nanjing', '1340005'union all
select '025', 'Jiangsu', 'Nanjing', '1340006'union all
select '025', 'Jiangsu', 'Nanjing', '1340007'union all
select '0511', 'Jiangsu', 'Zhenjiang', '1340008'union all
select '020', 'Guangdong', 'Guangzhou', '1300008'
go
if object_id ('blacklist') is not null drop table blacklist
create table blacklist (Col001 varchar (100))
insert blacklist
select '13400009546' union all
select '13999045904' union all
select '13566036035' union all
select '13608334588' union all
select '13575856588' union all
select '13883743004' union all
select '13977378668' union all
select '13000088668'
go
-Establish a stored procedure
USE mobile
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'outsheng' AND type = 'p')
DROP procedure outsheng
go
create procedure outsheng
@ qushi1 varchar (20),
@ sheng1 varchar (20) output
as
select @ sheng1 = Province from mobile.dbo. mobile segment mobile segment where city == qq1
return
go
--Write cursor
declare @user varchar (100)
declare @pw varchar (100)
set @user = 'sa'
set @pw = 'ggs'
declare @qushi varchar (20)
declare @sheng varchar (20)
declare @mdsql varchar (1000)
declare @sql varchar (1000)
declare cur_qushi scroll cursor for select distinct Districts from cell phone segment mobile segment 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 number segment from mobile.dbo. mobile segment where mobile segment where district city =' '' + @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
--View Results
--Go to the D:\numbers directory, you can see the two directories "Jiangsu" and "Guangdong"
-There are two files in the "Jiangsu" directory: Nanjing.txt, Wuxi.txt (Contents: 14000095546)
-There is a file in the "Jiangsu" directory: Guangdong.txt (content: 13000088668) |
|