내가 가진 저장 프로 시저의 출력 매개 변수에 간헐 null 값이 나타납니다. 저장 프로 시저 내부의 NOLOCK과 관련이 있는지 궁금합니다. 그것은 대부분의 시간 동안 작동하지만 간헐적으로 실패합니다. 특히 높은 부하 상태. 대부분의 시간은 예상 할 "y"또는 "n"을 반환합니다. 여기 높은 트래픽 SQL 테이블 간헐 Null 출력 매개 변수
SqlConnection con = getCon();
SqlCommand cmd = new SqlCommand("loginRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@username", username));
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@exists", System.Data.SqlDbType.VarChar, 3, System.Data.ParameterDirection.Output, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Util.sendErrorEmail(ex.ToString());
}
finally
{
con.Close();
}
//The following line is the one that throws an "Object reference not set to an instance of an bject." exception
string userExists = cmd.Parameters["@exists"].Value.ToString();
저장된 프로 시저이다
ALTER PROCEDURE [dbo].[loginRecord]
(
@username nvarchar(100),
@exists char(1) OUTPUT
)
AS
IF EXISTS(select username from Users WITH (NOLOCK) where username = @username)
BEGIN
set @exists='y'
END
ELSE
BEGIN
set @exists='n'
--insert user account--
insert into Users (username, datejoined)
values (@username, getdate())
END
insert into Logins (username, logged)
values (@username, getdate())
GO
실패 (null 반환)하면 반환 할 항목은 무엇입니까? Y 또는 N 상황 또는 둘 다에 항상 실패합니까? –
저장 프로 시저를 보면 if/else가 있는데 값이 무엇이든 관계없이 설정해야하는 것처럼 보입니다. 해당 사용자가 아직 존재하지 않으면 Users 테이블에 삽입해야합니다. – Robert
예,하지만 문제가 하나 또는 다른 상황에서만 발생하는지 확인하려고합니다. 하나 또는 다른 상황에서만 발생하는 경우 해결하는 것이 더 간단한 문제입니다. –