VB.Net从一个表插入/选择另一个错误

时间:2016-10-31 15:06:34

标签: sql vb.net parameters

我试图从tbl1中选择*并将其插入到tbl1_DEL中。基本上如果用户想要删除一条记录,我会先删除实际记录,然后再错误地将其插入到_DEL表中。到目前为止,这是我的代码......

    queryD = "Insert Into tbl1_DEL "
    queryD = queryD & " SELECT * from Tbl1 WHERE IdClient = @IdClient"

    cmd.CommandText = queryD
    cmd.Connection = Conn
    Conn.Open()

    cmd.Parameters.Add("@IDClient", SqlDbType.NChar).Value = 17 'Just testing here with hardcoded IDClient

    rowAffected = cmd.ExecuteNonQuery()

我想知道我怎么能够继续这个并且基本上做类似......

    if rowAffected <>0 then 
    Delete * from Tbl1 where IDClient = 17 'parametized as it is with the Insert/Select above
    End if

我想知道它是否可以与参数有关,因为该字段的数据类型是数字(18,0)。

这是我一直在犯的错误......

  

列名或提供的值数与表不匹配   定义

1 个答案:

答案 0 :(得分:2)

我怀疑tbl1_DEL和Tbl1的结构不同。

同步结构或指定字段列表(推荐)

Insert into tbl1_DEL (Fld1,Fld2...) 
 Select Fld1,Fld2...
  From  Tbl1 
  Where dClient = @IdClient

根据你的评论

 Insert into tbl1_DEL
     Select *,TheOtherField = GetDate()    -- Assuming the one extra field is the last
      From  Tbl1 
      Where dClient = @IdClient
相关问题