如何使用数组在 table 中插入多行
How Can Insert Multi Rows in a table using array
我需要使用此代码
在我的 sql 服务器 Table 中同时添加多行
declare @idproduct int
declare @idfile int
set @idproduct = (select id from Products where name = 'DR-8416')
set @idfile = (select id from Files where filename like '%8416%')
insert into ProductsFiles(idproducts, idfile) values (@idproduct, @idfile)
@idfile 是一个有很多值的数组;
当我尝试添加时收到此错误
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
我该如何解决这个问题?
我能猜到的最好的是你想要的是:
INSERT INTO ProductsFiles (idproducts,
idfile)
SELECT P.id,
F.id
FROM Products AS P
CROSS JOIN Files AS F
WHERE P.[name] = 'DR-8416'
AND F.[filename] LIKE '%8416%';
注意我使用了 CROSS JOIN
,因为你的问题表明 Products
和 Files
之间没有关系。如果有将 CROSS JOIN
更改为 INNER JOIN
并添加相关的 ON
子句。如果您不知道 JOIN
语法,我建议您查找并学习它。 JOIN
语法是 SQL 的基础之一,不学习它就不会走得太远。花时间阅读它比我给你一个答案并尝试解释基础知识你会学到更多。
我需要使用此代码
在我的 sql 服务器 Table 中同时添加多行declare @idproduct int
declare @idfile int
set @idproduct = (select id from Products where name = 'DR-8416')
set @idfile = (select id from Files where filename like '%8416%')
insert into ProductsFiles(idproducts, idfile) values (@idproduct, @idfile)
@idfile 是一个有很多值的数组; 当我尝试添加时收到此错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我该如何解决这个问题?
我能猜到的最好的是你想要的是:
INSERT INTO ProductsFiles (idproducts,
idfile)
SELECT P.id,
F.id
FROM Products AS P
CROSS JOIN Files AS F
WHERE P.[name] = 'DR-8416'
AND F.[filename] LIKE '%8416%';
注意我使用了 CROSS JOIN
,因为你的问题表明 Products
和 Files
之间没有关系。如果有将 CROSS JOIN
更改为 INNER JOIN
并添加相关的 ON
子句。如果您不知道 JOIN
语法,我建议您查找并学习它。 JOIN
语法是 SQL 的基础之一,不学习它就不会走得太远。花时间阅读它比我给你一个答案并尝试解释基础知识你会学到更多。