This should be a really easy question, but I wanted to make sure it was done right where I'm not a SQL guy. We need to turn all of the patients with a security level of 1 to 2. All patients who are not at 1 are currently at 2. Thanks and sorry for asking such a simple question. SQL Server 2005.
select
patient_id,
security_level
from patient
where security_level = '1'
SQLFIDDLE
update patient set security_level = '2'
where security_level = '1';
Above will update the entire patient table, and set the security level to 2 (where it initially was 1).
A simple update should work. Something like:
UPDATE patient
SET sercurity_level='2'
WHERE security_level='1';
Edit: changed correct table name and added quotes
Related
I am trying to write a statement to find matching values from two different tables and set table 1 suppression to 1 if person_id from table 2 matches person_id from table 1 in SQL.
Table 1 = id_num, person_id, phone_number, supression
table 2 = person_id, uid
So if person_id from table 2 matches person_id from table 1 it should set each record to 1 in suppression column in table 1.
Sample data and dbms would be needed to give a reliable answer, but the general gist is like this
-- for mySql, syntax will vary by dbms
update table1
inner
join table2
on table1.personid = table2.personid
set table1.suppression = 1;
http://sqlfiddle.com/#!9/42dbf6
Thank you for the first suggestion. I ended up going with the below
update in_12768_load1_all set "suppression" = '1' from (select * from "sup1027") as a where a."person_id" = in_12768_load1_all."person_id";
I thought I explained my question pretty well. No clue why someone would down arrow this.
I have an issue where I need to update a field in a number of SQL Server tables. The customer Order ID needs to be updated on all records that do not follow the standard format "C000001" (a letter followed by numbers). There are approximately 300 records that need changed (carry over records from a previous database version), as well as the corresponding linked tables.
I am a bit rusty on my SQL, so would like to verify the commands.
My proposed commands
UPDATE Customer_Order
SET ID = CONCAT('X', ID)
WHERE ID not like 'c%';
UPDATE Customer_Order_Line
SET Cust_Order_ID = CONCAT('X', Cust_Order_ID)
WHERE Cust_Order_ID not like 'c%';
UPDATE Quote_Order
SET Cust_Order_ID = CONCAT('X', Cust_Order_ID)
WHERE Cust_Order_ID not like 'c%';
etc... (I have approx 12 additional tables to update same as above)
Assistance is appreciated, thanks in advance.
More of a formatted comment here..
Any time you want to do this, just make your update a select to verify your update:
SELECT ID, CONCAT('X', ID) as NewID
FROM Customer_Order
WHERE ID not like 'c%'
Make sure the NewID column is what you want to replace ID with.
So this is my first question and first day here. Thanks in advance.
I want to perform the following query -
Put the fine on books_return if the duration is violated, otherwise put zero.
The duration is in book_issue. SO I figured out that I can calculate the difference between return_date and issue_date and if it is greater than 10 then fine_amount will update to 20; otherwise will update to zero. However, I am having trouble with the query statement which is the following -
UPDATE (
SELECT brt.fine_amount
FROM book_return brt, book_issue bi
WHERE brt.book_id = bi.book_id
AND brt.return_date - bi.issue_date > bi.issue_duration
) SET fine_amount = 20;
error - ORA-01779: cannot modify a column which maps to a non key-preserved table
How can I pull off this query?
EDIT: book_id and borrower_id are the composite primary keys of both tables.
related tables -
You can do what you want with merge. If you use update, you can use exists:
UPDATE book_return brt
SET fine_amount = 20
WHERE EXISTS (SELECT 1
FROM book_issue bi
WHERE brt.book_id = bi.book_id AND
brt.return_date - bi.issue_date > bi.issue_duration
);
I have a situation where i need to update a row in table and when faced with a duplicate entry then take a decision based on another column value.
For example let's say my table is like this : salary_table
Salary Username Usersurname Last_entry_date
3000 abc bak 20-feb-13
4000 sdf kup 20-mar-15
5000 abc bak 20-mar-15
so my update query is something like this
update salary_table
set salary=9000
where username=abc
and usersurname=bak;
For records like row 2 when there is unique entry this will not cause any problem
but i will get multiple rows for records like 1 (1 and 3) but i only want to update one row. In this case i would like to check last_entry_date. And the entry which has latest date (row 3 in this case) should get updated.
How can it be done ?
Any help would be highly appreciated.
Update salary_table
set salary = 9000
where username= 'abc'
and usersurname= 'bak'
and Last_entry_date = (select max(Last_entry_date)
from SalaryTable
where s.username = username
and s.usersurname = usersurname);
you have to add "where clause" on what you want in this case
"last_entry_date = ??"
With out adding proper filter how you identify which row to be updated.
I don't have much experience with SQL Server and I'm pretty much lost when it comes to anything more advanced than simple INSERT/SELECT statements.
My task is to create table which hold 'criteria' (rules) for other tables in same DB. I mean we have an Employee table, a Salary table etc. I need to make some rules between Employee table and Salary table by its values, i.e. if Employee = John and Salary = 2000 then make 1 criteria, if Employee = Steve and Salary = 3000 then make 2 criteria.
I have created a table called Rules:
RuleID Criteria
--------------------------------------------------
1 Employee = John and Salary = 2000
2 Employee = Steve and Salary = 3000**
My question: As mentioned above is correct way to create 'criteria' table? (maybe I have 50 different conditions...Would it be correct way If I create table with all conditioned as mentioned above?) Please advise.
I am sorry If I am not able to explain it in correct way or asking basic question.
I would appreciate any help in this. Thanks a lot in advance..
We have created an effective Rules Engine that notifies the appropriate user(s) for a given rule. Our columns are as follows: RuleID, RuleName, SequenceNum, IsActiveFlag, RuleRunDays, DataSteward, CarbonCopyRecipients, NotificationLevel, RuleSQL, NotificationSubject, NotificationText, HTMLFlag
The key is to have a column that is a valid executable SQL statement (as RuleSQL above). Here is an example of a value you might put in the RuleSQL column:
SELECT ErrorMessage = 'Salary cannot be null for Active employees.'
, e.EmployeeName , es.Salary , ...
FROM EmployeeSalary es
JOIN Employee e
ON e.EmployeeId = es.EmployeeId
WHERE es.Salary IS NULL AND e.Status = 'Active'