简体   繁体   中英

Stored procedure WHERE clause error (expects parameter)

ALTER PROCEDURE [dbo].[sp_twa_report1]
    (@date1 as DATE,
     @date2 as DATE,
     @Filter1 as INT,
     @Kheadoption as NVARCHAR(10),
     @Kcodeoption as NVARCHAR(10),
     @Ktypeoption as NVARCHAR(10),
     @Shiftoption as INT)
AS
BEGIN
    SELECT
        wageentry.empno, wageentry.kdate, wageentry.kcode,
        empmain.paybookno, 
        kamjaricode.kdesc, kamjarihead.khead, kamjarihead.khdesc,
        wageentry.shiftno
    INTO
        #tmpTbl1
    FROM
        wageentry, empmain, kamjaricode, kamjarihead
    WHERE 
        wageentry.empno = empmain.empno
        AND wageentry.kcode = kamjaricode.kcode
        AND kamjaricode.khead = kamjarihead.khead
        AND wageentry.kdate >= @date1 AND wageentry.kdate <= @date2
        AND (wageentry.shiftno = CASE @Shiftoption 
                                     WHEN 1 THEN 1 
                                     WHEN 2 THEN 1 
                                     WHEN 3 THEN 2 
                                     ELSE 5 
                                 END
             OR wageentry.shiftno = CASE @Shiftoption 
                                        WHEN 1 THEN 2 
                                        WHEN 2 THEN 1 
                                        WHEN 3 THEN 2 
                                        ELSE 5 
                                    END
            OR wageentry.shiftno = CASE @Shiftoption
                                       WHEN 1 THEN 5  
                                       WHEN 2 THEN 1 
                                       WHEN 3 THEN 2 
                                       ELSE 5 
                                   END)
        AND ((@Kheadoption IS NULL) OR (kamjaricode.khead = @Kheadoption))
        AND ((@Kcodeoption IS NULL) OR (kamjaricode.kcode = @Kcodeoption))
        AND ((@Ktypeoption IS NULL) OR (kamjaricode.ktype = @Ktypeoption))

    -- More code here

End

The above stored procedure works fine from Management Studio, but when I run it from my Winforms app, I get an error:

Procedure or function 'sp_twa_report1' expects parameter '@Kheadoption', which was not supplied.

The parameters @Kheadoption, @Kcodeoption, @Ktypeoption are null as default. So from my understanding those lines of the query should be ignored. Where am I going wrong?

My Winforms code is:

commandText = "sp_twa_report1";

SqlCommand command = new SqlCommand(commandText, cs);
command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@date1", SqlDbType.Date).Value = mySdate;
command.Parameters.Add("@date2", SqlDbType.Date).Value = myEdate;
command.Parameters.Add("@Filter1", SqlDbType.Int).Value = Filter1;
command.Parameters.Add("@Kheadoption",SqlDbType.NVarChar).Value = Kheadoption;
command.Parameters.Add("@Kcodeoption", SqlDbType.NVarChar).Value = Kcodeoption;
command.Parameters.Add("@Ktypeoption", SqlDbType.NVarChar).Value = Ktypeoption;
command.Parameters.Add("@Shiftoption", SqlDbType.Int).Value = myShiftno;
        
cs.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
cs.Close();

The parameters @Kheadoption, @Kcodeoption, @Ktypeoption are null as default. So from my understanding those lines of the query should be ignored.

Consider using DBNull instead of null when passing parameters for a stored procedure.

Per MSDN : When you send a null parameter value to the server, you must specify DBNull, not null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM