Avatar of ScuzzyJo
ScuzzyJo
Flag for United Kingdom of Great Britain and Northern Ireland asked on

SQL Error - can't see anything wrong!

Hi

I hope someone can help.  I'm getting an error on the following code:

SELECT DISTINCT old_ssn
INTO LFRD_Old_SSN_Cnt
FROM LFRD;
--
--
ALTER TABLE LFRD_Old_SSN_Cnt ADD SSN_SSNs nvarchar (255);
GO
--
--
UPDATE A SET SSN_SSNs = B.SSN
FROM LFRD_Old_SSN_Cnt A LEFT OUTER JOIN LFRD B ON A.old_ssn = B.old_ssn;
--
--

The message is:

Msg 102, Level 15, State 1, Line 83
Incorrect syntax near ')'.

Line 83 is just after GO on the ALTER TABLE bit.

I can't for the life of me see what's wrong with the code.  Please help.

Thanks
Sarah
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Russ Suter

I don't think you want to be using a semicolon and then a GO statement. You should choose one or the other.
Jim Horn

Looks correct to me.  Wild guesses..
  • Is the table name spelled correctly?
  • Do you need to add the schema name to the ALTER TABLE?
  • Is the column added already in the table?
  • Might need a GO between the SELECT .. INTO that creates the table and the ALTER TABLE that changes it.
ScuzzyJo

ASKER
Hi Russ

Thanks, but I don't think that's the problem.  I always write it like that and I've never had a problem with it before.

Thanks
Sarah
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ScuzzyJo

ASKER
Hi Jim

It's driving me mad!

The table name must be correct as I copied and pasted it from the line where the table was created to be sure.

I'm not sure why I would need to add the schema name or even what that is!  Is it the bit with dbo in front of it?  I've tried dragging that across but it didn't make any difference.

The table is dropped and is then added as part of the SELECT DISTINCT  bit above.

I tried adding a GO, but it didn't make any difference.

It's very strange and I'm at a complete loss!

Thanks
Sarah
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ScuzzyJo

ASKER
Thank you.  I've found it!  It was bracket in some dynamic SQL on Line 131!!!!!!!

I cut and pasted everything after that line into Notepad and then ran it.  It was fine, so I gradually added bits back in until it fell over again and I found it.

You're right about the error messages not being very helpful.
Jim Horn

Yeah alot of times when you get an ambiguous error message you have to start there and read up to flush out the actual error.

And if you're working in dynamic SQL then you're playing with fire.  It's often helpful to add a line SELECT @sql (or whatever your dynamic SQL variable is) to your script just to review and make sure it is correct.

Thanks for the accept, good luck with your project.  -Jim
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ScuzzyJo

ASKER
Thanks, Jim.  I'm pretty new to dynamic SQL, so there will no doubt be a question on that coming up either this afternoon or maybe in the morning as I'm in the UK, so finish work in about half an hour.
Jim Horn

No prob.  Dynamic SQL is levels harder to build and support then regular SQL, and should be avoided if at all possible.
Vitor Montalvão

I've found it!  It was bracket in some dynamic SQL on Line 131!!!!!!!
Would be possible the error referencing the Line 83 of the dynamic SQL? Or isn't that big dynamic SQL?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ScuzzyJo

ASKER
Hi Vitor

The dynamic SQL bit is tiny and starts way lower down.  Line 83 seems to have nothing to do with that bit.

Thanks
Sarah
Vitor Montalvão

Ok. Just wanted to try to find a logical explanation for the error message.
Looks like you just found another Microsoft "feature" ;)
Cheers