Monday, March 26, 2012

Query Problem

This has to be obvious but I can't see the problem. I am executing the
query:
=======================================
Insert Into TempTable (email) Values (Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
'A', 'AAA', 'AAAAA'))
=======================================
But I get a syntax error "near Select". Can someone straighten me out.
Thanks
WayneTry this
Insert Into TempTable (email) (Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass
In (
'A', 'AAA', 'AAAAA'))
Madhivanan|||Take out the VALUES keyword. You do not, and actually cannot, include that
keyword when performing an INSERT ... SELECT.
Richard
"Wayne Wengert" wrote:

> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>
>|||This will do
Insert Into TempTable (email)
(Select Distinct N1.Email From Names N1
Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
'A', 'AAA', 'AAAAA'))
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Wayne Wengert" <wayneDONTWANTSPAM@.wengert.com> wrote in message
news:u8lSldZHFHA.1176@.TK2MSFTNGP12.phx.gbl...
> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
> N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>|||You can't have a SELECT statement in the values clause.
Is this statement intended to insert one row or multiple rows.
Madhivanan's solution works for 1 row. The following will insert as
many rows as returned by the query:
INSERT INTO TempTable (email)
SELECT DISTINCT N1.Email
FROM Names N1
INNER JOIN Bands B1
ON B1.BandDirector = N1.NameID
WHERE B1.BandClass In ('A', 'AAA', 'AAAAA')
David Portas
SQL Server MVP
--|||Thanks all. I didn't realize that you could not use "Values" in that case.
Wayne
"Wayne Wengert" <wayneDONTWANTSPAM@.wengert.com> wrote in message
news:u8lSldZHFHA.1176@.TK2MSFTNGP12.phx.gbl...
> This has to be obvious but I can't see the problem. I am executing the
> query:
> =======================================
> Insert Into TempTable (email) Values (Select Distinct N1.Email From Names
N1
> Inner Join Bands B1 On B1.BandDirector = N1.NameID Where B1.BandClass In (
> 'A', 'AAA', 'AAAAA'))
> =======================================
>
> But I get a syntax error "near Select". Can someone straighten me out.
>
> Thanks
>
> Wayne
>

No comments:

Post a Comment