Custom SQL with ALTER TABLE - sql

In Tableau, I'm trying to add a column through Alter Table in a custom SQL
It looks like this:
ALTER TABLE (SELECT [W$].[W], [W$].[SDate], [W$].[EDate], [Date E$].[Date]
FROM [W$],[Date E$]
WHERE [Date E$].[Date]
BETWEEN [W$].[SDate] AND [W$].[EDate])
ADD [Type] string NOT NULL DEFAULT "P1"
But I get an error saying Syntax Error in FROM clause.
I also get this error if I use ALTER TABLE [Date E$]
ALTER TABLE [Date E$]
ADD [Type] string NOT NULL DEFAULT "P1"
error message:
Database error 0x80040E14: Syntax error in FROM clause.
; The query: SELECT TOP 1 * FROM ( ALTER TABLE [Date E$]
ADD [Type] string NOT NULL DEFAULT "P1" ) [TableauSQL]
This is my first time using Alter Table so I'm not sure where the FROM clause is being used, or if I'm doing this incorrectly. I'm following the instructions I found here:
Add a column with a default value to an existing table in SQL Server
Is it possible that Tableau doesn't allow Alter Table to be used when creating a Custom SQL?
I am using Tableau Desktop 2018.1.2

Tableau custom SQL will not allow DDL statements such as Alter table. It creates a read-only connection to the database.
You might be able to do this using the initial sql function that is available when you setup your connection. Really Tableau is the wrong tool for this. If you want to alter tables you should use a SQL client thats appropriate for your database.

Related

how to fix this please

