実行しようとしているコードは次のとおりです。
const string Script = @"
DECLARE @AccountKey Int
SELECT @AccountKey = AccountKey FROM dbo.CREAccount WHERE AccountId = @AccountId
INSERT dbo.CREException (CreatedOn, Message, Source, StackTrace, AccountKey, Category, Priority)
VALUES(GETUTCDATE(), @Message, @Source, @StackTrace, @AccountKey, @Category, @Priority)";
using (var command = new SqlCommand(Script, connection))
{
var message = ex.Message;
if (message.Length > 250) message = message.Substring(0, 250);
command.Parameters.Add(new SqlParameter("@Message", message));
command.Parameters.Add(new SqlParameter("@Source", ex.Source ?? string.Empty));
command.Parameters.Add(new SqlParameter("@StackTrace", ex.StackTrace ?? string.Empty));
command.Parameters.Add(new SqlParameter("@AccountId", accountId));
command.Parameters.Add(new SqlParameter("@Category", category));
command.Parameters.Add(new SqlParameter("@Priority", priority));
command.ExecuteNonQuery();
}
予想どおり - @AccountKey がコードで指定されていないため失敗します - これは T_SQL 自体のパラメーターです。パラメータを使用しながら目的を達成するにはどうすればよいですか? accountId
パラメーターは次のようになりますnull
-それが、ルックアップを分解して2つのクエリに挿入する理由です