如何在插入前避免重复数据。我使用下面的代码但是我收到了这个错误。
没有为'temp'的第1列指定列名。
没有为'temp'的第2列指定列名。
没有为'temp'的第3列指定列名。
没有为'temp'的第4列指定列名。
没有为'temp'的第5列指定列名。
没有为'temp'的第6列指定列名。
没有为'temp'的第8列指定列名。
没有为'temp'的第9列指定列名。
无效的列名'datelist'。
using (SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
var fromdate = DateTime.Parse(txtfromdate.Text);
var todate = DateTime.Parse(txttodate.Text);
var datedif1 = (todate - fromdate).Days;
var sqlInsert = new SqlCommand("INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth]) SELECT * from (SELECT @datedif,@batch,@daywk,@semester,@weekbatch,@subject,@facultyname,@facultyid,@weekMonth) AS temp WHERE NOT EXISTS (SELECT datelist,batch from datelist where batch=@batch and datedif=@datedif)", con2);
var sqlParamater = sqlInsert.Parameters.Add("@datedif", SqlDbType.Date);
var sqlParameter1 = sqlInsert.Parameters.Add("@batch", SqlDbType.NVarChar);
var sqlParameter2 = sqlInsert.Parameters.Add("@daywk", SqlDbType.NVarChar);
var sqlParameter3 = sqlInsert.Parameters.Add("@semester", SqlDbType.NVarChar);
var sqlParameter4 = sqlInsert.Parameters.Add("@weekbatch", SqlDbType.NVarChar);
var sqlParameter5 = sqlInsert.Parameters.Add("@subject", SqlDbType.NVarChar);
var sqlParameter6 = sqlInsert.Parameters.Add("@facultyname", SqlDbType.NVarChar);
var sqlParameter7 = sqlInsert.Parameters.Add("@facultyid", SqlDbType.NVarChar);
var sqlParameter8 = sqlInsert.Parameters.Add("@WeekMonth", SqlDbType.NVarChar);
con2.Open();
// try
//{
for (var i = 0; i <= datedif1; i++)
{
var consecutiveDate = fromdate.AddDays(i);
sqlParamater.Value = consecutiveDate;
sqlParameter1.Value = batch1;
sqlParameter2.Value = dayweek;
sqlParameter3.Value = semester;
sqlParameter4.Value = weekbatch;
sqlParameter5.Value = subject;
sqlParameter6.Value = faculty;
sqlParameter7.Value = facultyid;
sqlParameter8.Value = weekmonth;
int s = sqlInsert.ExecuteNonQuery();
}
}
答案 0 :(得分:2)
我承认没有测试过这个,但是给出了错误消息,子查询:
SELECT
@datedif,
@batch,
-- and so on
@weekMonth
) AS temp
需要定义列名,因为值来自变量。
那将是:
SELECT
@datedif as datedif,
@batch as batch,
-- and so on
@weekMonth as weekMonth
) AS temp
答案 1 :(得分:1)
你得到的错误说明了一切。它是因为您的内部选择表temp需要定义所有列名。同样在WHERE NOT EXISTS (SELECT datelist
中,您使用的是您的表名datelist
作为列名。
要修复你的错误,请写这样的sql。
INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth])
SELECT * FROM
(
SELECT @datedif as datedif,@batch as batch,@daywk AS batch,@semester AS semester,@weekbatch AS weekbatch,@subject AS subject,@facultyname AS facultyname,@facultyid AS facultyid,@weekMonth AS weekMonth
) AS temp
WHERE NOT EXISTS (SELECT * from datelist where batch=@batch and datedif=@datedif);
您可以选择以IF NOT EXISTS(SELECT){Insert}
格式编写查询,如下所示。如果您需要捕获数据库中已存在记录的时间,则可以添加ELSE
/ RAISERROR
THROW
IF NOT EXISTS(SELECT * from datelist where batch=@batch and datedif=@datedif )
BEGIN
INSERT INTO datelist ([datedif],[batch],[daywk],[semester],[weekbatch],[subject],[facultyname],[facultyid],[WeekMonth])
VALUES (@datedif,@batch,@daywk,@semester,@weekbatch,@subject,@facultyname,@facultyid,@weekMonth)
END
ELSE
BEGIN
RAISERROR('Record already exists in the table',16,1);
END