Incorrect syntax near 'for' SQL Server - sql

I want to add a new Column to an existing Table that has already Data in it.
The Column should be NOT NULL.
Because of that i want to set a Default Value. But when i do it throws the following exception:
"Incorrect syntax near 'for'"
ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000' FOR SIDNew
I already had a look at How to set a default value for an existing column
and Incorrect syntax near the keyword 'FOR' but none of them helped me.

Simply lose the part FOR SIDNew. You are adding a new column with a default constraint. You are not adding a new default constraint to an existing column.

The FOR keyword is only required if you're adding default values to Azure SQL Data Warehouse or Parallel Data Warehouse. If you're using normal relational SQL Server (on-premise or Azure) then you can just write it without the FOR
ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000';
https://msdn.microsoft.com/en-gb/library/ms190273.aspx

Related

How do I change column data type in Redshift?

I have tried changing the column data type in Redshift via SQL. Keep getting an error:
[Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState 42601] ERROR: syntax error at or near "TABLE" LINE 17: ALTER TABLE bmd_disruption_fv ^
Unable to connect to the Amazon Redshift server 'eceim.master.datamart.eceim.sin.auto.prod.c0.sq.com.sg'. Check that the server is running and that you have access privileges to the requested database
The first sql query works. I have tried writing the Alter Table script before the Select lines but it did not work too.
`
*Extract selected columns and renaming them for easier reference
*/
select ID, Completion_Time AS Date_Reported, Name2 AS Name, Contact_Info_for_updates AS Contact_Info,
Your_operation_line AS Operation_Line, Aircraft_Registration_SMU_SMT_etc AS Aircraft_Reg,
Designation_trade_B1_B2_ACT_AST_AAT AS Trade, Choose_your_Issue AS Issue, Manpower, Material, Equipment_GES,
Information, Tools, State_details_here_SVO_number_too AS Issue_Details, Time_wasted_on_due_to_issue AS Time_Wasted,
State_additional_comments_suggestions AS Additional_Comments, Stakeholders, Status
from bmdm.bmd_disruption_fv
/*Change colum data type
*/
ALTER TABLE bmd_disruption_fv
{
ALTER COLUMN ID TYPE INT
}
`
Several things are causing issues here. First the curly brackets '{}' should not be in the alter table statement. Like this:
alter table event alter column eventname type varchar(300);
Second, and likely more importantly, you can only change the length of varchar columns. So changing a column type to INT is not possible. You will need to perform a multistep process to make this change to the table.

SQL | Change date to date+time

So I have been trying to fix this problem and other Stackoverflow don't seem to do it for me
I have a column called "entered" with the datatype: "date". I would like to change the datatype from date to date+time. All post are saying to use:
ALTER TABLE (table)
ALTER COLUMN (column) timestamp
But then I get the following error:
ERROR: syntax error at or near "timestamp"
SQL state: 42601
Character: 44
For the record, the table currently has data and I cant simply copy and drop it
The syntax you posted should work for SQL Server / MS Access.
In Oracle SQL, I have used the below SQL syntax to change the data type of a column. I am not sure if this was one of the other suggestions that did not work. Hope this works for you.
alter table
your_table_name
modify
(
your_column_name datetime
);

How to alter datatype of a column in BigQuery

I'm trying to change the datatype for a column in my bigquery table from INT64 to STRING with the condition it's not NULL.
When I type:
ALTER TABLE table_name ALTER COLUMN id STRING NOT NULL
I get an error
Syntax error: Expected keyword DROP or keyword SET but got identifier "STRING"
How should I resolve this?
It is unsupported to change a column's data type at the moment.
Take a look at the official documentation. It explains 2 ways to manually change a column's data type. For the record:
Using a SQL query: choose this option if you are more concerned about simplicity and ease of use, and you are less concerned about costs.
Recreating the table: choose this option if you are more concerned about costs, and you are less concerned about simplicity and ease of use.
Despite the fact that you have got the error due to not using SET:
ALTER TABLE table_name
ALTER COLUMN id SET DATA TYPE STRING
but anyway, unfortunately, it's not possible to alter from INT64 to STRING directly.
What you can do is create a new table using
CAST(id AS STRING) id

Error while trying to mask a column in SSMS

I'm trying to mask some information in my table, when I use this :
ALTER TABLE Person
ALTER COLUMN DoB add masked with (Function = 'default()')
I get those two error :
Incorrect syntax near the word 'masked'
and
Incorrect syntax near the keyword 'with'. If the statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
That's literally the same code as the Microsoft documentation, but for some reason it doesn't work
If somebody could explain me what is the problem, that would help me a lot
To mask a column using the ALTER statement the datatype of the column must be included in this statement. The ADD keyword is also not necessary. I don't know what the datatype of the DoB column from your example is but it will need to be changed as below. From the name I'm guessing this is a datetime column, but if it's not the matching datatype will need to be substituted in.
ALTER TABLE Person ALTER COLUMN DoB DATETIME MASKED WITH (FUNCTION = 'default()')
Update:
This error was a result of using SQL Server 2012 when this feature was released in SQL Server 2016.

Error message when trying to import db.sql into my database on phpMyAdmin?

I'm using phpMyAdmin to hold my databases.
I made a database and I pressed the "import" button.
After that, I chose my file db.sql
And then I clicked the go button.
Instead of importing my file, this error message comes up:
Error
SQL query:
TABLE structure FOR TABLE `category` --
CREATE TABLE `category` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`category` VARCHAR( 30 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =9;
MySQL said:
#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 'Table structure for table category
CREATE TABLE category (
id int(1' at line 1
What does all of this mean?
And how do I get my file to import to the database?
How do I fix this?
I'm trying to open up an ebook that I need to read, and I can't find it anywhere else.
Thanks, I really appreciate your help.
I'd expect the first line to be a comment (start with --). The lack of the comment delimiter combined with what isn't really an SQL statement are causing your error message since MySQL doesn't know what to do with the line TABLE structure FOR TABLE `category`.
Where did this SQL statement come from? Either there's a bad copy/paste or the original source is missing the comment part.
Either remove the first line or add the two dashes to make MySQL ignore it as a comment:
-- TABLE structure FOR TABLE `category` --
Also, the trailing -- is pretty much ignored and probably the result also of a bad copy/paste.

Categories