Dropping XML columns in DB2 - sql

I'm trying to drop an XML column in db2 in the following manner. But every time I do this, it is resulting in an error
create table One(name int, address XML);
alter table One add column age xml;
alter table One drop column age;
Error starting at line : 5 in command -
alter table One drop column age
Error report -
DB2 SQL Error: SQLCODE=-1242, SQLSTATE=42997, SQLERRMC=7, DRIVER=4.11.77
DB2 official documentation suggests that issue is fixed in DB 9.7. I'm currently checking on 10.5 & 11.5 versions but still facing the same issue.
https://www.ibm.com/support/knowledgecenter/en/SSEPEK_11.0.0/xml/src
/tpc/db2z_alterxml.html
DB2 documentation suggests to run CHECK pending status on a table after a re-org but there were no commands that are available.
Is there a way to resolve this drop column issue for XML datatypes? Or else DB2 is not allowed to drop XML columns in a table by default?
Can someone suggest on this issue?
https://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.wn.doc/doc/c0055038.html
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.messages.sql.doc/com.ibm.db2.luw.messages.sql.doc-gentopic5.html#sql1242n
IBM suggests all the XML columns in a table need to dropped in a single alter statement. Is this still a restriction in 10.5 & higher versions of db2 ?

This remains a restriction in Db2-Linux/Unix/Windows up to and including Db2 v11.5.
IBM states (in the help for SQL1242N reason code 7)
"For a table containing multiple columns of data type XML, either do
not drop any XML columns or drop all of the XML columns in the table
using a single ALTER TABLE statement
"
This restriction only applies to tables with more than one XML column.
You can workaround in various ways, for example create a new table and copy the existing data into it etc, or arrange your physical data model differently.

Related

Error writing dataframe to SQL database table using dbWriteTable in R

I'm attempting to create a new table in a SQL database from a series of dataframes that I've made available to the global environment (dataframes created via a function in R).
I'm able to connect to the server:
#libraries
library(odbc)
library(dplyr)
library(DBI)
#set up a connection to the database
staging_database <- dbConnect(odbc::odbc(), "staging_db")
#Write dataframe to table in database (table does not exist yet in SQL!)
dbWriteTable(staging_database , 'test_database', `demographics_dataframe`, row.names = FALSE)
However, I'm getting the following error:
Error: <SQL> 'CREATE TABLE "test_database" (
"field1" varchar(255),
"field2" BIT,
"field3" BIT,
"field4" varchar(255),
"field5" varchar(255),
"field6" varchar(255),
"field7" varchar(255),
"field8" INT,
"field9" INT,
Very unhelpful error here - is there something I'm missing? I've followed the documentation for dbWritetable. Something I'm noticing, that I believe may be a part of the problem, is that I can't view any existing tables within "staging_db".
dbListTables(staging_database) reveals a bunch of metadata, but no actual tables that exist (I can verify they exist by logging into Microsoft SQL Server).
I recently encountered the same error. In my case, there was no problem with special characters or unexpected symbols anywhere in the table, since I was able to write the table “piece by piece” each time writing only a subset of columns.
I am guessing it has something to do with the number of columns to be written. In a single go, I was able to write up to 173 columns.
Rather than think that this is the limit I would say there is an internal limit for the length of the “CREATE TABLE” string hidden somewhere along the way.
Workaround that worked for me was to split the table into 2 sets of columns with the ID in both of them and joining them later.
Edit
So the truth was for me that 2 columns had the same name. The error indeed comes from the "CREATE TABLE" string - one cannot create table with duplicated column names.
Even a year after asking the question, I hope this can help someone dealing with the same problem ;)

BigQuery create Table differences between standard and legacy sql

