I have a stored procedure in which the data is populated into the destination table using a temporary table(in this i perform all the computation).
the problem i am facing is i have two columns which are getting its value by concatenating two other columns.
i.e.
concat(status_code|status_reason) as STATUS
concat(prior_status_code|prior_status_reason) as PRIOR_STATUS
Now this concatenation i have done it in temporary table.
i have to load its data into a destination table and the destination table doesn't have the columns STATUS and PRIOR_STATUS.
for loading the data i am using
insert into Destination( column_names,Status,prior_status)
select (column_names) from temporary table
so when i execute this part it throws me an error that columns are invalid as not present in the table.
P.S. I have used alter command to add those two columns in the destination. Still not getting the desired result.
Is there any other way to do this and where am i going wrong in this?
please help.
Thanks in advance
try this to add you field. it is important that you put the fieldnames in Backquotes. status is a reserved word.
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100),
ADD COLUMN `PRIOR_STATUS` VARCHAR(100);
you can also specify the position of the field in the table like:
ALTER TABLE youTable
ADD COLUMN `status` VARCHAR(100) AFTER fieldxx,
ADD COLUMN `PRIOR_STATUS` VARCHAR(100) BEFORE fieldzz;
Related
Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.
I would like to extract the row header from a table and insert it in another table with the same number of columns and data type, how can i do it in sql studio without having to retype the whole headers again ?
header row
second table
You can use create table like
CREATE TABLE new_tbl LIKE orig_tbl;
this create an empty table with the same column name of the orig_tbl
if you need to change the column name you can use alter table
ALTER TABLE your_table_name CHANGE `Column 0` `Extension Field 1` ;
It's my code
alter table num_of_people add column date_pre varchar(40);
alter table num_of_people add column time_pre varchar(40);
The result like this:
enter image description here
But I can't insert new data into these two columns:
insert into num_of_people(date_pre,time_re) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
Nothing happened:
enter image description here
I create a new table,that's worked:
create table Date_time(
date_p varchar(40),
time_p varchar(40)
);
insert into date_time(date_p,time_p) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
So,Why?
I just want to split the charactor in column Deal_time of table num_of_people. What's the better way? I am a novice in MySQL.Thanks a lot.
According to your images, you should perform an Update query instead of Insert to the table num_of_people.
To explain why you can't insert new data into those two columns, my best guess is that at least one of your existings columns must be a NOT NULL column that has no explicit DEFAULT value defined. Even if you succeeded in executing the Update, the total number of your table's records would be doubled.
Ref: http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
For data entry into a NOT NULL column that has no explicit DEFAULT
clause, if an INSERT or REPLACE statement includes no value for the
column, or an UPDATE statement sets the column to NULL, MySQL handles
the column according to the SQL mode in effect at the time:
If strict SQL mode is enabled, an error occurs for transactional
tables and the statement is rolled back. For nontransactional tables,
an error occurs, but if this happens for the second or subsequent row
of a multiple-row statement, the preceding rows will have been
inserted.
If strict mode is not enabled, MySQL sets the column to the implicit
default value for the column data type.
I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.
One of the columns in a somewhat large table (~10,000 records) is of the data type DECIMAL(10,0). I'm using MySQL.
I'd like the values to be displayed to 2 decimal places, so I need to alter this to DECIMAL(10,2), without screwing up the table's existing records. How could this be done?
Which DBMS are you using ? you can try like this for MySQL :
alter table tblName modify columnName newDataType;