列名称“allocationStart”无效

时间:2012-01-20 14:35:19

标签: asp.net sql

无论出于何种原因,我在尝试执行查询时收到错误“无效的列名allocationStart”。这在dateadd函数内,并且数据库中存在列DOES。它是datetime

以下是查询:

cmd.commandText = "Insert Into EmpPac 
                     (catalogIdent, empPacIdent, empIdent, allocation, 
                      quantityLimit, quantityIssued, quantityShipped, 
                      allocationMonths, sizeChartIdent, sizeNotes, nextUpdate)
                     values ( '" & catalogIdent & "', '" & intvalue_EmpPak 
                      & "', '" & empIdent & "',"&jobQuantityLimit&",'"
                      &jobQuantityLimit&"', '0', '0',"&
                      allocationMonths&", '"& sizeChartident & 
                      "', '', DATEADD(month, "&allocationMonths&
                      ", allocationStart))"
cmd.execute

2 个答案:

答案 0 :(得分:2)

您没有指定allocationstart所在的表格。

您需要从源表中将INSERT源设为SELECT,而不是使用VALUES关键字,因为它需要显式值列表。

例如:

INSERT INTO MyTargetTable
SELECT <stuff>, DATEADD(month, XXX, allocationstart)
FROM MySourceDataTable

答案 1 :(得分:0)

我说实话 - 我没有100%关注你的问题,但我认为我理解你要做的事情。

原始错误源自DATEADD命令。

DATEADD的第三个参数是要操作的日期时间值。

我猜你的&#34; allocationStart&#34; column在DB中自动设置当前日期时间,并将该列传递给DATEADD函数以对其进行操作并将返回值插入&#34; nextUpdate&#34;柱。如果您正在尝试这样做,则不会设置自动日期时间列(能够返回日期/时间等)直到提交行,因此您无法在函数中使用auto = generated列在INSERT语句中。

我还强烈建议你看看参数,它更安全,更好的做法。

如果您在&#34; nextUpdate&#34;的月份编号之后?在列中,您可以使用DateTime.Now.Month并将其附加到SQL字符串。

相关问题