I need to add multirows at the same time in my sql server Table using this code
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)
that @idfile is an array with many values; when I try to add I recied this error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
How Can I solve this Problem ?
INSERThere? A cartesian product of everyidfromFilesandProducts(that meet the requisite of theWHERE)?WHILEloop, that is awfully slow. SQL Server excels as Set Based methods, not iterative ones. I suggest against using that methodology Farshad Razaghi.FilesandProducts? Don't forget, we can't see your data, and we don't have any samples or expected results. Best I can suggest at the moment is you wantINSERT INTO...SELECTsyntax.