Solved

Why this TSQL syntax error?

Posted on 2011-03-14
4
305 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

20 Experts available now in Live!

Get 1:1 Help Now