SQL Deletion of only one field with where clause - sql-server

I have a table thus
ID stuff1 stuff2
1 10 cool
2 4 poor
3 8 cool
4 1 sucks
I need an sql to delete only 10 in row 1
I used->>> delete stuff1 from mytable where ID=1
This works on access db but does not work on sql server
How do I do this on SQL server???

Are you trying to delete the row, or do you just want to remove the values from column stuff1?
In case you want to delete the row:
DELETE FROM mytable where ID = 1
In case you just want to remove the value of stuff1:
-- to set it to null
UPDATE mytable SET stuff1 = NULL WHERE ID = 1
-- to set it to zero
UPDATE mytable SET stuff1 = 0 WHERE ID = 1

By delete one field, I'm guessing you really mean null the column value:
UPDATE mytable
SET stuff1 = null
WHERE ID=1
DELETE works on whole rows, not individual columns.

Related

Query using a statement within a VARCHAR2 column

Is there a way for a select statement to include in the WHERE clause a statement that is contained within the table? For example, the following table:
CREATE TABLE test_tab(
date_column DATE,
frequency NUMBER,
test_statement VARCHAR2(255)
)
/
If
MOD(SYSDATE - DATE, frequency) = 0
were contained within the column test_statement, is there a way to select rows where this is true? The test_statement will vary and not be the same throughout the table. I am able to do this in PL/SQL but looking to do this without the use of PL/SQL.
This kind of dynamic SQL in SQL can created with DBMS_XMLGEN.getXML. Although the query looks a bit odd so you might want to consider a different design.
First, I created a sample table and row using your DDL. I'm not sure exactly what you're trying to do with the conditions, so I simplified them into two rows with simpler conditions. The first row matches the first condition, and neither row matches the second condition.
--Create sample table and row that matches the condition.
CREATE TABLE test_tab(
date_column DATE,
frequency NUMBER,
test_statement VARCHAR2(255)
)
/
insert into test_tab values(sysdate, 1, 'frequency = 1');
insert into test_tab values(sysdate, 2, '1=2');
commit;
Here's the large query, and it only returns the first row, which only matches the first condition.
--Find rows where ROWID is in a list of ROWIDs that match the condition.
select *
from test_tab
where rowid in
(
--Convert XMLType to relational data.
select the_rowid
from
(
--Convert CLOB to XMLType.
select xmltype(xml_results) xml_results
from
(
--Create a single XML file with the ROWIDs that match the condition.
select dbms_xmlgen.getxml('
select rowid
from test_tab where '||test_statement) xml_results
from test_tab
)
where xml_results is not null
)
cross join
xmltable
(
'/ROWSET/ROW'
passing xml_results
columns
the_rowid varchar2(128) path 'ROWID'
)
);
This calls for dynamic SQL, so - yes, it is PL/SQL that handles it. I don't think that SQL layer is capable of doing it.
I don't know what you tried so far, so - just an idea: a function that returns ref cursor might help, e.g.
SQL> create table test (date_column date, frequency number, test_statement varchar2(255));
Table created.
SQL> insert into test values (trunc(sysdate), 2, 'deptno = 30');
1 row created.
SQL> create or replace function f_test return sys_refcursor
2 is
3 l_str varchar2(200);
4 l_rc sys_refcursor;
5 begin
6 select test_statement
7 into l_str
8 from test
9 where date_column = trunc(sysdate);
10
11 open l_rc for 'select deptno, ename from emp where ' || l_str;
12 return l_rc;
13 end;
14 /
Function created.
Testing:
SQL> select f_test from dual;
F_TEST
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
DEPTNO ENAME
---------- ----------
30 ALLEN
30 WARD
30 MARTIN
30 BLAKE
30 TURNER
30 JAMES
6 rows selected.
SQL>
A good thing about it is that you could save the whole statements into that table and run any of them using the same function.
You can try this
select * from test_tab where mod(sysdate - date, frequency) = 0;

oracle trigger to update a new row of view while inserting new row in a table

Can someone help me to write a trigger on an oracle expression view x which is populating data from another table y through a select query.
The logic:
After inserting a new row in table y; when value of a new column on an inserted row in table y is 'xyz' then I want to update some columns of expression view x based on the condition 'xyz' on the new row inserted to table y.
Can someone help?
Chandra
This is how I understood the problem (though, more through reading comments than a question whose "expression view" made me puzzled).
That would be a trigger-based solution; it checks whether value entered into the A column is larger than 100 (that's your "if condition"); if so, it modifies both C and D columns. If not, it doesn't do anything.
SQL> create table test (a number, b number, c number, d number);
Table created.
SQL> create or replace trigger trg_bi_test
2 before insert on test
3 for each row
4 when (new.a > 100)
5 begin
6 :new.c := 3;
7 :new.d := 4;
8 end;
9 /
Trigger created.
SQL> insert into test (a) values (50);
1 row created.
SQL> insert into test (a) values (200);
1 row created.
SQL> select * from test;
A B C D
---------- ---------- ---------- ----------
50
200 3 4
SQL>

Swap values in a column in SQL Server table

I am trying to update my tables data(1=>3, 2=>1, 3=>2) by swapping them using below queries.
/* Temporarily set 1 to a dummy unused value of 11
so they are disambiguated from those set to 1 in the next step */
update <tablename>
set id = 11
where id = 1
update <tablename>
set id = 1
where id = 2
update <tablename>
set id = 2
where id = 3
update <tablename>
set id = 3
where id = 11
Wondering if I can optimize my script.
You can just use case. Conceptually the operation happens "all at once" so there's no need to use a fourth dummy value as in your sequential approach.
UPDATE YourTable
SET ID = CASE ID WHEN 1 THEN 3
WHEN 2 THEN 1
WHEN 3 THEN 2
END
WHERE ID IN (1,2,3)
Though changing ids is unusual as they should generally be immutable.

Oracle update where select statement returns multiple row error

Hi i have a table like this:
ID Name Number
1 john 91234567
2 tom 98765432
3 ken 91357246
...
I am trying to change number [91234567] to another number but i get this single row query returns more than 1 row.
My statement:
Update table set number = '9000000' where id = (select id from table where number = '91234567')
Perhaps i have another record with the same number further down the table.
Since i do not have access to the id, how can i change my statement? Thanks.
Try to use IN instead of =
Update table
set number = '9000000'
where id IN (select id from table where number = '91234567')
If you need to change all the records containing 91234567 to 9000000:
update table
set number = 9000000
where number = 91234567

Update Trigger conditional values

I have been searching around stackoverflow but I have not found what I'm looking for.
I have a Sql Server Database. One table with a field "Priority" I would like to update on every insert or update.
The problem is that I want to perform a Priority pile using this rules:
1.-If the value I try to insert has a prority value that already exists then every consecutive row in the table must change its priority value adding 1.
2.-If the value I try to insert has a priority that not exists then the trigger does nothing.
This is the trigger I haver built:
ALTER trigger [Priority]
on [dbo].[TBL_PILA]
after insert, update
AS
declare #priority int;
declare #reg_id int;
SELECT #reg_id =i.id from inserted i;
SELECT #priority =PRIORITY from TBL_PILA where ID =#reg_id
-- perform update here in TBL_PILA table
UPDATE TBL_PILA SET PRIORITY=PRIORITY+1 WHERE ID <>#reg_id AND PRIORITY>=#priority
GO
Edit: As Suggestions, I change the trigger body like this:
ALTER trigger [Priority]
on [dbo].[TBL_PILA]
after insert, update
if exists (select *
from inserted i join TBL_PILA m
on i.ID = m.ID
)
update TBL_PILA set PRIORITY = PRIORITY + 1
where exists (select * from inserted i
join TBL_PILA m
on i.ID = m.ID
where m.PRIORITY>=i.PRIORITY)
Now I have a new problem: the priority value inserted is affected by the trigger and it is added 1 too.
Does anybody knows how to increase priority values only affecting some range as I explained below?
The problem is whenever exists a few consecutive values and one not consecutive value the trigger must stops.
Example
Priority 1 2 4 5 6 8
If I try to insert a row with priority=3 the result should be:
1 2 3 4 5 6 8
Then If I try to insert a row with priority=4 then the result should be:
1 2 3 4 (inserted value) 5 (4+1) 6 (5+1) 7 (6+1) 8
But using the trigger I built I get this: 1 2 3 4 5 6 7 9 (8+1)

Resources