Sql server 2008 我正在为你的网站添加书签,我将阅读并希望从中学习。最后一个问题是关于您的代码的,因此我了解它的作用以及我可以更改的内容DATEADD(DAY,DATEDIFF(DAY,0,@trsto),0)DAY和0做什么?此代码是否符合SQL Server 2005?

Sql server 2008 我正在为你的网站添加书签,我将阅读并希望从中学习。最后一个问题是关于您的代码的,因此我了解它的作用以及我可以更改的内容DATEADD(DAY,DATEDIFF(DAY,0,@trsto),0)DAY和0做什么?此代码是否符合SQL Server 2005?,sql-server-2008,if-statement,Sql Server 2008,If Statement,我正在为你的网站添加书签,我将阅读并希望从中学习。最后一个问题是关于您的代码的,因此我了解它的作用以及我可以更改的内容DATEADD(DAY,DATEDIFF(DAY,0,@trsto),0)DAY和0做什么?此代码是否符合SQL Server 2005?作为我们仍然使用2005年的一些客户,我并不是想说你错了。我在MsSQL上只工作了2个月,所以我仍在努力学习是非,改掉坏习惯。你的答案正是我想要的。非常感谢你。我只有几个问题。用户在网站上选择一个被解析为datetime的日期,查询在哪里看到这


我正在为你的网站添加书签,我将阅读并希望从中学习。最后一个问题是关于您的代码的,因此我了解它的作用以及我可以更改的内容
DATEADD(DAY,DATEDIFF(DAY,0,@trsto),0)
DAY和
0
做什么?此代码是否符合SQL Server 2005?作为我们仍然使用2005年的一些客户,我并不是想说你错了。我在MsSQL上只工作了2个月,所以我仍在努力学习是非,改掉坏习惯。你的答案正是我想要的。非常感谢你。我只有几个问题。用户在网站上选择一个被解析为datetime的日期,查询在哪里看到这些信息?在声明中,N'做什么?那么,2000年代表了什么?再次感谢你much@JohnZ我在代码中标记了datetime参数如何传递到查询中。您不需要转换为字符串并连接以传入文本(取决于它们的使用方式),将它们作为参数传递给sp_executesql()更安全。
N
前缀表示包含的字符串是Unicode,sp_executesql需要Unicode值。
22000
的意思是“从第二个字符开始,取2000个字符”——这去掉了前面的逗号。先生,你真是一个了不起的人,能帮助我。我正在为你的网站添加书签,我将阅读并希望从中学习。最后一个问题是关于您的代码的,因此我了解它的作用以及我可以更改的内容
DATEADD(DAY,DATEDIFF(DAY,0,@trsto),0)
DAY和
0
做什么?此代码是否符合SQL Server 2005?作为一些客户,我们仍然使用2005?
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
-- 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