Reputation: 119
So I'm trying to insert a small batch of rows (<10) at once into a table. The data is in a table-valued parameter, but I need to add some extra columns to it that are common to all the rows before inserting it. I've tried two ways of doing this:
INSERT INTO endTable
([col1],[col2],[commoncol1],[commoncol2])
SELECT
t.col1, t.col2, @CommonValue1, @CommonValue2
FROM startTable t
I've also tried to do it by putting the common variables into a temporary table variable, and then cross joining it with startTable, like this:
DECLARE @tempTable TABLE([temp1],[temp2])
INSERT INTO @tempTable(temp1,temp2) VALUES (@CommonValue1, @CommonValue2)
INSERT INTO endTable
([col1],[col2],[commoncol1],[commoncol2])
SELECT
a.col1, a.col2, b.temp1, b.temp2
FROM startTable a CROSS JOIN @tempTable b
Both of these attempts give the same error if and only if startTable has multiple rows:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I was hoping to accomplish these inserts without resorting to a loop, but I'm not incredibly familiar with SQL, so I don't know if I'm out of luck, or if I'm missing something obvious. Any help would be greatly appreciated.
Upvotes: 0
Views: 127
Reputation: 13419
Since your INSERT
statement does not have a subquery, the error message doesn't make sense. My guess is that you have a trigger on endTable
that is throwing the error.
Run the following query to see if you have any triggers on endTable
. If you do, then you will need to inspect the SQL in the trigger to see why it is throwing the error.
SELECT *
FROM sys.triggers
WHERE parent_id = OBJECT_ID(N'endTable')
Upvotes: 2