使用批量复制的CASE语句,导出到csv

时间:2018-02-05 13:32:57

标签: sql-server csv tsql case export-to-csv

  

我使用批量复制实用程序将文件导出到.csv,我有八个   表格,并希望 避免制作八个不同的程序 ,我该怎么做   把它全部放在这个里面?

这是我的尝试......

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ('[dbo].[generateCSV]') IS NOT NULL
DROP PROCEDURE [dbo].[generateCSV]
GO

  CREATE PROCEDURE [dbo].[generateCSV]

(
 @table varchar(100),
 @output varchar(100), 
 @date varchar(12),
 @server varchar(30)
)

AS

BEGIN

DECLARE @sqlClients varchar(8000)



SELECT @sqlClients = 

   CASE @table WHEN 'Clients' THEN 'bcp "select 
* from ' + DB_NAME() + '.dbo.' + @table  + ' 
where ReportingDate = ''' + @date + '''"' + ' queryout ' +  @output + '  -c 
-C65001 -t";" -r"\n" -T -S' + @server 

  WHEN 'Receivables' THEN 'bcp "SELECT * from ' + DB_NAME() + + '.dbo.' + 
   @table  + ' where ReportingDate = ''' + @date + '''"' + ' queryout ' +  
   @output + '  -c -C65001 -t";" -r"\n" -T -S' + @server 

  ...
END

exec master..xp_cmdshell @sqlClients 




       -- Main EXEC

EXEC dbo.generateCSV @table = 'Clients', @date = '2017-10-31', @output = 
'//172.18.16.109/share/Test.csv (server with export target location ) ', 
@server = '172.18.16.108(server we are connected to and from which we are 
taking the data)' 

除了客户端和应收账款之外,我还有8个表,而基于主程序调用中提供的@table参数的 我想要不同的bcp select。 我该怎么做? 当我运行上面的脚本时,我会收到:

enter image description here

1 个答案:

答案 0 :(得分:2)

错误是由于在为@sqlClients分配值时不恰当地结束了case语句。只需添加'结束'以下语句末尾的关键字 -

SELECT @sqlClients =

$(document).ready(function () {
        fixTabulation();
    });


function fixTabulation() {
    /* This can be used to auto-assign tab-indexes, or
    #  commented out if it manual tab-indexes have
    #  already been assigned.
    */
    $('[tabindex]:not([tabindex="0"]):not([tabindex^="-"])').filter(":visible").each(function () {           
        $(this).attr('tabindex', Tab.i);
        Tab.i++;
        Tab.items++;
    });

    Tab.i = 0;

    /* We need to listen for any forward or backward Tab
    #  key event tell the page where to focus next.
    */
    $(document).on({
        'keydown': function (e) {                
            if (navigator.appVersion.match("Safari")) {                    
                if (e.keyCode == 9 && !e.shiftKey) { //Tab key pressed
                    e.preventDefault();
                    Tab.i != Tab.items ? Tab.i++ : Tab.i = 1;                        
                    if ($('[tabindex="' + Tab.i + '"]').prop('class').match('dropdown-toggle'))
                        $('[tabindex="' + Tab.i + '"]').click();
                    else
                        $('input[tabindex="' + Tab.i + '"], select[tabindex="' + Tab.i + '"], textarea[tabindex="' + Tab.i + '"]').not('input[type="hidden"]').focus();
                }
                if (e.shiftKey && e.keyCode == 9) { //Tab key pressed
                    e.preventDefault();
                    Tab.i != 1 ? Tab.i-- : Tab.i = Tab.items;
                    if ($('[tabindex="' + Tab.i + '"]').prop('class').match('dropdown-toggle'))
                        $('[tabindex="' + Tab.i + '"]').click();
                    else
                        $('input[tabindex="' + Tab.i + '"], select[tabindex="' + Tab.i + '"], textarea[tabindex="' + Tab.i + '"]').not('input[type="hidden"]').focus();
                }
            }
        }
    });

    /* We need to update Tab.i if someone clicks into
    #  a different part of the form.  This allows us
    #  to keep tabbing from the newly clicked input
    */
    $('button[tabindex], input[tabindex], select[tabindex], textarea[tabindex]').not('input[type="hidden"]').focus(function (e) {
        Tab.i = $(this).attr('tabindex');
        console.log(Tab.i);
    });
}
相关问题