0

我有这样的查询,

DECLARE @tmpSPJ TABLE(Kd_Rek_1 tinyint, Kd_Rek_2 tinyint, Kd_Rek_3 tinyint, Kd_Rek_4 tinyint, Kd_Rek_5 tinyint, Anggaran money, Gaji_L money, Gaji_I money, LS_L money, LS_I money, UP_L money, UP_I money)
    DECLARE @Peny_SPJ bit
    DECLARE @Tahun varchar(4), @Kd_Urusan varchar(3), @Kd_Bidang varchar(3), @Kd_Unit varchar(3), @Kd_Sub varchar(3), @Bulan tinyint

    SET @Kd_Urusan = '1'
    SET @Kd_Bidang = '2'
    SET @Kd_Unit = '1'
    SET @Kd_Sub = '0'
    SET @Tahun = '2013'
    SET @Bulan = '2'

    IF ISNULL(@Kd_Urusan, '') = '' SET @Kd_Urusan = '%'
    IF ISNULL(@Kd_Bidang, '') = '' SET @Kd_Bidang = '%'
    IF ISNULL(@Kd_Unit, '') = '' SET @Kd_Unit = '%'
    IF ISNULL(@Kd_Sub, '') = '' SET @Kd_Sub = '%'

    SELECT @Peny_SPJ = ISNULL(Peny_SPJ, 0)
    FROM Ref_Setting
    WHERE Tahun = @Tahun

    INSERT INTO @tmpSPJ
    SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5,
        SUM(A.Anggaran), SUM(A.Gaji_L), SUM(A.Gaji_I), SUM(A.LS_L), SUM(A.LS_I),
        SUM(A.UP_L), SUM(A.UP_I)
    FROM
        (
        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, A.Total AS Anggaran, 0 AS Gaji_L, 0 AS Gaji_I, 0 AS LS_L, 0 AS LS_I, 0 AS UP_L, 0 AS UP_I
        FROM Ta_RASK_Arsip A
        WHERE (A.Kd_Rek_1 = 5) AND (A.Tahun = @Tahun) AND (A.Kd_Perubahan = (SELECT MAX(Kd_Perubahan) FROM Ta_RASK_Arsip_Perubahan WHERE (Kd_Perubahan IN (4, 6, 8)) AND (LEFT(CONVERT(varchar, Tgl_Perda, 112), 6) <= (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2))) AND (Tahun = @Tahun))) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0, 
            CASE
            WHEN B.Jn_SPM IN (1, 2, 4) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN A.Nilai
            ELSE 0
            END AS Gaji_L, 0 AS Gaji_I, 
            CASE
            WHEN B.Jn_SPM IN (1, 2, 4) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 0
            ELSE A.Nilai
            END AS LS_L, 0 AS LS_I, 
            0 UP_L, 0 AS UP_I
        FROM Ta_SPM_Rinc A INNER JOIN
            Ta_SPM B ON A.Tahun = B.Tahun AND A.No_SPM = B.No_SPM INNER JOIN
            Ta_SP2D C ON B.Tahun = C.Tahun AND B.No_SPM = C.No_SPM
        WHERE (B.Jn_SPM = 3) AND (B.Kd_Edit <> 2) AND (LEFT(CONVERT(varchar, C.Tgl_SP2D, 112), 6) < (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2))) AND (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            AND (A.Kd_Rek_1 = 5)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0, 0 AS Gaji_L,  
            CASE
            WHEN B.Jn_SPM IN (1, 2, 4) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN A.Nilai
            ELSE 0
            END AS Gaji_I, 0 AS LS_L, 
            CASE
            WHEN B.Jn_SPM IN (1, 2, 4) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 0
            ELSE A.Nilai
            END AS LS_I, 0 AS UP_L,
            0 AS UP_I
        FROM Ta_SPM_Rinc A INNER JOIN
            Ta_SPM B ON A.Tahun = B.Tahun AND A.No_SPM = B.No_SPM INNER JOIN
            Ta_SP2D C ON B.Tahun = C.Tahun AND B.No_SPM = C.No_SPM
        WHERE (B.Jn_SPM = 3) AND (B.Kd_Edit <> 2) AND (LEFT(CONVERT(varchar, C.Tgl_SP2D, 112), 6) = (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2))) AND (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            AND (A.Kd_Rek_1 = 5)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0 AS Anggaran, 0 AS Gaji_L, 0 AS Gaji_I, 0 AS LS_L, 0 AS LS_I, A.Nilai_Setuju AS UP_L, 0 AS UP_I
        FROM Ta_Pengesahan_SPJ_Rinc A INNER JOIN
            Ta_Pengesahan_SPJ B ON A.Tahun = B.Tahun AND A.No_Pengesahan = B.No_Pengesahan
        WHERE (LEFT(CONVERT(varchar, B.Tgl_Pengesahan, 112), 6) < (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2))) AND (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0 AS Anggaran, 0 AS Gaji_L, 0 AS Gaji_I, 0 AS LS_L, 0 AS LS_I, 0 AS UP_L, A.Nilai_Setuju AS UP_I
        FROM Ta_Pengesahan_SPJ_Rinc A INNER JOIN
            Ta_Pengesahan_SPJ B ON A.Tahun = B.Tahun AND A.No_Pengesahan = B.No_Pengesahan
        WHERE (LEFT(CONVERT(varchar, B.Tgl_Pengesahan, 112), 6) = (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2))) AND (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0, 
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            ELSE 0
            END AS Gaji_L, 0 AS Gaji_I, 
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 0
            ELSE
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            END AS LS_L, 0 AS LS_I, 
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            ELSE 0
            END AS UP_L, 0 AS UP_I
        FROM Ta_Penyesuaian_Rinc A INNER JOIN
            Ta_Penyesuaian B ON A.Tahun = B.Tahun AND A.No_Bukti = B.No_Bukti
        WHERE (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            AND (B.Jns_P1 = 1) AND (LEFT(CONVERT(varchar, B.Tgl_Bukti, 112), 6) < (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2)))
            AND (@Peny_SPJ = 1)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0, 
            0 AS Gaji_L, 
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            ELSE 0
            END AS Gaji_I, 
            0 AS LS_L,
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN 0
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 0
            ELSE
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            END AS LS_I, 
            0 AS UP_L, 
            CASE
            WHEN B.Jn_SPM IN (2, 5) THEN
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            ELSE 0
            END AS UP_I
        FROM Ta_Penyesuaian_Rinc A INNER JOIN
            Ta_Penyesuaian B ON A.Tahun = B.Tahun AND A.No_Bukti = B.No_Bukti
        WHERE (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            AND (B.Jns_P1 = 1) AND (LEFT(CONVERT(varchar, B.Tgl_Bukti, 112), 6) = (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2)))
            AND (@Peny_SPJ = 1)

        UNION ALL

        SELECT A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5, 0, 
            CASE
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            ELSE 0
            END AS Gaji_L, 0 AS Gaji_I, 
            CASE
            WHEN (A.Kd_Rek_1 = 5) AND (A.Kd_Rek_2 = 1) AND (A.Kd_Rek_3 = 1) AND (A.Kd_Rek_4 = 1) THEN 0
            ELSE
                CASE A.D_K
                WHEN 'D' THEN A.Nilai
                ELSE -A.Nilai
                END
            END AS LS_L, 0 AS LS_I, 
            0 AS UP_L, 0 AS UP_I
        FROM Ta_Jurnal_Rinc A INNER JOIN
            Ta_Jurnal B ON A.Tahun = B.Tahun AND A.No_Bukti = B.No_Bukti
        WHERE (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            AND (LEFT(CONVERT(varchar, B.Tgl_Bukti, 112), 6) < (@Tahun + RIGHT('0' + CONVERT(varchar, @Bulan), 2)))
            AND (A.Kd_Rek_1 = 5) AND (@Peny_SPJ = 1) AND (B.No_BKU <> 9999)
        ) A
    GROUP BY A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5

    SELECT TOP 1 C.Kd_UrusanA, C.Kd_BidangA, C.Kd_UnitA, C.Kd_SubA,
        C.Kd_Urusan_Gab, C.Kd_Bidang_Gab, C.Kd_Unit_Gab, C.Kd_Sub_Gab,
        C.Nm_Urusan_Gab AS test, C.Nm_Bidang_Gab, C.Nm_Unit_Gab, C.Nm_Sub_Unit_Gab,
        B.Nm_Rek_5,
        CONVERT(varchar, A.Kd_Rek_1) + ' . ' + CONVERT(varchar, A.Kd_Rek_2) + ' . ' + CONVERT(varchar, A.Kd_Rek_3) + ' . ' + RIGHT('0' + CONVERT(varchar, A.Kd_Rek_4), 2) + ' . ' + RIGHT('0' + CONVERT(varchar, A.Kd_Rek_5), 2) AS Kd_Rek_5_Gab,
        A.Anggaran, A.Gaji_L, A.Gaji_I, A.Gaji_L + A.Gaji_I AS Gaji_T,
        A.LS_L, A.LS_I, A.LS_L + A.LS_I AS LS_T,
        A.UP_L, A.UP_I, A.UP_L + A.UP_I AS UP_T,
        A.Gaji_L + A.Gaji_I + A.LS_L + A.LS_I + A.UP_L + A.UP_I AS TOTAL_SPJ,
        A.Anggaran - (A.Gaji_L + A.Gaji_I + A.LS_L + A.LS_I + A.UP_L + A.UP_I) AS SISA,
        C.Nm_Pimpinan, C.Nip_Pimpinan, C.Jbt_Pimpinan, C.Nm_Bendahara, C.Nip_Bendahara, C.Jbt_Bendahara
    FROM @tmpSPJ A INNER JOIN
        Ref_Rek_5 B ON A.Kd_Rek_1 = B.Kd_Rek_1 AND A.Kd_Rek_2 = B.Kd_Rek_2 AND A.Kd_Rek_3 = B.Kd_Rek_3 AND A.Kd_Rek_4 = B.Kd_Rek_4 AND A.Kd_Rek_5 = B.Kd_Rek_5,
        (
        SELECT @Kd_Urusan AS Kd_UrusanA, @Kd_Bidang AS Kd_BidangA, @Kd_Unit AS Kd_UnitA, @Kd_Sub AS Kd_SubA,
            @Kd_Urusan AS Kd_Urusan_Gab,
            @Kd_Urusan + ' . ' + RIGHT('0' + @Kd_Bidang, 2) AS Kd_Bidang_Gab,
            @Kd_Urusan + ' . ' + RIGHT('0' + @Kd_Bidang, 2) + ' . ' + RIGHT('0' +  @Kd_Unit, 2) AS Kd_Unit_Gab,
            @Kd_Urusan + ' . ' + RIGHT('0' + @Kd_Bidang, 2) + ' . ' + RIGHT('0' +  @Kd_Unit, 2) + ' . ' + RIGHT('0' +  @Kd_Sub, 2) AS Kd_Sub_Gab,
            E.Nm_Urusan AS Nm_Urusan_Gab, D.Nm_Bidang AS Nm_Bidang_Gab, C.Nm_Unit AS Nm_Unit_Gab, B.Nm_Sub_Unit AS Nm_Sub_Unit_Gab,
            A.Nm_Pimpinan AS Nm_Pimpinan, A.Nip_Pimpinan AS Nip_Pimpinan, A.Jbt_Pimpinan AS Jbt_Pimpinan,
            G.Nm_Bendahara, G.Nip_Bendahara, G.Jbt_Bendahara
        FROM Ta_Sub_Unit A INNER JOIN
            Ref_Sub_Unit B ON A.Kd_Urusan = B.Kd_Urusan AND A.Kd_Bidang = B.Kd_Bidang AND A.Kd_Unit = B.Kd_Unit AND A.Kd_Sub = B.Kd_Sub INNER JOIN
            Ref_Unit C ON B.Kd_Urusan = C.Kd_Urusan AND B.Kd_Bidang = C.Kd_Bidang AND B.Kd_Unit = C.Kd_Unit INNER JOIN
            Ref_Bidang D ON C.Kd_Urusan = D.Kd_Urusan AND C.Kd_Bidang = D.Kd_Bidang INNER JOIN
            Ref_Urusan E ON D.Kd_Urusan = E.Kd_Urusan INNER JOIN
            (
            SELECT TOP 1 Tahun, Kd_Urusan, Kd_Bidang, Kd_Unit, Kd_Sub
            FROM Ta_Sub_Unit A
            WHERE (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            ORDER BY Tahun, Kd_Urusan, Kd_Bidang, Kd_Unit, Kd_Sub
            ) F ON A.Tahun = F.Tahun AND A.Kd_Urusan = F.Kd_Urusan AND A.Kd_Bidang = F.Kd_Bidang AND A.Kd_Unit = F.Kd_Unit AND A.Kd_Sub = F.Kd_Sub LEFT OUTER JOIN
            (
            SELECT Tahun, Kd_Urusan, Kd_Bidang, Kd_Unit, Kd_Sub, MIN(Nama) AS Nm_Bendahara, MIN(Nip) AS Nip_Bendahara, MIN(Jabatan) AS Jbt_Bendahara
            FROM Ta_Sub_Unit_Jab A
            WHERE (A.Kd_Jab = 4) AND (A.Tahun = @Tahun) AND (A.Kd_Urusan LIKE @Kd_Urusan) AND (A.Kd_Bidang LIKE @Kd_Bidang) AND (A.Kd_Unit LIKE @Kd_Unit) AND (A.Kd_Sub LIKE @Kd_Sub)
            GROUP BY Tahun, Kd_Urusan, Kd_Bidang, Kd_Unit, Kd_Sub
            ) G ON F.Tahun = G.Tahun AND F.Kd_Urusan = G.Kd_Urusan AND F.Kd_Bidang = G.Kd_Bidang AND F.Kd_Unit = G.Kd_Unit AND F.Kd_Sub = G.Kd_Sub
        ) C
    ORDER BY A.Kd_Rek_1, A.Kd_Rek_2, A.Kd_Rek_3, A.Kd_Rek_4, A.Kd_Rek_5

这是我试图执行它的php,

include ($_SERVER['DOCUMENT_ROOT'] . '/simda/classes/koneksi.php');
global $conn;
$kon = new koneksi();
$conn = $kon->bukaKoneksi();
$params = array();
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$rs = sqlsrv_query($conn, $query, $params, $options);
//$row = sqlsrv_fetch_array($rs, SQLSRV_FETCH_ASSOC);
if ($rs != NULL) {
    while ($row = sqlsrv_fetch_array($rs, SQLSRV_FETCH_ASSOC)) {
        echo 'testt';
    }
    $num_rows = sqlsrv_num_rows($rs);
    echo $num_rows;
}

结果只是行数,132 行(尽管在查询中我写了“top 1”)“testt”没有显示。如果我直接在 sql server 中执行我的查询,它只显示了 1 个它应该显示的数据。如果我尝试使用查询“SELECT * FROM my_table”,“testt”会正确显示。我不知道那里有什么问题。如此混乱。我的代码有什么问题吗?请帮助我,对不起我的英语,谢谢

4

1 回答 1

0

从查询中创建一个存储过程(例如 my_sp),然后从 PhP 运行它:“exec my_sp”

于 2013-08-24T15:43:36.743 回答