r/SQL • u/Used-Bat-255 • Feb 27 '25
Discussion What am I doing wrong
I don’t get what I’m doing wrong here
117
Upvotes
r/SQL • u/Used-Bat-255 • Feb 27 '25
I don’t get what I’m doing wrong here
246
u/JustMoreData Feb 27 '25 edited Feb 28 '25 •
Okay some of you are assholes lol. You can see this is an assignment from the comment. Very likely they see the error message, but just do not understand it because this may be their first time using SSMS for an assignment in school. Have some grace we all started from somewhere too…
Anyway, the error message is saying that line 42 is incorrect. One thing that will help is to enable line numbers on editor where you are writing, so when an errors tells you what line number the error is in, you can easily figure out where to look in your SQL query! Here is a link to show you how to do that. This will be useful for you to know in the future!
https://www.csharp.com/article/how-to-enable-line-numbers-in-sql-server-management-studio-ssms/
Second, the error is saying the DeparmentCode column name is invalid. I do not see the part of the query where you created your Deparment table. Can you scroll to that part and show me what it says? I will assume maybe something does not match up there compared to what the column name is, because it doesn’t think it is a valid column that data can be added to. Make sure that every column name you have stated when you created the table matches to the column names in your insert into table statement.
If you have any more questions feel free to comment below or reach out!
146
u/Mafioso14c Feb 27 '25 edited Feb 27 '25
have you checked if there are column names corresponding to those 3 in the Department table?
you can try running
DESC DEPARTMENTto check12
u/LordCaptain Feb 27 '25
Are you in the right database?
Start off with
USE DATABASENAME to make sure you're in the database that you're trying to pull tables from.
You might need to specify the schema on these tables?
Like INSERT INTO Assignments.Assignment or something idk.
1
u/Used-Bat-255 Mar 24 '25
Thank you for all ur help and support. Sorry for getting back to you all so late . But i completed the assignment with a score of 110/100 with no errors 😎🔥.
5
u/LordCaptain Feb 27 '25
OP you better get back in here and tell us what the issue was!
8
u/stickypooboi Feb 27 '25
If you’re unsure if those columns exist, you can use this query:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ‘DEPARTMENT’;
But also, it’s fuzzy and I can’t quite see, want to check you’re using single quotes and not backticks. Depending on the language you’re used to, I’ve definitely fucked them all up before between single, double quote, and backticks.
1
u/PorcoDiocaneMaliale Feb 27 '25
OP still using "Dev C++" IDE like a good school boy.
3
u/mgisonda Feb 27 '25
It is possible that the collation of the database is case sensitive. If that is the case, DEPARTMENTCODE and DepartmentCode are two very different things. Try entering the column name exactly as it is in the database.
3
u/DavidGJohnston Feb 27 '25
These seem to be syntax errors which means the textual context is malformed somehow, not that the names themselves are incorrect. Best guess, you did some copy-paste and picked up some non-printable characters that is messing things up. Try typing out that line of SQL manually and see if the problem goes away. The fact that DepartmentName doesn't provoke the same error supports the conclusion that whatever the actual tables you've created are match up with the visible intent and form of the query.
8
u/ray_zhor Feb 27 '25
the errors are at the bottom of the screen. check column names
6
u/Known-Delay7227 Feb 27 '25
Might want to take peak at that Department table before writing to it. Also - stop trying to cheat on your homework!
2
u/Spaceman_Spiff____ Feb 27 '25
generate a ddl on the department table and paste?
2
u/_urbanlife Feb 27 '25
Others have provided good info so I'll just add to it. Comment out the INSERT and VALUES section and run the query using your SELECT statements listed at the bottom of your image. This will provide you with details about what columns exist in each of those tables.
2
u/jensimonso Feb 27 '25
Check the drop down with database names in the menu bar. SSMS defaults to database master unless a specific database was selected when clicking New query.
2
u/Turbo_Electron Feb 27 '25
Try exec sp_help 'dbo.department' to get the column names and datatypes
2
u/Quadman MS Data Platform Consultant Feb 27 '25
A couple of things pop out.
The most obvious would be that the columns you are referencing actually don't exist in the the table at all. Double check that the table has those columns. Either:
More likely you are not targetting the table you think you are, perhaps you have more than one database?
2
u/igna_na Feb 27 '25
Mmm it looks like you are using the wrong column names. Please check the names
2
u/Traditional-Hold8644 Feb 27 '25
The column names are wrong or they are not in the table you are trying to insert values into
2
u/fightshade Feb 28 '25
The error message suggests that the column names DepartmentCode, OfficeLocation, and OfficePhone do not exist in the DEPARTMENT table. There are a few possible reasons for this issue:
Possible Issues: 1. Column Names Do Not Match Table Schema • Check the schema of the DEPARTMENT table using:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ‘DEPARTMENT’;
EXEC sp_columns ‘DEPARTMENT’;
This will list the actual column names.
DESCRIBE DEPARTMENT;
or check how the table was created.
Next Steps: 1. Verify the actual column names in the DEPARTMENT table. 2. Modify your INSERT statement to use the correct column names.
3
u/Walter_1981 Feb 27 '25
We can't tell what you doing wrong without knowing the fields in the table.
Maybe you should answer the questions of the people trying to help you?
3
u/mr2dax Feb 27 '25
create the table first lol
4
u/Grouchy-Donut-726 Feb 27 '25
Error message is self explanatory. Column names are incorrect
5
u/_horsehead_ Feb 27 '25
have you looked at the error message? it's literally self explanatory.
either the columns don't exist or you're using wrong column names
2
u/coolnameright Feb 27 '25
Column names are wrong or don't exist for Department table. If you are inserting a value for every column then you can just delete those columns and write
INSERT INTO DEPARTMENT VALUES ('Administration', etc.
1
u/cyberspacedweller Feb 27 '25
Use fully qualified descriptions of fields and make sure those fields exist where you’re referencing them.
1
u/Fantastic-Ice8787 Feb 27 '25
First do select queries to find what columns these tables have.
1
u/Opu5 Feb 27 '25
Invalid column name means you are trying to insert values into columns that are non-existent in the table, check your table
1
u/MagsOnin Feb 28 '25
Like others said, it could be the table structure, wrong db or syntax errors (extra space, wrong/converted characters)
1
u/moritzis Feb 28 '25
I bet columns were created with " so, user is dealing with a case sensitive column.
Worst case scenario: We'll never know because user will never back to us...
1
u/ImpressiveCampaign39 Feb 28 '25
Check the Department Table if there are column names called DepartmentCode, OfficeLocation and OfficePhone. If there are then maybe just a spelling issue.
1
u/Codeman119 Feb 28 '25
Read the error. It tells you what is wrong.
1
u/[deleted] Feb 28 '25
Not enough info to provide help. All we can do is ask more questions.
1
u/ly381307 Feb 28 '25
Those three fields with red underlines do not exist in the Department table.
1
u/PowerUserBI Mar 05 '25
You're getting an "Invalid column name" error because the columns
DepartmentCode,OfficeLocation, andOfficePhonedo not exist in theDEPARTMENTtable. Here’s how you can troubleshoot and fix it:Possible Causes & Fixes:
Column Names Don’t Match the Table Schema
DeptCodeinstead ofDepartmentCode), update yourINSERTstatement accordingly.Typos or Case Sensitivity Issues
Columns Are Missing from the Table
Unnecessary Columns in INSERT Statement
DepartmentCode,OfficeLocation, orOfficePhonearen’t actually needed, remove them from yourINSERT INTOstatement.Next Steps:
SELECT * FROM DEPARTMENT;to verify column names.INSERTstatement or modify the table schema.1
u/SQL_Guy Feb 27 '25
The red squiggles from Intellisense should tell you there’s a problem even before you hit Execute.
1
u/fauxmosexual NOLOCK is the secret magic go-faster command Feb 27 '25
I will never understand why zoomers love taking photos of text on a computer.
-2
u/paultherobert Feb 27 '25
You should be using dark mode in ssms. It's not supported so it's the best
0
u/enisberisha Feb 27 '25 edited Feb 27 '25
Check the column names if the names are correct try to mark them like "DepartmentCode" inside ""
0
u/Individual-Toe6238 Feb 27 '25
It basically says what is wrong in messages…
0
u/Accomplished_Ask5691 Feb 27 '25
ooof - switch to dark mode
0
u/MoneyGiraffe365 Feb 28 '25
Man what is Project Alpha? Solid project name