updating the table in sql server - sql-server-2005

I am trying the following code.
String sym=request.getParameter("symbol");
Statement st1=con.createStatement();
String symIns="UPDATE "+tblname+" SET Symbol='+sym+'";
int m=st1.executeUpdate(symIns);
if(m==0)
out.println("m is zero");
else
if(m!=0)
out.println("Inserted");
I am receiving the values of sym and tblname from previous page.But After executing this code it is printing m as zero and hence not updating the table. This code is not working for the table which does not have any record(contain only null values). But working for the table which have some records in it. I have also tried to execute it directly in the database. It is showing query executed successfully , but giving message as (0 row(s) affected). This is the query which is successfully running in the database.`
UPDATE six SET SYMBOL='eq' WHERE SYMBOL='be';
As I said it is working for this table and this table is having value for SYMBOL column as 'be'.

The query is behaving correctly. If you have matching records, it will update the rows.
So, In your query UPDATE six SET SYMBOL='eq' WHERE SYMBOL='be';, It will update the six table where you have rows with field value 'be'. But It will not update where you have NULL.
The query will execute but does not update any records and thus returns zero rows affected.
So, If you want to update rows which contains NULL as field value.
Add IS NULL
UPDATE six SET SYMBOL='eq' WHERE SYMBOL IS NULL

Related

Cannot insert a NULL value into column x using update statement in Redshift

I am trying to update several rows using output of the other query , having the condition when nombregestorevento LIKE 'karate%'. However I don't understand the reason why Redshift is throwing the error
ERROR: Cannot insert a NULL value into column nombregestorevento
while there is not any null in the output of any of the queries.
Here is the query:
begin;
update lu_gestor_evento
set nombregestorevento = (select nombregestorevento from lu_gestor_evento_pro a12
where a12.id_gestorevento = lu_gestor_evento.id_gestorevento)
WHERE lu_gestor_evento.nombregestorevento LIKE 'karate%';
commit;
I had checked both tables and I can't find any Null in any of the tables. How can I change the query to not throwing this error.
The error does indicate that your subquery returns a null value. If there are no null values in the source table, then it means that the subquery returned no rows - which reads as a null value in the set clause.
In other words, the error happens when the id_gestorevento of target table lu_gestor_evento does not exist in source table lu_gestor_evento_pro.
Probably, you want to update matching rows only, and leave others untouched. If so, in Redshift we can use the update/from syntax:
update lu_gestor_evento e
set nombregestorevento = ep.nombregestorevento
from lu_gestor_evento_pro ep
where ep.id_gestorevento = e.id_gestorevento

Insert a column from output of a query into already populated postgresql database without adding new rows

I have a table named comments_live which has 4 fields and some 24000 entries. I want to add another field which is the output of a select query. The query is like this:
INSERT INTO comments_live SELECT SUBSTR(record,1, POSITION(' ' IN record)) AS project_id FROM comments_live;
What this query is doing is it's appending the result of the SELECT query to the table. I want the 'project_id' field to be appended to the existing 24000 rows. i.e. The above query should not add those extra rows to the table.
Any help would be appreciated.
PS: I tried adding a empty column 'project_id' to the table first and then executing the query, still I'm getting the same result.
I conclude from your code example that you want to add a column whose content is the substring from the column RECORD. If you succeeded in adding the empty column, then you just need code like the following:
update comments_live
set project_id=substr(record,1,[...])

Values not updating but showing rows affected

THERE IS NO TRIGGER ON THIS TABLE.
I'm facing strange behaviour of sql server. One of the table column value not updating.
Here is the query and output:
Now if I execute the update statement, it executes successfully:
As per update statement all the clientId values should be 10, but it still remains 2. Here the select query result after executing update statement:
I really don't find any possible issue of this behaviour. Please help to solve this puzzle.
This might helpfully to help me:
SQL Server 2012 Express
Table:
Schema:
Table schema
If I rename the column name clientId to clientId2 or anything then update works. But if I rename the changed column name to clientId with udpated value then the updated values become 2 again.
If I keep the columne name same but table name change to Company2 or anything then then clientId values update fine.
https://raw.githubusercontent.com/codenamejakir/Demo-Video/master/sqldemo.swf
With transaction: https://raw.githubusercontent.com/codenamejakir/Demo-Video/master/SqlLive.swf
Overall I have noticed that if the table name "Company" and column name "ClientId" then column value not updating.
Thanks.

Update query in Access to update MANY columns from Null to value

I have a database table with about 100 columns (bulky, I know). I have about half of these columns which I will need to update iteratively to set Is Null or "" values to "TBD".
I compiled all 50 some columns which need to be updated into an update query with Access SQL code that looked something like this...
UPDATE tablename
SET tablename.column1="TBD", tablename.column2="TBD", tablename.column3="TBD"....
WHERE tablename.column1 Is Null OR tablename.column1="" OR tablename.column2 Is Null OR tablename.column2="" OR tablename.column3 Is Null OR tablename.column3=""....
Two issues: This query with 50 columns receives a "query is too complex" error.
This query is also just functionally wrong...because I'm losing data within these columns due to the WHERE statement. Records that had values populated which I did not want to update are being updated because of the OR clause.
My question is how can I go about updating all of these columns and setting their null or empty values to a particular value (in this case, "TBD")?
I know that I can just use a select query to select the columns I need to update, run it, and just CTRL+H to find & replace "" to "TBD". However, I'm worried about the potential for this to introduce errors into my dataset. I also know I could also go through column by column and update these values via an update query. However, this would be quite time consuming with 50+ columns & the iterative updates which I need to run on the entire dataset.
I'm leaning towards this latter route. I am still wondering if there are any other scripted options which I can build into a query to overcome such an issue, and that leads me here to you.
Thank you!
You could just run 50 queries:
UPDATE table SET column1="TBD" WHERE column1 IS NULL OR column1 = "";
An optimization could be:
Create a temporary table which determines which rows actually would need an update: Concatenate all column values such that a single NULL or empty would result in an record in your temp table. This way you only have to scan the base table once.
Use the keys from that table to focus on those rows only.
Etc.
That is safe and only updates your empty values (where as your previous query would have updated all columns unless you would have checked every value first with an IFNULL).
This query style also does not run into the too complex issue
You could issue one query as:
UPDATE tablename
SET column1 = iif(column1 is null or column1 = "", "TBD", column1),
column2 = iif(column2 is null or column2 = "", "TBD", column2),
. . .;
If you don't mind potentially updating all rows, you can leave out the where clause.

SQL update function doesn't throw any error message but it returns no rows

I am trying to update a table value from another table. I want to update pstatus from productcode.
Here is my code (which gives no error and results 0 rows):
UPDATE pstatus
SET code=(select code FROM productcode)
besides this, I can't re-run any update in sql - it gives no error, but returns 0 rows (I have more than 5 rows in productcode table).
In this case you have a two problem
you say you dont have any rows in pstatus if no record in table how can table row update ?
your inner query select code FROM productcode return more then one row then how sql decide to set with value in wich field?
An update statement doesn't return rows. You should have used something more like
Update pstatus
Set code=a.code
from productcode a
where a.*somekeyfield* = *value*
You have to determine which single record from productcode you wish to use as the basis for the update. What somekeyfield should be is something you need to determine.
An update statemend doesn't add records, it only updates records that already exist in the table.
The update query does actually udpate all the records in the table, but as there are no records, the number of updated records is zero.
If you want to add records, you should use an insert query instead:
insert into pstatus (code)
select code from productcode