I have a few questions around the create table syntax in standard and legacy sql
The new BigQueryUI doesn't show standard sql types and shows only legacy types. I understand they are mapped one to one with the legacy types but the examples in creating partitioned tables shows options which are not available in the UI
If I create a table using the JSON field schema can I still use the standard sql?
The BigQueryUI shows only partitioning the table by Ingestion time, but I want to create a table with date column and I don't see an option for it. If I have to create the DDL manually, I did not see the examples on how to use JSON field schema to construct a create table statement.
The new BigQueryUI doesn't show standard sql types
BigQuery standardSQL and LegacySQL are 2 options to write SQL syntax (See this link for more detail) and have nothing to do with the column Types in BigQuery, Details on table types can be found in this link, I also find this Link helpful
If I create a table using the JSON field schema can I still use the standard sql?
To create a table using JSON you need to run bq command line, If you need help how to write this syntax let us know
but I want to create a table with date column and I don't see an option for it
You can use this standardSQL syntax to do this:
#standardSQL
CREATE OR REPLACE TABLE `project.dataset.tableId`
PARTITION BY myDate
CLUSTER BY cluster_col AS
SELECT * from sourceTable
Note: myDate column is a column in the source table

SSMS 2014 - DB Collation for unicode/ multiple languages

In SSMS 2014, I have a DB with collation set as Chinese_Simplified_Stroke_Order_100_CI_AI, in which I create a table for a regular process with about 50 columns in it.
It basically contains sales data of some products. Few of these columns have integers as values and others contain English text.
Two of these columns, however contain values in the form of text which is in Chinese. A sample script that I update the column with is as below:
ALTER TABLE table_xyz
ALTER COLUMN comments NVarchar (max)
COLLATE Chinese_Simplified_Stroke_Order_100_CI_AI
UPDATE table_xyz
SET comments =
CONCAT('以下的邮件是专为您的最终客户所准备。', Account_Name)
This has been working fine till now.
I have a similar table in a second DB with collation set as Japanese_CI_AS_KS_WS and accordingly the comments in this table are in Japanese. Sample update statement as below:
ALTER TABLE table_abc
ALTER COLUMN comments NVarchar (max) COLLATE Japanese_CI_AS_KS_WS
UPDATE table_xyz
SET comments =
CONCAT('次の電子メールは顧客のためのテンプレートです', Account_Name)
Now, I have been tasked to transfer these tables to an existing DB that has SQL_Latin1_General_CP1_CI_AS collation set as default. The problem is I whenever I update the above two tables in this new DB, all I get in the output is '???????'
I have tried searching for solutions and have observed the below:
Many suggestions include converting the datatype to unicode.
A few people at my workplace suggested changing the collation of the column.
Use UTF-8 as default character set
As per my knowledge the first two are already taken care of when I run the Alter table statement. The third point seems valid for MySQL and not SQL Server.
Also, if I import the table from the respective DBs directly along with the data, the column values are displayed correctly (in Chinese and Japanese text). However, when I truncate and try to load the data I face the problem. I would be unable to import data in this way, since the end objective is to keep all tables in a single DB and purge the remaining DBs.
Hope I've made the problem statement clear.
You need to use N at the beginning of string literal containing UNICODE characters.
The code in your first example should be like this.
CONCAT(N'以下的邮件是专为您的最终客户所准备。', Account_Name)

SQLite: How to add a column to all the tables in the database?

I am quite new to SQLite (SQL overall). I created a database and created multiple tables in it and suddenly realized I had forgot to add a column to them. Now I have 280 tables having distinct names without a column. I have tried adding * and combining with SELECT but it gives an error. How can I accomplish it?
my code:
ALTER TABLE "MYBUS.*" ADD COLUMN STOPID int null;
After 3 hours issue finally solved.
After searching for sometime, I stumbled upon this answer
SQlite alter table with result from select statement
I ran the code:select 'alter table '||tbl_name||' add STOPID int null' from sqlite_master
It returned all the 280 SQL queries needed but with quotation marks, so I took the clue from this.lau and created a python regex script and ran it. Worked like a charm and within 10 lines of code I was able to add a column to all my 280 tables.
See this link https://dba.stackexchange.com/questions/160145/add-columns-to-all-tables-in-a-database-if-the-columns-dont-exist
Even though it applies to SQL Server, this should help you in getting started.
Ensure that you perform a check on the column name before adding it to a table.

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.