0

我正在编写在网站上使用的查询。当需要查询时,它会直接解析到服务器,结果全部由查询控制。该网站只是返回表格。我在网站上拥有的是客户端可以选择的复选框,这些复选框被解析为查询中的@var = 1or 或@var = 0. 正因为如此,现在我有这个代码来检查和添加或不取决于它是否被检查。我的问题是,有没有比使用这样的IF语句更好的方法来解决这个问题,因为我有几个代码部分包括:

SET @sql = 'select distinct '
If @mgchk = 1 
    SET @sql = @sql + 'p.MainGroup'
If @sgchk = 1 and @sql = 'select distinct '
    SET @sql = @sql + 'p.SubGroup'
If @sgchk = 1 and @sql not like '%SubGroup'
    SET @sql = @sql + ',p.SubGroup'
If @ssgchk = 1 and @sql = 'select distinct '
    SET @sql = @sql + 'p.SubSubGroup'
If @ssgchk = 1 and @sql not like '%SubSubGroup'
    SET @sql = @sql + ',p.SubSubGroup'
If @Seasonchk = 1 and @sql = 'select distinct '
    SET @sql = @sql + 'p.Season'
If @Seasonchk = 1 and @sql not like '%Season'
    SET @sql = @sql + ',p.Season'
If @vendorchk = 1 and @sql = 'select distinct '
    SET @sql = @sql + 'p.VendorID'
If @vendorchk = 1 and @sql not like '%VendorID'
    SET @sql = @sql + ',p.VendorID'

SET @sql = 
    @sql + 
    ' into 
        ##aa 
    from 
        RPProducts p, 
        RPIv i, 
        RPTrsd d, 
        RPTrs s 
    WHERE 
    s.StoreID = d.StoreID and 
        s.ReceiptNO = d.ReceiptNO and 
        i.UPC = d.UPC and 
        i.StoreID = d.StoreID and 
        i.IVProduct = p.Productid and 
        s.TRSdate >= '''+ convert(varchar(10), @trsfrom, 101) +''' and 
        s.TRSdate <= '''+ convert(varchar(10), @trsto, 101) +''''
execute sp_executesql @sql

@mgchk / @sgchk / @ssgchk / @seasonchk / @vendorchk 是复选框变量

要回答@Aaron,

由于动态查询,全局临时。提取数据时,整个查询都会得到处理并立即删除。那里不会发生冲突。

我的日期变量是datetime,它在动态 SQL 中给了我一个错误。

是的,回忆同样的事情来检查,这就是整个问题的原因,如果有比IF检查更好的东西可以使用。

而且我发现使用别名连接更容易......

4

1 回答 1

4
-- rather than convert to a dangerously formatted string,
-- here is a much better way to strip time from a datetime
-- (if you need to!)

SET @trsfrom = DATEADD(DAY, DATEDIFF(DAY, 0, @trsfrom), 0);
SET @trsto = DATEADD(DAY, DATEDIFF(DAY, 0, @trsto), 0);

DECLARE @sql NVARCHAR(MAX) = N'SELECT DISTINCT ';

-- here's an easier way to strip the first comma:

SET @sql += SUBSTRING(
    CASE WHEN @mgchk     = 1 THEN ',p.MainGroup'   ELSE '' END
  + CASE WHEN @sgchk     = 1 THEN ',p.SubGroup'    ELSE '' END
  + CASE WHEN @ssgchk    = 1 THEN ',p.SubSubGroup' ELSE '' END
  + CASE WHEN @Seasonchk = 1 THEN ',p.Season'      ELSE '' END
  + CASE WHEN @vendorchk = 1 THEN ',p.VendorID'    ELSE '' END, 2, 2000);

SET @sql += ' INTO ##aa 
    FROM
      dbo.RPProducts AS p -- use schema prefix!
    INNER JOIN dbo.RPIv AS i  -- use PROPER JOINS!
      ON i.IVProduct = p.Productid
    INNER JOIN dbo.RPTrsd AS d 
      ON i.UPC = d.UPC 
      AND i.StoreID = d.StoreID
    INNER JOIN dbo.RPTrs AS s 
      ON s.StoreID = d.StoreID 
      AND s.ReceiptNO = d.ReceiptNO
    WHERE s.TRSdate >= @trsfrom -- use strongly typed parameters!
    AND s.TRSdate <= @trsto;';

EXEC sp_executesql @sql, 
    N'@trsfrom DATETIME, @trsto DATETIME', 
    @trsfrom, @trsto;
----^^^^^^^^^^^^^^^^ here is how the query gets the @trsfrom & @trsto values

我仍然认为您使用##global 临时表非常危险。如果两个人同时运行这段代码,他们就会遇到严重的问题。

于 2013-02-28T15:42:35.850 回答