1

i have a a table called messages and a table called customer

I want to insert a new message in the message table for every customer ID in the customers table. I wrote the below query

INSERT INTO MESSAGES
(
ID,
VERSION,
CUSTOMERID,
SERVICE,
CREATED_BY,
MESSAGE
)
VALUES
(
NEWID(),
'1',
SELECT id FROM customer,
null,
'test',
'test')

But i get the error

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Any ideas?

1
  • JUST TO ADD that i only need the customer_id from the customer table and no other data Commented Sep 2, 2014 at 7:29

1 Answer 1

2

Not possible, try this:

INSERT INTO MESSAGES
(ID, VERSION, CUSTOMERID, SERVICE, CREATED_BY, MESSAGE)
SELECT NEWID(), '1', id,  null, 'test', 'test'
FROM customer

In this way you can insert in your message table all customer rows

Sign up to request clarification or add additional context in comments.

1 Comment

Fields' number in INSERT and in SELECT must be the same. In that query by CUSTOMER table you'll get only ID field, the others are constants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.