Why is this simple SQL code not working in Azure Databricks? - sql

I'm trying to alter a table in Azure Databricks using the following SQL code.I would like to add a column to the existing table 'logdata' and being unsuccessful.
ALTER TABLE logdata
ADD sli VARCHAR(255)
Error message:
Error in SQL statement: ParseException: no viable alternative at input 'ALTER LOGDATA'(line 1, pos 6)
I tried searching online, but couldn't find what's causing this. Could anyone please help this beginner please?

For Azure syntax be like : ALTER TABLE table_name ADD COLUMNS (col_name data_type)
So your query must be like :
ALTER TABLE logdata
ADD COLUMNS (sli VARCHAR(255))

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

using new columns in the temp table added by alter table not work

I have a problem that the new added column can't be used in the further comments.
I have a temp table built by "select into" then I need to add an identity column by "alter table". But when I want to use the new column in a "join", I got an error "Invalid column". please note that, these commands could work separately.
I think the reason is, the new column is not found by the compiler and it give an error before running it.
Is there a solution for that ?
I have got this problem in sql server 2000 and it seems in a newer version, the problem is not there.
create table #tmp_tb
(name varchar(4), val int)
insert into #tmp_tb values('ab',1);
insert into #tmp_tb values('abc',2);
select * from #tmp_tb
alter table #tmp_tb add id int NOT NULL IDENTITY(1,1);
select * from #tmp_tb
select id,name,val from #tmp_tb
An error occurred :
Msg 207, Level 16, State 3, Line 9
Invalid column name 'id'.
Replace the last line with
EXECUTE sp_executesql N'select id,name,val from #tmp_tb';
Indeed, the parser doesn't know about the new column yet. By passing it through sp_executesql you avoid this.

Oracle Error using Toad:

Since a couple of days I can't execute CREATE and TRUNCATE SQL statement using Oracle 12 and Toad 12, ONLY if I terminate my statements with a semicolumn. I've always used the same connections, user and tables, but now I receive these errors while querying:
CREATE TABLE W_TEST_01
AS SELECT *
FROM CFG_FLOW
WHERE 1 = 2;
ORA-00933: SQL command not properly ended
CREATE TABLE W_TEST_01 (PIPPO NUMBER);
ORA-00922: missing or invalid option
TRUNCATE TABLE W_TEST_01;
ORA-03291: Invalid truncate option - missing STORAGE keyword
Can someone please help me?
Try with parentheses, like this:
CREATE TABLE W_TEST_01
AS (SELECT * FROM CFG_FLOW WHERE 1 = 2);
And if it still does not work, try to see the CTAS Tips
I advise you to take a look at the following link
Hope this can help!

Changing datatype of a column in database

I have to change the datatype of a column from CHAR to INTEGER in database. I used the following query:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET TYPE(INTEGER);
however it's showing error in the keyword "column" and giving the error code as -104 on execution. Following is the error log;
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=ALTER TABLE CERT_REQUEST ALTER CERT_R;BEGIN-OF-STATEMENT;<create_proc>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
I also tried the using the Modify keyword, but it isn't identified in my database.
I also tried the following query but again of no use.
alter table CERT_REQUEST alter column cert_request_nbr integer
It gives the following error
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=integer;umn cert_request_nbr;<alter_col_action1>, DRIVER=3.50.152
SQLState: 42601
ErrorCode: -104
Please provide me some suggestions.
Use MySQL Type Conversion or ALTER
ALTER TABLE cert_request MODIFY CERT_REQUEST_NBR INTEGER;
Or try MySQL Type Conversion
CAST(CERT_REQUEST_NBR AS UNSIGNED)
You have to use MODIFY not ALTER in MYSQL
ALTER TABLE cert_request
MODIFY CERT_REQUEST_NBR INTEGER;
And in DB2 you can try like this:
ALTER TABLE cert_request ADD COLUMN Temporary INTEGER;
UPDATE cert_request SET Temporary=CAST(CERT_REQUEST_NBR AS INTEGER);
ALTER TABLE cert_request DROP COLUMN CERT_REQUEST_NBR;
ALTER TABLE cert_request CHANGE Temporary CERT_REQUEST_NBR INTEGER;
Have you tried:
ALTER TABLE `cert_request` CHANGE `CERT_REQUEST_NBR` `CERT_REQUEST_NBR` INT(10) NOT NULL;
Be careful any data that is not a integer will be lost.
It will be helpful if you specify which database you are using . In oracle the solution should be
Alter table cert_request modify CERT_REQUEST_NBR number(30)
In db2(based on the error you provided, I believe your database is db2) the solution is
Alter table cert_request alter column CERT_REQUEST_NBR set datatype decimal(30)
You are missing the word 'Data' between 'Set' and 'Type'.
It should be like this:
ALTER TABLE cert_request
ALTER column CERT_REQUEST_NBR SET DATA TYPE(INTEGER);
May be, It happen due to varchar data present in field values.
Delete that data, and then run your query
It is because your column is having data which are not integer. You will either need to remove those data and try or you can run a query mentioned in above post which will make your data null which are not int.

change column name in sql server without using sp_rename

I want to change my column name in sql server using a query, without using sp_rename command. Can you please tell me if anybody know it?
I tried as:
alter table table_name alter column 'column1' to 'column2'
and it gave an error message as:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'column1'.
It is not possible to rename a column using the ALTER TABLE statement in SQL Server. Reference.
Use sp_rename instead.
OR
You can right click on the table -> select rename -> type new name.
And example is here.
alter table table_name add column 'column2'
update table_name set column2 = column1
alter table table_name drop column column1
PSEUDOCODE, need data types, likely to turn off triggers, etc. Quite ugly, but does "get the job done".
NOT very good for large tables, of course...