在SQL表中动态使用where子句插入 [英] inserting using where clause in SQL table dynamically

查看:67
本文介绍了在SQL表中动态使用where子句插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由3列(SnoComponentQuantity)组成的表COMPONENTS,其中我最初在其中编写SnoComponent列,并且我想填充列'quantity '使用某些表达式(例如:((d1+d2)*d3)),该表达式涉及来自另一个表 SHEET(d1 int,d2 int ,d3 int,d4 int ,d5 int,d6 int) 的变量.

I am having a table COMPONENTS of 3 columns (Sno,Component,Quantity) among which I am writing Sno and Component columns initially and I want to fill the column 'quantity' using some expression(ex: ((d1+d2)*d3) ) involving variables from another table SHEET(d1 int,d2 int ,d3 int,d4 int ,d5 int,d6 int).

在这里,我需要根据COMPONENTS表(components.sno)中Sno列中的值将值写入数量列.

Here I need to write values to quantity column based on value in Sno column from COMPONENTS table(components.sno).

我曾经将表达式值保留在'x'中,并尝试插入到COMPONENTS中,如下所示:

I used to keep expression value in 'x' and trying to insert into COMPONENTS as shown below:

insert into components(Quantity) values(x) 
where components.sno='y'; [Y is inetger starting from 0 to 70]

但以上查询显示where

请建议我最好的SQL查询来实现这一目标! 在此先感谢..!

Please suggest me best SQL query to achieve this! Thanks in Advance..!

推荐答案

除非使用WHERE NOT EXISTS,否则不能对WHERE子句使用INSERT,所以只需执行以下操作:

You can't do INSERT with WHERE clause unless it's WHERE NOT EXISTS, so just do:

INSERT INTO components(Quantity) VALUES(x)

也许您需要做UPDATE

UPDATE components SET Quantity=x WHERE components.sno='y';

这篇关于在SQL表中动态使用where子句插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