执行插入查询时出错

Error when executing insertion query

插入前如何避免重复数据。我使用了以下代码,但出现此错误。

No column name was specified for column 1 of 'temp'.

No column name was specified for column 2 of 'temp'.

No column name was specified for column 3 of 'temp'.

No column name was specified for column 4 of 'temp'.

No column name was specified for column 5 of 'temp'.

No column name was specified for column 6 of 'temp'.

No column name was specified for column 8 of 'temp'.

No column name was specified for column 9 of 'temp'.

Invalid column name '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();
                }

            }

我承认没有测试过这个,但是给出错误信息,子查询:

SELECT 
    @datedif,
    @batch,
    -- and so on
    @weekMonth
) AS temp

需要定义列名,因为值来自变量。

那就是:

SELECT 
    @datedif as datedif,
    @batch as batch,
    -- and so on
    @weekMonth as weekMonth
) AS temp

您收到的错误说明了一切。这是因为您的内部 select table temp 需要定义所有列名。同样在您的 WHERE NOT EXISTS (SELECT datelist 中,您正在使用您的 table 名称 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} 格式编写您的查询,如下所示。如果您需要在数据库中已存在记录时捕获,可以添加 ELSERAISERROR / 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