SQL server query to add column with default value - sql

I'm trying to change my SQL server database by adding another column to a table with 0 as a default value.
I thought this script worked in the past (for another table), but now I have an error when I try to execute the script
The script
ALTER TABLE Whatever
ADD WhateverColumn tinyint NOT NULL
DEFAULT(0)
The errors
On hovering mouse over "NOT": Incorrect syntax near 'NOT'. Expecting 'TO'
On hovering mouse over "0": Incorrect syntax near 0. Expecting '(', or SELECT
Anyone knows what's wrong with this?

Try this:
ALTER TABLE Whatever
ADD WhateverColumn tinyint NOT NULL DEFAULT 0

Maybe the "Whatever" you are using as the table name has unclosed quotation marks, or even the "WhateverColumn" (both that you place here as a token, i get it) my have this problem, or even the "WhateverColumn" actual name is a reserved word?

#SammuelMiranda has just asked the same just now. It matters if you are using reserved keyword as table or column name also.
you can check this link
Add a column with a default value to an existing table in SQL Server

As I expected, updating my SQL Server Management Studio to version 17.8.1 solved my issue.
After updating, I was able to run this command without any problem.

Related

How would I fix these "ORA-00933: SQL command not properly ended" "ORA-00923: FROM keyword not found where expected" errors?

This Statement:
SELECT id, units, cost FROM inventory_list WHERE cost <= 20;
Gives me:
ORA-00923: FROM keyword not found where expected
While this statement:
SELECT * FROM items WHERE ilt_id = 'il010230126' OR ilt_id = 'il010230128';
Gives me:
ORA-00933: SQL command not properly ended
Not sure about this and may be version dependent (below link is for oracle 10g... but you can see on this site
https://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm
That cost is an oracle reserved keyword, so it is not wise to use it as a column name.
If you have no control of the table I think you may be able to surround it in double quotes eg select "COST" to avoid oracle picking it up as a reserved word.
By default Oracle creates fields in uppercase so the field name will need to be in uppercase unless when the table was created it was forced into different case by surrounding it in Quotes.
Check that you don't have invisible characters in your file and that you're using the right encoding. I sometimes accidentally introduce them because I have a non english keyboard map and accidentally hit the wrong key combination.
Just type again one of your SQL statements and test them.

VBA Update statement not liking column name

This is minor issue that could be rectified change the column name, but I'm wondering why I am getting a syntax error for my update statement in VBA when I try to update the column "size" in my sql database. The statement works fine if I change the column name to anything else like "sizes" or "sizing", but it gives me a syntax error if I change the column name back to "size". Why is that?
Could you provide the statement?
Also, wrap your column names in square brackets. I think in general is a good practice.
UPDATE [dbo].[TableName]
SET[ColumnName] = Value

Can't update data in NVarChar column of a table in SSMS

I have a table in an SSMS database:
I am trying to update the contents of the "Name" column by removing the leading spaces from every entry. Following the question How to delete leading empty space in a SQL Database Table using MS SQL Server Managment Studio, I am therefore trying to run the following:
UPDATE ReferenceHierarchy set Name = LTRIM(Name)
The problem is that when I try to run it, it says "Name" is an invalid column. When I look at the code completion options for "Name", it sees the three fields ID, ParentID, and Sequence. Interestingly, these are the three non-NVarChar fields.
What could be the problem? And how can I fix it?
LTRIM only removes the leading NORMAL Spaces. In your case it may be the Tab space.
Try this for TAB SPACE
UPDATE ReferenceHierarchy set Name = LTRIM(REPLACE(Name,(CHAR(9)),''))
If your space character not a TAB Space and Normal space then it might by non Unicode character
Then Try this
UPDATE REFERENCEHIERARCHY SET NAME = TRIM(LTRIM(CASE WHEN NAME NOT LIKE '[A-ZA-Z0-9]%'
THEN STUFF(NAME, 1, 1, ' ')ELSE NAME END))
TT's comment was the key to solving this one. I actually think there is an interesting moral here: if things are not working the way you think they should, perhaps your enviornment is not what you think it is. What was actually happening was that I had in SSMS an old version of the same database in which the ReferenceHierarchy table did not have a "name" column. And without realizing it, I was running my query against that version. Running it against the correct version of the database solved my problem.

Trying to create a new column, get #1064 error

I've been using phpMyAdmin to manage my db without any problem, but today I ran into this error if I try to add any column by using the interface to any table of any database:
ALTER TABLE `testing` ADD `faaa` INT NOT NULL AFTER ;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
But if I add the column via SQL command in phpMyAdmin, this time by removing AFTER, the column is added without any problem.
I'm still inexperience with phpMyAdmin, so I guess I must have missed a mandatory field to fill when creating a new column in the interface. Can anyone shed a light on this for me?
AFTER column_name is used to designate which column in the table you want to insert the new column after. You're providing the AFTER without telling it which column you want the new column to be inserted behind. If you don't care about the order of the columns in your table, omit the AFTER, and the new column will be inserted at the end of the column list.
You have no column name after the AFTER statement, so the phpMyAdmin doesn't know where it should be put. Whether it's you forgetting to select the column or a phpMyAdmin bug, I have no idea because for adding a new column, the only required fields are the name and type, which you have.

copy one field from table to another field in the same table

I used this query to copy one full column from the same table:
UPDATE 'content_type_chapter'
SET 'field_chapternumbersort2_value' = 'field_chapternumbersort_value'
But I have received this error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''content_type_chapter' SET 'field_chapternumbersort2_value'='field_chapternumber' at line 1
What could be wrong, I'm unable to get it right.
Single-quotes are for strings.
Try backticks instead, e.g.:
UPDATE
`content_type_chapter`
SET
`field_chapternumbersort2_value` = `field_chapternumbersort_value`
The backticks aren't strictly necessary, though.
Just leave the quotes off your field names, otherwise it thinks you are giving it strings