I can't seem to insert my old table into the new table. Very weird
Code:
INSERT INTO 'newhawk_playewadwds' (`player_id`,'player') SELECT `player_id`,'player' FROM 'hawk_playewadwds';
Error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You
have an error in your SQL syntax; check the manual that corresponds to your MySQ
L server version for the right syntax to use near ''newhawk_playewadwds' (`playe
r_id`,'player') SELECT `player_id`,'player' FROM 'h' at line 1
Extra info:
Both tables exist and have slightly diffrent column structors (One has a varchar(50), while the other doesn't). Which is why i included the direct columns in the statement.
Quotes are delimiters for strings. Use backticks to escape column and table names, not quotes.
INSERT INTO `newhawk_playewadwds` (`player_id`,`player`)
SELECT `player_id`,`player`
FROM `hawk_playewadwds`;
But you actually only need to escape the reserved words.
Try to replace ' char with table names and for column player as below
INSERT INTO `newhawk_playewadwds` (`player_id`,`player`)
SELECT `player_id`,`player`
FROM `hawk_playewadwds`;
Use backticks across the query. You are using backticks and single quotes. Either use backticks or remove the single quotes:
-- This will work (Backticks only)
INSERT INTO `newhawk_playewadwds` (`player_id`,`player`)
SELECT `player_id`,`player`
FROM `hawk_playewadwds`;
-- This will also work (No backticks and no single quotes)
INSERT INTO newhawk_playewadwds (player_id,player)
SELECT player_id,player
FROM hawk_playewadwds;
Another thing, as you mentioned column structures are different,
If newhawk_playewadwds has column > 50, and hawk_playewadwds has < 50, It shouldn't be a problem
If hawk_playewadwds' has > 50 andnewhawk_playewadwds` has <50, then do either of the following:
Increase the size of the new table newhawk_playewadwds to same size of hawk_playewadwds
Use Substring in select as:
INSERT INTO `newhawk_playewadwds` (`player_id`,`player`)
SELECT `player_id`,SUBSTRING(`player` ,1,50)
FROM `hawk_playewadwds`;
Related
I am using JPA to generate a script for creating database tables based on my entities:
javax.persistence.schema-generation.scripts.action=create
javax.persistence.schema-generation.scripts.create-target=db_setup.sql
The file is generated with the correct tables, however the statements do not end with a semicolon, for example:
create table hibernate_sequence (next_val bigint)
insert into hibernate_sequence values ( 1 )
insert into hibernate_sequence values ( 1 )
I assume this is valid in standard SQL? However, it is not valid in MySQL. Is there a way to tell JPA to add the semicolons to the end of the line? Or what else could be the reason that it is missing?
Since Hibernate 5.1.0, the line delimiter for the generated SQL can be defined by setting the hibernate.hbm2ddl.delimiter property, e.g.
hibernate.hbm2ddl.delimiter=";"
The default is no delimiter.
See also comments on this ticket.
I am trying to get rows from table using SQLDeveloper by writing simple query:
SELECT * FROM agreements WHERE agreementkey = 1;
SELECT * FROM agreements WHERE agreementkey = 4;
but getting invalid character encountered error. It's not a problem with query(working using other keys, i.e. agreementkey = 3) but with XMLType column in this table - there is something wrong with data in some rows. Is there a way to select this affected row(I know keys of this affected rows) using queries? Maybe export to file or something? Solution of copying value manually is not acceptable.
Create an empty copy of the table and then run an INSERT into it based on a select from the original table but do it using the DML error logging clause.
This should show you any rows that fail to load and the reason for the failure.
CREATE TABLE test_agreements
AS SELECT * FROM agreements
WHERE ROWNUM <1;
INSERT INTO test_agreements
SELECT *
FROM agreements
LOG ERRORS REJECT LIMIT UNLIMITED
This will create you an error logging table called ERR$TEST_AGREEMENTS which you can query to find the problem rows.
Problem is in WHERE key = 1 cause key is a Reserve Word in Oracle. You should escape it like
SELECT * FROM table WHERE "key" = 1;
KEY is a reserved word so to overcome that you need to use double quotes "".
SELECT * FROM table WHERE "key" = 1;
I think the problem can be solved by putting the argument in quotes:
SELECT * FROM agreements WHERE agreementkey = "1";
I wish I were familiar with XML, but I have run into this with VARCHAR2 columns that are supposed to have all numeric values. Oracle looks at the request
where agreementkey = 1
and tries to convert agreementkey to a number rather than 1 to a varchar2.
If the database contains invalid characters I would try one the following:
Maybe the solution of BriteSponge will work, using an insert statemant with error logging clause.
Use datapump to export the data to a file. I think the log will contain information to identify the invalid columns.
There was a tool called "character set scanner" that checked the characters of the data of a table, here is some documentation: CSSCAN. Or maybe you can use the Database Migration Assistent for Unicode (DMU) mentioned in the same manual.
4- You can write a small PL/SQL program that retrieves the rows row by row and in case of an error catches the exception and notifies you about the row.
DECLARE
invalid_character_detected EXCEPTION;
PRAGMA EXCEPTION_INIT(invalid_character_detected, ???); begin
for SELECT rowid into rec FROM agreements do begin
for
SELECT * into dummy
FROM agreements
where rowid=rec.rowid
do
null;
end loop;
except
WHEN invalid_character_detected THEN
dbms_ouput.put_line(rec.rowid)
end;
end loop;
end;
I did not compile and test the program. ??? is the (negative) error code, e.g. -XXXXX if the error is ORA-XXXXX
I am trying to setup a query for my application to pull only values from a table that have a specific column set. Mostly this column will be null, but if you edit and save the item on the application end without putting anything in this field, then it saves a blank string to that database field.
I have tried the TSQL query:
SELECT * from TABLE where COLUMN is not NULL AND COLUMN != ''
This query returns the results I need, but when I run the same query in HQL:
SELECT OBJECT from TABLE where COLUMN is not NULL and COLUMN <> ''
Then it still contains the values that have a blank string in that column. I have tried this using HQL with the operators <> and !=, and have also tried converting it to a criteria object using Restrictions.ne("column","") but nothing seems to provide the result I need.
I tried Length as in the comments, but had no luck. With the length in the query hibernate generates the full query as so. the time_clock_id column is the one that i'm having the problem with. Hibernate is set to SQLServerDialect
select timezone0_.time_zone_id as time1_368_, timezone0_.version as version368_, timezone0_.modification_timestamp as modifica3_368_, timezone0_.time_offset as time4_368_, timezone0_.modification_user as modifica5_368_, timezone0_.name as name368_, timezone0_.description as descript7_368_, timezone0_.active as active368_, timezone0_.time_clock_id as time9_368_ from time_zone timezone0_ where timezone0_.active=1 and (timezone0_.time_clock_id is not null) and len(timezone0_.time_clock_id)>0
Rookie Mistake. There was another place within my action class where I was using a different query to build the select list in the application. This was resulting in the list being overwritten with all values instead of those that use blank. After snipping this duplication I can use the operator column <> '' and I am getting the correct results
I am attempting to query from a tab delimited file with H2 and java. When I select * there are no problems, however, one of the columns has a space in the column name. When I try to query on just that column I get an exception:
Caused by: org.h2.jdbc.JdbcSQLException: Column "EXAMPLE" not found; SQL statement:
It appears as though it is not grabbing both words in the column name (Example ColumnName), but only grabbing the first.
This is what I have:
System.out.println( simpleJdbcTemplate.queryForList( "SELECT Example ColumnName FROM CSVREAD('" + fileName
+ "', null,'UTF-8', chr(9)) where send = 1;", new Object[] {} ) );
I'm guessing there is a special syntax to do this, but I can't seem to find it. I've tried enclosing the column name in: square brackets, single quotes, double quotes, tick marks all to no avail.
Is there a way to query H2 using columns that have spaces in the name?
According to the documentation, double quotes should do the trick.
Remember to escape them correctly in Java, i.e. to just store your column name in a String, use
String exampleColumnName = "\"Example ColumnName\"";
Also, note it is case sensitive, from the documentation:
Quoted names are case sensitive
You can also use brackets [] if you set "MODE=MSSQLServer" in your connection properties. Example:
SELECT * FROM [My Table]
The connection string would look something like this:
jdbc:h2:~/test;MODE=MSSQLServer
If I have a SQL table with columns:
NR_A, NR_B, NR_C, NR_D, R_A, R_B, R_C
and on runtime, I add columns following the column's sequence such that the next column above would be R_D followed by R_E.
My problem is I need to reset the values of columns that starts with R_ (labeled that way to indicate that it is resettable) back to 0 each time I re-run my script . NR_ columns btw are fixed, so it is simpler to just say something like:
UPDATE table set col = 0 where column name starts with 'NR_'
I know that is not a valid SQL but I think its the best way to state my problem.
Any thoughts?
EDIT: btw, I use postgres (if that would help) and java.
SQL doesn't support dynamically named columns or tables--your options are:
statically define column references
use dynamic SQL to generate & execute the query/queries
Java PreparedStatements do not insulate you from this--they have the same issue, just in Java.
Are you sure you have to add columns during normal operations? Dynamic datamodels are most of the time a realy bad idea. You will see locking and performance problems.
If you need a dynamic datamodel, take a look at key-value storage. PostgreSQL also has the extension hstore, check the contrib.
If you don't have many columns and you don't expect the schema to change, just list them explicitly.
UPDATE table SET NR_A=0;
UPDATE table SET NR_B=0;
UPDATE table SET NR_C=0;
UPDATE table SET NR_D=0;
Otherwise, a simple php script could dynamically build and execute your query:
<?php
$db = pg_connect("host=localhost port=5432 user=postgres password=mypass dbname=mydb");
if(!$db) die("Failed to connect");
$reset_cols = ["A","B","C","D"];
foreach ($col in $reset_cols) {
$sql = "UPDATE my_table SET NR_" . $col . "=0";
pg_query($db,$sql);
}
?>
You could also lookup table's columns in Postgresql by querying the information schema columns tables, but you'll likely need to write a plpgsql function to loop over the query results (one row per table column starting with "NR_").
if you rather using sql query script, you should try to get the all column based on given tablename.
maybe you could try this query to get all column based on given tablename to use in your query.
SELECT attname FROM
pg_attribute, pg_type
WHERE typname = 'tablename' --your table name
AND attrelid = typrelid
AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')
--note that this attname is sys column
the query would return all column with given tablename except system column