从CSV为SQLCMD查询声明值

时间:2016-05-18 08:15:29

标签: sql-server csv vbscript

我已经编写了一个SQL查询来搜索日期范围内的特定忠诚度销售,如下所示,是的,这是一个VBScript问题......

DECLARE @datefrom date = '01-Jul-2015'
DECLARE @dateto date = '23-Aug-2015'
SELECT CONVERT(nvarchar,[SaleDate],112) as SaleDate, year(saledate) as SaleYear, month(saledate) as salemonth, day(saledate) as saleday,
COUNT([CustID]) as Customers, SUM([SaleTotal]) as SaleTotal
FROM [Server].[dbo].[LoyaltyTransactions]
WHERE LoyaltyTranTypeID =11 and SaleDate between @datefrom and @dateto
GROUP BY convert(nvarchar,[SaleDate],112), year(saledate), month(saledate), day(saledate)
ORDER BY convert(nvarchar,[SaleDate],112)

问题是我不希望每次最终用户想要不同的日期范围时从/到修改日期,所以我决定尝试编写我的第一个VBScript。

我设法编写了以下内容,以便从HTA GUI中输出最终用户Start&结束日期(DD-MMM-YYYY)输入到CSV文件。

<html>
<head>
<title>LRR</title>
</head>
<HTA:APPLICATION
 CONTEXTMENU="no" 
 SCROLL="no"
 SINGLEINSTANCE="yes"
 Navigable="yes"
 BORDER="dialog"
/>
    <script language="VBScript">
    Sub SaveData
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists("F:\DISKSETS\SCRIPTS\SQLREPORT\Report.txt") Then
            objFSO.DeleteFile("F:\DISKSETS\SCRIPTS\SQLREPORT\Report.txt")
            Set objFile = objFSO.CreateTextFile("F:\DISKSETS\SCRIPTS\SQLREPORT\Report.txt")
            strLine = SD.Value & "," & ED.Value & ","   
            objFile.WriteLine strLine
            objFile.Close
        End If
    End Sub
    Sub Window_onLoad
        window.resizeTo 320,230
    End Sub
            Sub RunProgram
        Set objShell = CreateObject("Wscript.Shell")
        objShell.Run "F:\DISKSETS\SCRIPTS\SQLREPORT\Report.vbs"
    End Sub
</script>
<body>
<p><span style="font-size:16px;">&nbsp;</span><u><strong><span style="font-size:16px;">Loyalty Redemption Report</span></strong></u></p>
<p><strong><span style="font-size:14px;">&nbsp;START DATE : <input maxlength="14" name="SD" size="14" type="text" value="DD-MMM-YYYY" />&nbsp;</span></strong></p>
<p><strong><span style="font-size:14px;">&nbsp; &nbsp;END DATE :&nbsp;<input maxlength="14" name="ED" size="14" type="text" value="DD-MMM-YYYY" /></span></strong></p>
<p>&nbsp;<strong><span style="font-size:14px;"><input name="REPORT" type="button" value="REPORT" onclick="SaveData" /></span></strong></p>
</body>
</html>

这就是我遇到的问题我现在需要VBScript从CSV中提取两个日期并将它们放在上面写的SQL Query ive中。

从那里可以使用SQLCMD来获取VBSCript中的查询以输出到excel。

TIA。

1 个答案:

答案 0 :(得分:0)

您已经检索到SD.Value和ED.Value(可能是这些日期),那么为什么不将它们作为参数传递给vbscript调用呢?

<html>
<head>
<title>LRR</title>
</head>
<HTA:APPLICATION
 CONTEXTMENU="no" 
 SCROLL="no"
 SINGLEINSTANCE="yes"
 Navigable="yes"
 BORDER="dialog"
/>
    <script language="VBScript">
    Sub SaveData
        RunProgram SD.Value, ED.Value
    End Sub
    Sub Window_onLoad
        window.resizeTo 320,230
    End Sub
    Function RunProgram(startDate, endDate)
        Set objShell = CreateObject("Wscript.Shell")
        objShell.Run "F:\DISKSETS\SCRIPTS\SQLREPORT\Report.vbs " & startDate & " " & endDate
    End Function
</script>
<body>
<p><span style="font-size:16px;">&nbsp;</span><u><strong><span style="font-size:16px;">Loyalty Redemption Report</span></strong></u></p>
<p><strong><span style="font-size:14px;">&nbsp;START DATE : <input maxlength="14" name="SD" size="14" type="text" value="DD-MMM-YYYY" />&nbsp;</span></strong></p>
<p><strong><span style="font-size:14px;">&nbsp; &nbsp;END DATE :&nbsp;<input maxlength="14" name="ED" size="14" type="text" value="DD-MMM-YYYY" /></span></strong></p>
<p>&nbsp;<strong><span style="font-size:14px;"><input name="REPORT" type="button" value="REPORT" onclick="SaveData" /></span></strong></p>
</body>
</html>

在您的vbscript中,可以使用WScript.Arguments(0)WScript.Arguments(1)访问这些参数,指示传入的第一个和第二个参数。