我的代码不起作用。我尝试了一些不同的东西,但不知道从哪里开始。我在以下查询中添加了从null开始的值。运行我的代码后,分数值仍为空。
Answers数据库和WeeklyPredictions数据库都有几个属性可用于解决其他问题。这只是使用第一个问题1的代码片段。
var Q1 = "";
var weeknumber = "";
if (IsPost)
{
if (Request.Form["weeknumber"] != null)
{
weeknumber = Request.Form["weeknumber"];
}
if (Request.Form["Question1"] != null)
{
string selectedAnswer = Request.Form["Question1"].ToString();
Q1 = Request.Form["Question1"];
}
var db = Database.Open("foo");
var q1w = ("UPDATE t1 " +
"SET t1.weeklyScore = t1.weeklyScore + 10, t1.totalScore = t1.totalScore + 10 " +
"FROM dbo.Scores as t1 " +
"INNER JOIN dbo.WeeklyPredictions AS t2 " +
"ON t1.Id = t2.Id " +
"WHERE t2.Question1 = @0 " +
"AND t2.WeekNumber = @1");
db.Execute(q1w, Q1, weeknumber);
答案 0 :(得分:2)
在SQL NULL + 10
中仍然是NULL
,请更改您的代码以使用ISNULL(col, 0) + 10
https://technet.microsoft.com/en-us/library/ms191504(v=sql.105).aspx
答案 1 :(得分:0)
假设这是SQL Server,我会将其写为:
UPDATE s
SET weeklyScore = COALESCE(s.weeklyScore, 0) + 10,
totalScore = COALESCE(s.totalScore, 0) + 10
FROM dbo.Scores s INNER JOIN
dbo.WeeklyPredictions wp
ON s.Id = wp.Id
WHERE wp.Question1 = @0 AND wp.WeekNumber = @1;