硬編碼,這個工程:此SQL服務器查詢是否存在語法錯誤?我可以不使用「目標。@ 1」嗎?
var insertCommand1 = ("MERGE INTO Leaderboard WITH (HOLDLOCK) AS target USING (SELECT * FROM Scores WHERE WeekNumber = 7) AS Source ON (target.id = source.id) WHEN MATCHED THEN UPDATE SET target.Id = source.Id, target.Week7 = source.weeklyScore WHEN NOT MATCHED THEN INSERT (Id, Week7) VALUES (source.Id, source.weeklyScore);");
db.Execute(insertCommand1);
這不起作用:
var insertCommand1 = ("MERGE INTO Leaderboard WITH (HOLDLOCK) AS target USING (SELECT * FROM Scores WHERE WeekNumber = @0) AS Source ON (target.id = source.id) WHEN MATCHED THEN UPDATE SET target.Id = source.Id, [email protected] = source.weeklyScore WHEN NOT MATCHED THEN INSERT (Id, @2) VALUES (source.Id, source.weeklyScore);");
db.Execute(insertCommand1, weeknum, weekstring, weekstring);
錯誤說,有近一個語法錯誤@ 1。這可能是什麼? 我已經調試過,以確保weeknum和weekstring的值是正確的。
在SQL服務器2015年VS
模式工作的2表 -
Leaderboard(Id, Week1, Week2, Week3, Week4, Week5,
Week6, Week7, Week8, Week9, Week10)
with Id as the primary key
Scores(Id, WeekNumber, weeklyScore)
with Id and WeekNumber as the primary key
您可能會問錯的問題[**什麼是XY問題?**](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) 向我們顯示一些數據和預期的輸出以及您想要達到的目標,我們可以爲您提供更多幫助。 –
@JuanCarlosOropeza剛剛發佈了模式 - 希望這有助於! –
Jules你的模型看起來應該正常化,'排行榜'應該是'{Id,Week_id,Score}'然後如果你想顯示幾個星期你使用'PIVOT'這將允許你添加刪除周而不改變你的數據庫設計。 –