如何将此动态查询转换为普通的sql查询

时间:2016-08-11 14:31:06

标签: sql-server

如何将此动态查询转换为普通的sql查询

    SET @SQL = 'SELECT  DelayId, DelayCategory, DelayReason FROM [TAMS].[DelayMaintenance] WHERE IsUtilized = 1 AND ' + @CHECKPOINTTYPE + ' = ' + CAST(1 AS CHAR(1)) + 
       ' union      SELECT 0, NULL, ''--- Pick One----'' ORDER BY DelayId'

EXEC (@SQL) 

2 个答案:

答案 0 :(得分:1)

假设潜在列名称数量有限,您可以这样做:

class ReselectableSegmentedControl: UISegmentedControl {
    @IBInspectable var allowReselection: Bool = true
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let previousSelectedSegmentIndex = self.selectedSegmentIndex
        super.touchesEnded(touches, withEvent: event)
        if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
            if let touch = touches.first {
                let touchLocation = touch.locationInView(self)
                if CGRectContainsPoint(bounds, touchLocation) {
                    self.sendActionsForControlEvents(.ValueChanged)
                }
            }
        }
    }
}

否则,您可以在传递给SQL Server之前以服务器端语言构建查询,这与您已经使用的解决方案几乎相同。

答案 1 :(得分:0)

我试过这个

SELECT  DelayId, DelayCategory, DelayReason
    FROM [TAMS].[DelayMaintenance]
    WHERE CASE @CHECKPOINTTYPE
        WHEN 'TRUCK' THEN Truck
        WHEN 'RAIL' THEN RAIL
        WHEN 'BARGE' THEN BARGE
        ELSE NULL
      END = 1
      AND IsUtilized = 1
    union      SELECT 0, NULL, '--- Pick One----' ORDER BY DelayId