使用bat文件生成SQL查询

时间:2017-07-15 07:05:32

标签: sql sql-server sql-server-2008 batch-file batch-processing

我有一个要求,我需要使用sql查询查看每周的数据。

我使用- (void)viewDidLoad { [super viewDidLoad]; selectedIndexPathsInSectionDictionary = [NSMutableDictionary dictionary]; // Do any additional setup after loading the view, typically from a nib. } -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { int numberOfItemsSelected = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] count]; if(((ComboItem*)comboItemsArray[indexPath.section]).itemNumberEachCombo == numberOfItemsSelected) { NSIndexPath *oldIndex = [[selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)] firstObject]; [tableView deselectRowAtIndexPath:oldIndex animated:NO]; [self tableView:tableView didDeselectRowAtIndexPath:oldIndex]; } return indexPath; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; [self selectItemAtIndexPath:indexPath]; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [comboTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; [self deselectItemAtIndexPath:indexPath]; } - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath { NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; if(array){ [array addObject:indexPath]; } else { array = [NSMutableArray array]; [array addObject:indexPath]; [selectedIndexPathsInSectionDictionary setObject:array forKey:@(indexPath.section)]; } } - (void)deselectItemAtIndexPath:(NSIndexPath*)indexPath { NSMutableArray* array = [selectedIndexPathsInSectionDictionary objectForKey:@(indexPath.section)]; [array removeObject:indexPath]; } 等查询。

现在我的要求是我需要一个批处理文件,可以根据用户选择给我这个脚本。像用户可以给出开始日期和结束日期,sql查询应该自动生成。

1 个答案:

答案 0 :(得分:1)

获取日期范围的一种更不容易出错的方法是使用图形日期选择器 比必须检查有效性的手动用户输入 (月/日名称将与您的语言环境匹配,而不是我的德语名称)

Graphical date picker

此PowerShell脚本:

# Function Pick-Date
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
Function Pick-Date { 
  $Cal = new-object System.Windows.Forms.MonthCalendar 
  $Cal.ShowWeekNumbers = $true 
  $Cal.MaxSelectionCount = 10 # change this value for the max date distance
  $Cal.Dock = 'Fill' 
  $Form = new-object Windows.Forms.Form 
  $Form.text = "Drag the mouse to select a date range then hit [enter]" 
  $Form.Size = new-object Drawing.Size @(656,620) 
  $btnSelect = new-object System.Windows.Forms.Button
  $btnSelect.Size = "1,1"
  $btnSelect.add_Click({ $Form.close() }) 
  $Form.Controls.Add($btnSelect ) 
  $Form.AcceptButton = $btnSelect
  $Form.Controls.Add($Cal) 
  $Form.Add_Shown({$Form.Activate()})  
  [void]$Form.showdialog() 
  return ("SELECT * from table Between '"+
         (Get-Date($Cal.SelectionStart) -format 'dd-MM-yyyy')+
         "' and '"+
         (Get-Date($Cal.SelectionEnd) -format 'dd-MM-yyyy')+
         "'")
} 
Pick-Date

将具有此输出,您可以保存为file.sql

PS> .\Pick-Date.ps1
SELECT * from table Between '27-08-2012' and '31-08-2012'

修改

powerShell脚本的批处理包装器将查询存储在变量SqlQuery

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
::Wrap Pick-Date.ps1 in same folder as batch

For /F "delims=" %%%A in (
  'Powershell -NoP -NonI -NoLogo -File Pick-Date.ps1 '
) Do Set "SqlQuery=%%A"

Set SqlQuery