我在我的DataTable SQL语句中有这个查询,现在我很困惑,如何将日期参数传递给这个查询,日期只有2,startdate和enddate,但日期重复了几次。
SELECT
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'B' OR
shift = 'E' OR
shift = 'F' OR
shift = 'G' OR
shift = 'H' OR
shift = 'J' OR
shift = 'P') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS P,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_8
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'B' OR
shift = 'N' OR
shift = 'O' OR
shift = 'S' OR
shift = 'T') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS S,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_7
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'B' OR
shift = 'M' OR
shift = 'Q') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS M,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_6
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'C') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS C,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_5
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'D') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS D,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_4
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'I') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS I,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_3
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'K') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS K,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_2
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'L') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102))) AS L,
(SELECT COUNT(shift) AS Expr1
FROM dbo.JadwalKerja AS JadwalKerja_1
WHERE (nip = dbo.JadwalDinas_DaftarKaryawan.NIP) AND (shift = 'R') AND (date >= CONVERT(DATETIME, '2015-12-01 00:00:00', 102)) AND (date <= CONVERT(DATETIME, '2015-12-31 00:00:00', 102)))
AS R
FROM dbo.JadwalDinas_DaftarKaryawan
ORDER BY Nama
答案 0 :(得分:3)
您可以声明变量以保存这些值:
DECLARE @sDate datetime, @eDate datetime
SET @sDate = '2015-12-01 00:00:00'
SET @eDate = '2015-12-31 00:00:00'
然后更换
CONVERT(DATETIME, '2015-12-01 00:00:00', 102)
同
@sDate
和
CONVERT(DATETIME, '2015-12-31 00:00:00', 102)
与@eDate
您可以创建一个包含所有此查询的存储过程,并将这些变量作为参数重新接收
CREATE PROCEDURE mySP @sDate datetime, @eDate datetime
AS
--Your query here