Error while trying to mask a column in SSMS - 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.

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
);

MariaDB to calculate table reference in select query

I have a quite dumb client application that wants to get information from MariaDB based on a parameter.
This parameter is a string that contains spaces, like 'valid parameter'.
In MariaDB there is a table for each of the possible string values, and the table name is the string value after spaces have been replaced by underscores and a prefix is added. So I can perform the necessary conversion like this:
SELECT CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
Now the result 'prefix_valid_parameter' is the table to query, so actually I need to fire off
SELECT * from CONCAT('prefix_', REPLACE('valid parameter',' ','_'));
but MariaDB responds with
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '('prefix_', REPLACE('valid parameter',' ','_'))' at line 1
I would have expected either the content of table 'prefix_valid_parameter' or an error stating "table 'prefix_valid_parameter' not found". How can I make the table_reference part of my SQL SELECT statement dynamic?
You need to use dynamic SQL:
set #sql = 'select * from [table]';
execute immediate replace(#sql, '[table]',
concat('prefix_', replace('valid parameter', ' ', '_'))
);
I should add that the need to do this perhaps suggests a flaw in your data model. If the tables have the same structure, it is better to put all the rows in a single table.

Incorrect syntax near 'for' SQL Server

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

Current date UPDATE statement not working in SQL Server 2012

I'm trying to update a column named dattime that is a date column (not a datetime column like the name would lead you to believe) in my table named pr-pre-a with the current date. I'm using SQL Server 2012 and when I use:
UPDATE pr-pre-a
SET [dattime] = getdate()
the getdate() is not bolded meaning it is not a recognized command, and when I try to run it, it tells me there is a syntax error. however when I use:
UPDATE pr-pre-a
SET [dattime] = current_timestamp
it is bolded, but it still says there is a syntax error. What do I need to change to get this to work?
May be you need to escape - in your table name using []
UPDATE [pr-pre-a]
SET [dattime] = getdate()
Have you tried wrapping your table name in []?
I don't think SQL Server likes hyphens in table names without the square brackets or double apostrophes.