我在实现这一更新该如何操作。请版主帮忙!!!!!

qqyzq 2004-07-23 09:44:54
在SQL数据库(test)中有两个表;
表一(Product):
字段: ProtudtID ProductNAme Grade1 Grade2 Grade3
说明: 商品编号 名称 一等品 二等品 三等品
数据: 1001 商品A 10元 9元 8元
表二(Produce)
字段: Pdate ProduceID ProduceID Grade ProduceLong Amount
说明: 日期 生产单号 商品编号 等级 产量 价值
数据: 04.03.02 0001 1001 一等品 20
那么我现在想实现的更新操作是这样:
在Produce表中进行更新:表中的 价值= 20 * 10 条件首先是Produce表中的商品编号要等于Product表中的商品编号 并且 等级也是的。在上面的数据中商品编号为1001,等级为一等品,所在价值才会等 20 * 10
如果等级为二等品 那么价值就是 20 * 9
请问各位这个更新如何实现。
...全文
118 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqyzq 2004-07-24
  • 打赏
  • 举报
回复
谢谢回复,不好意思还忘了一点,还忘了一条日期。我要更新的数据是我指定时间段。
比如我要更新是 2004.03.01 到 2004.03.10这间的数据。语句还该如何写。
AntingZ 2004-07-24
  • 打赏
  • 举报
回复
用这个看看,报什么错误?

Try
myconn.open()
dff = CmdOLD.ExecuteNonQuery()
myconn.close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
qqyzq 2004-07-24
  • 打赏
  • 举报
回复
这条Sql语句在查询分析器里面执行是对的。没有任何错误提示。就是通过程序来实现在报错。
AntingZ 2004-07-24
  • 打赏
  • 举报
回复
你在 myconn.open()之前用
inputbox("","",Cmd.CommandText)
然后把这个sql拷贝到查询分析器里面看看!
AntingZ 2004-07-24
  • 打赏
  • 举报
回复
update Produce set
Amount=(case grade when '一等品' then a.ProduceLong*b.grade1 when '二等品' then a.ProduceLong*b.grade2 else a.ProduceLong*b.grade3 end)
from Produce a join Product b
on a.productid=b.productid
where a.pdate between '2004-4-1' and '2004-4-10'
qqyzq 2004-07-24
  • 打赏
  • 举报
回复
版主不在吗。这个更新程序在Sql的查询分析器里已经对了,但是在vb.net程序中执行时系统提示有错,系统说我语句不对。请问我在程序中实现这条更新语句该如何写。
我是这样写的,但是有错:
Dim aff As Integer
Cmd.CommandText = "update Produce set " & _
"Amount=(case grade when '一等品' then a.ProduceLong*b.grade1 " & _
"when '二等品' then a.ProduceLong*b.grade2 else a.ProduceLong*b.grade3 end) " & _
"from Produce a join Product b on a.productid=b.productid " & _
"where a.pdate >='2004.04.01' and a.pdate <= '2004.04.10'"
myconn.open()
dff = CmdOLD.ExecuteNonQuery() 影响行数 (一运行时走到这里就出错)
myconn.close()
msgbox("更新成功")


szch 2004-07-24
  • 打赏
  • 举报
回复
where Pdate >='2004.03.01' and Pdate <= '2004.03.10'
qqyzq 2004-07-24
  • 打赏
  • 举报
回复
已经调试好了。语句如下:
Dim fmmsg As New frmStatus()
fmmsg.Show("正在组织 " & Me.Text & " 的数据........")
Me.Cursor = Windows.Forms.Cursors.Default
Application.DoEvents()
Dim mouseCur As Cursor
Me.Cursor = Windows.Forms.Cursors.WaitCursor
Dim aff As Integer
Dim Tim As Integer
Dim Txt As RichTextBox
Try
SqlCmD.CommandText = "update Producetable set " & _
"ProduceAmount=(case grade when '一等品' then a.ProductLong * b.grade1 when '二等品' then a.ProductLong * b.grade2 else a.ProductLong * b.grade3 end) " & _
"from Producetable a join Product b " & _
"on a.productid=b.productid " & _
"where a.producedata between '" & DT1.Value & " ' and '" & DT2.Value & " '"
Myconn.Open()
aff = SqlCmD.ExecuteNonQuery
Tim = SqlCmD.CommandTimeout
Myconn.Close()
Catch es As Exception
MessageBox.Show(es.Message)
End Try
Application.DoEvents()
Me.Cursor = Windows.Forms.Cursors.Default()
fmmsg.Close()
MsgBox("共变动 :" & Str(aff) & "笔数据;" & "共用时: " & Str(Tim) & "秒")
qqyzq 2004-07-24
  • 打赏
  • 举报
回复
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中

其他信息:系统错误。
出错语句是在: dff = CmdOLD.ExecuteNonQuery()

AntingZ 2004-07-23
  • 打赏
  • 举报
回复
CREATE TABLE #Produce (
[Pdate] [datetime] NULL ,
[ProduceID] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[ProductID] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Grade] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Producelong] [int] NULL ,
[Amount] [int] NULL
) ON [PRIMARY]
GO

CREATE TABLE #Product (
[ProductID] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[Productname] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[grade1] [int] NULL ,
[Grade2] [int] NULL ,
[Grade3] [int] NULL
) ON [PRIMARY]
GO

insert into #produce(pDate,ProduceID,productID,Grade,ProduceLong,Amount)
values(getdate(),'001','1001','一等品',20,null)

insert into #product(Productid,productname,grade1,grade2,grade3)
values('1001','产品A',10,9,8)

--before update
print '更新前:'
select * from #produce

update #Produce set
Amount=(case grade when '一等品' then a.ProduceLong*b.grade1 when '二等品' then a.ProduceLong*b.grade2 else a.ProduceLong*b.grade3 end)
from #Produce a join #Product b
on a.productid=b.productid

print '更新后:'
select * from #produce

--删除临时表
drop table #produce
drop table #product

/* 执行结果
所影响的行数为 1 行)


(所影响的行数为 1 行)

更新前:
Pdate ProduceID ProductID Grade Producelong Amount
------------------------------------------------------ ---------- ---------- ---------- ----------- -----------
2004-07-23 23:00:19.327 001 1001 一等品 20 NULL

(所影响的行数为 1 行)


(所影响的行数为 1 行)

更新后:
Pdate ProduceID ProductID Grade Producelong Amount
------------------------------------------------------ ---------- ---------- ---------- ----------- -----------
2004-07-23 23:00:19.327 001 1001 一等品 20 200
(所影响的行数为 1 行)
*/
AntingZ 2004-07-23
  • 打赏
  • 举报
回复
update Produce set
Amount=(case grade when '一等品' then a.ProduceLong*b.grade1 when '二等品' then a.ProduceLong*b.grade2 else a.ProduceLong*b.grade3 end)
from Produce a join Product b
on a.productid=b.productid
AntingZ 2004-07-23
  • 打赏
  • 举报
回复
你的表字段估计写错了,下面用中文,你根据对应换成实际的英文字段名:

update Produce set 价值=
(case 等级 when '一等品' then 20*10 when '二等品' then 20*9 else 0 end)
from Produce a join Product b
on a.商品编号=b.商品编号

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