Home > Database > Mysql Tutorial > body text

Why does my query \'SELECT INTO\' result in an \'Undeclared Variable\' error?

Linda Hamilton
Release: 2024-10-30 00:04:02
Original
485 people have browsed it

Why does my query

SELECT INTO and Error: Undeclared Variable

When attempting to execute the query:

SELECT id_subscriber
INTO newsletter_to_send
FROM subscribers
Copy after login

You encounter an error:

#1327 - Undeclared variable: newsletter_to_send
Copy after login

Cause:

The error arises because the variable newsletter_to_send is not declared or initialized before its use in the query.

Solution:

To resolve this issue, you need to use the INSERT ... SELECT syntax instead of the SELECT INTO syntax. The correct query should be:

INSERT INTO newsletter_to_send
SELECT id_subscriber
FROM subscribers
Copy after login

The INSERT ... SELECT syntax allows you to insert multiple rows into a table by using the results of a subquery.

Additional Note:

It's worth considering if there is a need for a WHERE clause in the subquery to filter the results and potentially prevent inserting unwanted rows into the newsletter_to_send table.

The above is the detailed content of Why does my query \'SELECT INTO\' result in an \'Undeclared Variable\' error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!