복붙노트

[SQL] 스칼라 반환 함수를 호출 할 때 ExecuteScalar는 항상 널 (null)을 반환

SQL

스칼라 반환 함수를 호출 할 때 ExecuteScalar는 항상 널 (null)을 반환

왜이는 null합니까?

//seedDate is set to DateTime.Now; con is initialized and open. Not a problem with that
using (SqlCommand command = new SqlCommand("fn_last_business_date", con))
{
       command.CommandType = CommandType.StoredProcedure;
       command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
       object res = command.ExecuteScalar(); //res is always null 
}

제가 DB에 직접 전화를하지만 다음과 같습니다 :

select dbo.fn_last_business_date('8/3/2011 3:01:21 PM') 
returns '2011-08-03 15:01:21.000' 

이는 내가 코드에서 호출 할 때 볼 것으로 예상 결과입니다

왜 왜 왜?

해결법

  1. ==============================

    1.시험:

    시험:

    using (SqlCommand command = new SqlCommand("select dbo.fn_last_business_date(@seed_date)", con))
    {
           command.CommandType = CommandType.Text;
           command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
           object res = command.ExecuteScalar(); //res is always null 
    }
    
  2. ==============================

    2.왜 모두가 선택 구문을 주장합니까? ..

    왜 모두가 선택 구문을 주장합니까? ..

    using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("calendar.CropTime", c))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.DateTime).Direction = ParameterDirection.ReturnValue;
        cmd.Parameters.AddWithValue("@d", DateTime.Now);
    
        cmd.ExecuteNonQuery();
    
        textBox1.Text = cmd.Parameters["@RETURN_VALUE"].Value.ToString();
    
    }
    
  3. ==============================

    3.당신은 실제로 적발되지 않는 오류를 얻고있다. 당신이 스토어드 프로 시저를 호출처럼 당신은 스칼라 UDF를 호출하지 않습니다.

    당신은 실제로 적발되지 않는 오류를 얻고있다. 당신이 스토어드 프로 시저를 호출처럼 당신은 스칼라 UDF를 호출하지 않습니다.

    어느 proc 디렉토리에 저장되어있는 UDF를 포장, 또는 구문을 변경합니다. 사실 확인이 일반적이지 않기 때문에 그게 뭔지 모르겠어요 ...

    아 하 다음 질문을 참조하십시오

  4. from https://stackoverflow.com/questions/6932199/executescalar-always-returns-null-when-calling-a-scalar-valued-function by cc-by-sa and MIT license