关于将一个表分开导出成不同文件的办法
假设有如下表:
select * from wjz
结果为:
OrderID ProductID UnitPrice Quantity Discount
10252 20 64.80 40 0.05
10268 29 99.00 10 0
10272 20 64.80 6 0
10290 29 99.00 15 0
10292 20 64.80 20 0
10305 29 99.00 25 0.1
10329 38 210.80 20 0.05
10351 38 210.80 20 0.05
10353 38 210.80 50 0.2
10354 29 99.00 4 0
10360 29 99.00 35 0
10360 38 210.80 10 0
10369 29 99.00 20 0
10372 20 64.80 12 0.25
10372 38 210.80 40 0.25
10382 29 99.00 14 0
10384 20 64.80 28 0
10400 29 99.00 21 0
10417 38 210.80 50 0
10420 9 77.60 20 0.1
10424 38 210.80 49 0.2
10440 29 99.00 24 0.15
10465 29 99.00 18 0.1
10479 38 210.80 30 0
10510 29 123.79 36 0
10514 20 81.00 39 0
10515 9 97.00 16 0.15
10516 18 62.50 25 0.1
10517 59 55.00 4 0
10518 38 263.50 15 0
10523 20 81.00 15 0.1
10531 59 55.00 2 0
10535 59 55.00 15 0.1
10537 51 53.00 6 0
10540 38 263.50 30 0
10541 38 263.50 4 0.1
10549 51 53.00 48 0.15
10555 51 53.00 20 0.2
10558 51 53.00 20 0
10561 51 53.00 50 0
10566 18 62.50 18 0.15
10567 51 53.00 3 0
10567 59 55.00 40 0.2
10575 59 55.00 12 0
10583 29 123.79 10 0
10588 18 62.50 40 0.2
10593 20 81.00 21 0.2
10601 59 55.00 35 0
10605 59 55.00 20 0.05
10616 38 263.50 15 0.05
10617 59 55.00 30 0.15
10624 29 123.79 6 0
10629 29 123.79 20 0
10634 18 62.50 50 0
10634 51 53.00 15 0
10639 18 62.50 8 0
10644 18 62.50 4 0.1
10645 18 62.50 20 0
10660 20 81.00 21 0
10663 51 53.00 20 0.05
10665 51 53.00 20 0
10665 59 55.00 1 0
10666 29 123.79 36 0
10672 38 263.50 15 0.1
10679 59 55.00 12 0
10687 9 97.00 50 0.25
10687 29 123.79 10 0
10691 29 123.79 40 0
10693 9 97.00 6 0
10694 59 55.00 25 0
10698 29 123.79 12 0.05
10701 59 55.00 42 0.15
10703 59 55.00 35 0
10706 59 55.00 8 0
10709 51 53.00 28 0
10716 51 53.00 7 0
10719 18 62.50 12 0.25
10727 59 55.00 10 0.05
10731 51 53.00 30 0.05
10745 18 62.50 24 0
10745 59 55.00 45 0
10749 59 55.00 6 0
10750 59 55.00 25 0.15
10756 18 62.50 21 0.2
10757 59 55.00 7 0
10762 51 53.00 28 0
10772 29 123.79 18 0
10772 59 55.00 25 0
10776 51 53.00 120 0.05
10783 38 263.50 5 0
10787 29 123.79 20 0.05
10789 18 62.50 30 0
10791 29 123.79 14 0.05
10799 59 55.00 25 0
10800 51 53.00 10 0.1
10801 29 123.79 20 0.25
10802 51 53.00 30 0.25
10803 59 55.00 15 0.05
10805 38 263.50 10 0
10816 38 263.50 30 0.05
10817 38 263.50 30 0
10821 51 53.00 6 0
10823 59 55.00 40 0.1
10828 20 81.00 5 0
10828 38 263.50 2 0
10831 38 263.50 8 0
10834 29 123.79 8 0.05
10835 59 55.00 15 0
10838 18 62.50 25 0.25
10841 59 55.00 50 0
10843 51 53.00 4 0.25
10848 9 97.00 3 0
10851 59 55.00 42 0.05
10853 18 62.50 10 0
10857 29 123.79 10 0.25
10860 51 53.00 3 0
10861 18 62.50 20 0
10865 38 263.50 60 0.05
10870 51 53.00 2 0
10877 18 62.50 25 0
10878 20 81.00 20 0.05
10889 38 263.50 40 0
10892 59 55.00 40 0.05
10893 29 123.79 24 0
10897 29 123.79 80 0
10912 29 123.79 60 0.25
10927 20 81.00 5 0
10935 18 62.50 4 0.25
10947 59 55.00 4 0
10948 51 53.00 40 0
10953 20 81.00 50 0.05
10956 51 53.00 8 0
10964 18 62.50 6 0
10964 38 263.50 5 0
10965 51 53.00 16 0
10971 29 123.79 14 0
10977 51 53.00 10 0
10981 38 263.50 60 0
10985 18 62.50 8 0.1
10986 20 81.00 15 0
10993 29 123.79 50 0.25
10994 59 55.00 18 0.05
10995 51 53.00 20 0
10999 51 53.00 15 0.05
11005 59 55.00 10 0
11006 29 123.79 2 0.25
11007 29 123.79 10 0
11017 59 55.00 110 0
11018 18 62.50 10 0
11021 20 81.00 15 0
11021 51 53.00 44 0.25
11026 18 62.50 8 0
11026 51 53.00 10 0
11028 59 55.00 24 0
11030 29 123.79 60 0.25
11030 59 55.00 100 0.25
11032 38 263.50 25 0
11032 59 55.00 30 0
11036 59 55.00 30 0
11045 51 53.00 24 0
11053 18 62.50 35 0.2
11055 51 53.00 20 0
11077 20 81.00 1 0.04
要求:我想分产品号将该表导出成不同的文件,文件名以P_产品号.txt来命名,导出到E盘根目录。
一、创建一个存储过程,以便允许参数输入
Create proc exp1
@productid int
As
Select * from wjz where productid=@productid
Go
二、生成调用该过程的SQLCMD命令,并利用构造语句
Select distinct 'sqlcmd -U sa -P 123 -S wangjingzheng -d wjz -Q"exec exp1 '
+convert(varchar(5),productid)+'" -o e:\P_'+convert(varchar(5),productid)+'.txt'
from wjz
三、将生成的语句粘出到DOS命令提示符下执行即可