Solved

Why this TSQL syntax error?

Posted on 2011-03-14
4
306 Views
Last Modified: 2012-05-11
Hi, I'm using sql 2005 express.  The sub select query runs fine when I run it by itself.  But I got the following error when I embedded it in this Insert error?  Thanks.

INSERT INTO [MedDb].[dbo].[DegreeMajor]
           ([Major Title])
     VALUES
           (select distinct degree_Major from dbo.PersonDegree pd
                  where degree_Major is not null and degree_Major <> '.' and degree_Major <> '')


Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.
0
Comment
Question by:lapucca
4 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 250 total points
ID: 35131825
INSERT INTO [MedDb].[dbo].[DegreeMajor]
           ([Major Title])
           select distinct degree_Major from dbo.PersonDegree pd
                  where degree_Major is not null and degree_Major <> '.' and degree_Major <> ''


do not use values
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 250 total points
ID: 35131871
try :
INSERT INTO [MedDb].[dbo].[DegreeMajor]
           ([Major Title])
           select distinct degree_Major from dbo.PersonDegree pd
                  where degree_Major is not null and degree_Major <> '.' and degree_Major <> '')

When using a subselect for an insert into you don't use the values keyword.
These are the valid statements :

Insert Into <table name>(<column name>) values(<set of values>)

or

Insert Into <table name>(<column name>) Select <Set of values> or <local variables> From <table name>


0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35131874
0
 

Author Closing Comment

by:lapucca
ID: 35132593
Thank you.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query 14 54
sql query help 4 44
SQL Server CASE .. WHEN .. IN statement - Syntax issue 4 49
Sql query 107 22
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now