I Created the db with phpmyadmin and created 8 columns i forgot to create date column i tried to create it with this code and i got this
ALTER TABLE "users" ADD "cdate" DATE
1064 - 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 '"users" ADD "cdate" DATE' at line 1
You can Use phpmyadmin to add a new column without any code
Or Use this :
ALTER TABLE `users` ADD `cdate` DATE
You should use this ` not this "
you can add columns to existing table using phpmyadmin
below the table structure you can find add [num] columns
and since you asked
ALTER TABLE `users` ADD `cdate` DATE
Read about this in the documentation https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
ALTER TABLE table_name ADD column_name column_definition
you dont need any qoute
ALTER TABLE users ADD cdate DATE
Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a reserve word of mysql

How do I change a column data type from varchar(255) to date?

I am using a reporting database which consists of 20 tables on SQL Server. In marketing table I have a column report_date which is currently a varchar(255). It is basically a date formatted in a way 2017-12-12. I want to change the type of this column to a date. I’m running this script but getting errors. The script is down below:
USE [reporting].[dbo].[marketing]
GO
SELECT CONVERT(date, 'report_date');
These are the errors I’m getting.
Msg 911, Level 16, State 1, Line 1
Database 'dbo' does not exist. Make sure that the name is entered correctly.
Msg 241, Level 16, State 1, Line 3
Conversion failed when converting date and/or time from character string.
How should I adjust the script?
If you want to change the column's data type (and you should) then you need to use an alter table statement. Your first error message is because of the USE directive - it should be
USE [reporting]
GO
Your second error message is because 'report_date' is a string constant, not a column name.
The select statement should be
SELECT CAST(report_date as date) -- Don't use Convert without the style argument....
FROM [dbo].[marketing]
Note that if you have even a single value that can't be converted to date you will get the second error again.
Basically I would recommend first making sure that the select statement completes without any exceptions, and only then alter the table:
USE reporting
GO
ALTER TABLE [dbo].[marketing]
ALTER COLUMN report_date DATE
GO
TRY THIS:
SELECT CONVERT(DATE, report_date) --If only for comparison
ALTER TABLE marketing ALTER COLUMN report_date DATE --If want to change in the table
The proper way to do this is like;
ALTER TABLE reportin.dbo.marketing ALTER COLUMN 'report_date' date
And you can check this How do you change the datatype of a column in MS SQL?
"Convert" is used for conversion from one datatype to other in select queries, you need to use alter statement for altering database columns and also
USE [databasename] is enough, so rewriting your query here :
USE [reporting]
GO
ALTER TABLE marketing ALTER COLUMN ReportDate DATE
Slow, but safe way is to:
Create a new column (ALTER TABLE dbo.MyTable ADD MyNewColumn DATE NULL;)
Update the new column using the old one (UPDATE dbo.MyTable SET MyNewColumn = CONVERT(DATE, MyColumn);)
Drop the old column (ALTER TABLE dbo.MyTable DROP MyColumn;) - Alternatively, you can rename this column instead and keep it as is)
Rename the new column (EXEC sp_rename 'dbo.MyTable.MyNewColumn', 'MyColumn', 'COLUMN';)
You might have to drop indexes beforehand, but this method (and it's alterations) help to prevent data loss.
If you encounter an error during casting, you should eliminate those values from the update (by for example adding a WHERE clause) and investigate them manually.
If you are using SQL Server 2012 or newer, you can use TRY_CONVERT() to ignore the values which cannot be converted to DATE. In this case you will have NULL in your new column.
Before you do anything, make sure, that all applications and code which is working with this column can handle the changes.
Notes
You might want to rebuild the table/indexes after a change like this.

Google BigQuery: how to create a new column with SQL

I would like to add an column to an already existing table without using legacy SQL.
The basic SQL syntax for this is:
ALTER TABLE table_name
ADD column_name datatype;
I formatted the query for Google BigQuery:
ALTER TABLE `projectID.datasetID.fooTable`
ADD (barColumn date);
But than the syntax is incorrect with this error:
Error: Syntax error: Expected "." or keyword SET but got identifier "ADD" at [1:63]
So how do I format the SQL properly for Google BigQuery?
Support for ALTER TABLE ADD COLUMN was released on 2020-10-14 per BigQuery Release Notes.
So the statement as originally proposed should now work with minimal modification:
ALTER TABLE `projectID.datasetID.fooTable`
ADD COLUMN barColumn DATE;
BigQuery does not support ALTER TABLE or other DDL statements, but you could consider submitting a feature request. For now, you either need to open in the table in the BigQuery UI and then add the column with the "Add New Field" button, or if you are using the API, you can use tables.update.
my_old_table
a,b,c
1,2,3
2,3,4
CREATE TABLE IF NOT EXISTS my_dataset.my_new_table
AS
SELECT a,b,c,
"my_string" AS d, current_timestamp() as timestamp
FROM my_dataset.my_old_table
my_new_table
a,b,c,d,timestamp
1,2,3,my_string,2020-04-22 17:09:42.987987 UTC
2,3,4,my_string,2020-04-22 17:09:42.987987 UTC
schema:
a: integer
b: integer
c: integer
d: string
timestamp: timestamp
I hope all is clear :)
With this you can easily add new columns to an existing table, and also specify the data types. After that you can delete the old table, if necessary. :)

Stop allowing null in a table column

I have a table in SQL server 2008.
I need a column that until now wasn't necessary, not to allow NULL anymore, without droping the table.
I tried to do something like this:
ALTER TABLE [Sessions] ALTER COLUMN region_id NOT NULL;
EDIT: It was a small syntax error. solved.
You have to specify the data type as well when you alter a column:
ALTER TABLE [Sessions] ALTER COLUMN region_id int /* ? */ NOT NULL;
you are missing datatype.
ALTER TABLE [Sessions] ALTER COLUMN region_id int NOT NULL;
You have to first set all values that are NULL to a non NULL value:
UPDATE [Sessions]
SET region_id=-1
WHERE region_id IS NULL
Instead of -1 you should choose something that would represent the formerly NULL values so you may distinguish them.
To get away with saving prevention you may go on:
Tools>Options>Designers> and unclick Prevent Saving Changes that require table re-creation
First off to save table chances like this you need to either script the changes, or when saving from the UI, you need to set SSMS to drop and recreate table for you. To do this;
Go to Tools
Options
Designers
UNTICK prevent saving changes that require table re-creations.
If you script the changes, you will need to first alter the table, and add a new column allowing nulls. Then update the column and remove nulls and then after you will be able to set the column to NOT NULL.
If you follow these steps you will solve your issue.

SQL server 2005 query not running

Before posting this question, I have tried so many things but that was not helpful for me.
I want to rename the column of table at sql server 2005, following query I have run at sql server2005:
1) ALTER TABLE Details RENAME COLUMN
AccountID TO UID; but it gives me the
error: Incorrect syntax near the
keyword 'COLUMN'.
2)I have added one new column in the
table by query: ALTER TABLE Details
ADD BID uniqueidentifier; and then I
want to set the coulmn property to not
null .
How can i do that?
Thanks in advance
AS
Use sp_Rename 'TableName.Column', 'NewColumnName', 'COLUMN'.
In order to do your second part of the question, you'll need to do:
ALTER TABLE myTable
ADD myColumn UNIQUEIDENTIFIER NOT NULL DEFAULT 'some default value'
If you don't want to specify a default value, you'll have to first create the column with NULL. Once the column is created, you can then populate with your desired values and then re-alter the column to NOT NULL.
1) Instead of using ALTER TABLE, why not use sp_rename? For example:
EXEC sp_rename 'Details.[AccountID]', 'title', 'UID'
2) You can use ALTER TABLE Details ALTER COLUMN BID NOT NULL, but you'll probably want to specify a default value for it also, if the table already has data in it.