Updating a column using deleted/inserted during update statement execution
I need to capture the old value while update statement is executed. I can not insert into temp table (there are many samples online where you can use OUTPUT clause to insert data in same table or another temp table)
something like
declare @count table
id int,
changed varchar (50)
insert into @count (id,changed) values (2,'T')
insert into @count (id,changed) values (3,'T')
insert into @count (id,changed) values (4,'T')
select * from @count
UPDATE @count
SET id=5,
Changed = GETDATE() -- here instead of date, I want to get inserted.id and deleted.id
OUTPUT inserted.id,
deleted.id
where id = 2
SELECT *FROM @count
Any help on this will be very much appreciated....
I am not sure to follow your question but you can get the old value just by doing:
UPDATE @count
SET id=5,
Changed = id
OUTPUT inserted.id,
deleted.id
WHERE id = 2;
since SQL Server uses all-at-once operations.
Microsoft SQL Server 2012 T-SQL Fundamentals
http://shop.oreilly.com/product/0790145321978.do
AMB
Some guidelines for posting questions...
Similar Messages
-
Using delete/insert mode operator target in mapping
Hello Guys,
Can you help me please to resolve my problem. It's very urgent.
I use OWB 10gR2 and I create a mapping for loading data from table to table.
I got a source table A that I want to integrate into a table B.
I want to put my target table B on delete/insert mode to delete data from B where A.annee = B.annee
before inserting data from A.
How to configure this mapping.
Thanks in advance.
Regards.
fanfita.you dont have to do anything in particular:
on the target table propperties click on the load type as Delete/Insert and specify the column which you need to check while deleting this can be done by clicking on the column of target table in your mapping and setting the properties such as "load while insert/delete/update etc" , you got to check the delete option of the columns that you want to check while deleting.
If your target table has sequence then delete and insert will generate new sequence id and i am not sure if there is a Foregin key which needs to be considered here so it might be a better option to use update/insert
if there are no dependencies at all then you can go for delete/insert option.
Edited by: Darthvader-647181 on Feb 5, 2009 1:46 AM -
Proper using of index for parallel statement execution
Hi all,
I've created index for my table
CREATE INDEX ZOO.rep184_med_arcdate ON ZOO.rep184_mediate(arcdate);It was before I started to think about parallel statement execution. As far as I've heard I should alter my index for proper using with parallel hint. Could you please suggest the way to go?marco wrote:
Hi all,
I've created index for my table
CREATE INDEX ZOO.rep184_med_arcdate ON ZOO.rep184_mediate(arcdate);It was before I started to think about parallel statement execution. As far as I've heard I should alter my index for proper using with parallel hint. Could you please suggest the way to go?when all else fails Read The Fine Manual
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/sql_elements006.htm#autoId63 -
Using 'Delete' button during Remote Desktop session
When I connect to my work PC from home everything works great except the delete button.
A PC has a delete button and a backspace button. For most software these two buttons do the same thing. The Mac has only a delete button which functions as a backspace button on the PC. If you want to delete a file, you must right click and select delete from the popup menu. I am using software that specifically needs the delete button to erase line items, but I do not have a delete button.
Is there anyway to enable this button to function as a delete button in the PC workspace?
ThanksPress the function key with the delete key and it should delete the file.
-
I want to update a column using select statment.
The statement starts with select keyword and i have to update the value in the emp table.
select deptno
case deptno
when 10 then
update emp set sal=sal+100 when empno=empno
else
update emp set sal=sal+200 when empno=empno
end)xy
from emp
The above statment shows error
ERROR at line 5:
ORA-00936: missing expression
Pls give some examples for this.user549774 wrote:
Your answer is correct but our requirement is we cannot start the statement with update keyword. We have to start using select keyword.Requirements are things like - Show all sales by year, Allow break out by region, etc.
Requirements do not specify syntax, and they do not specify impossibilities, such as
- Select data using a delete statement
- Delete data using an insert statement
- Truncate a table using the update statement
- Drive a car across the Atlantic ocean
So
Delete is for deleting
Insert is for inserting
Update is for updating
Select is for selecting
Whether it is possible to update the values using select case statement.No -
hai,
I wanted to use delete/insert option so that i can selectively delete data and insert
the new set of records.But what i found is that all the existing records get deleted
ie selective delete is not happening. Can anyone pls let me know how this can be achieved with the help of delete/insert option(in operator properties of table)
Thanks And Regards,
AshirThere is a specific Warehouse Builder forum here:
Warehouse Builder
You can also post questions to this forum as this forum covers all BI products. To answer your question, I am not sure what you are trying to do. Are you trying to delete the object via "Control Center"? It should be possible to deploy the job as a delete job to remove the directory from your target/source database.
Keith Laker
Data Warehouse Solution Architect
Oracle EMEA Consulting
BI Blog: http://oraclebi.blogspot.com/
DM Blog: http://oracledmt.blogspot.com/
BI on Oracle: http://www.oracle.com/bi/
BI on OTN: http://www.oracle.com/technology/products/bi/
BI Samples: http://www.oracle.com/technology/products/bi/samples/ -
How to get only updated records for a column using loading type INSERT
Hi,
Good morning all,
I have source1 containg 3 columns bill_cd,bill_desc,bill_date and
source2 has the columns bill_cd,bill_key,source_id.
My target has the columns bill_cd,bill_date,bill_desc .
Now the requirement is, bill_cd in target should not repeated when we run the mapping more than one thime. it should get only updated records not the previous records using only INSERT Loading type(for target) not to use update/insert.
How can we achieve this logic in mapping level.
Anybody Please give me some solution immediately.
Thanks in Advance,
SivThanks Herzog for your reply,
Here bill_cd is not unique. Yes,I want only new records using INSERT as loading type. Suppose when the map runs for the first time, bill_cd is loaded with values 1 to 5.
Now, in the source I have got new records 6 to 10 for bill_cd and when I run the mapping again I need to get only records for bill_cd from 6 to 10 using INSERT as loading type.
Is it possible to achieve this at mapping level?
Regards,
Siv. -
Updating a CLOB column using C++ with ATL
Hello,
I have tables with a single CLOB or BLOB colum with the rest of the columns of type VARCHAR2.
I'm getting a result of E_FAIL when updating a single row, for which I'm attempting to change one of the VARCHAR2 columns and the CLOB column. If I set the status for the CLOB column to DBSTATUS_S_IGNORE, the update succeeds (for the other column). If I set the status for all other columns to DBSTATUS_S_IGNORE and the status for the CLOB column to DBSTATUS_S_OK, it still fails.
If I use a SQL Server data source with the same schema and the same C++ code, the original update succeeds.
The length of the data I'm attempting to store is 11487 characters.
As near as I can tell, the Oracle provider does support this kind of functionality. Perhaps there is some coding variation needed for the Oracle data source. I have so far been unable to find a suitable example (or any code example for the Oracle provider using C++/ATL).
I'm using Oracle 9i Release 2 with the latest OLE DB provider (9.2.0.2.0).
Thanks,
Daniel E. Hale
[email protected]Some additional information on the problem:
I was using the ISequentialStream interface
with a Dynamic Accessor when the problem
occurred.
Since then I've tried using a SQL command to do
the update... and I've found the 11487 characters
is too long... I get a maximum length error when
I try the command in SQL+. (BTW, SQL Server works
fine with the long command.)
I doubt that the length is the reason for the failure
of the update using the ISequentialStream interface.
I'm just looking for something that works. Right
now, the only thing that works is not to do the
update at all, but instead do delete/insert. I'd
like to find a better solution than that.
-Daniel E. Hale -
Update a column using a connect by or other for hierachical relationships
I have a column which represents the 'order' of which a record loaded out of a table.
This 'order' is coming in wrong.
I know, because of a relatoinship between two of the columns what the correct order should be.
So for example if I do this:
select transid, laborcode, supervisorfrom enclabor_iface
connect by prior laborcode = supervisor
start with supervisor = 0;
I get all the records in the correct order... but of course I'd like to figure out how to use this in an update statement to update the transid columns, which is the order.
Can someone tell me if this is possible, and if so, how to do so?
I have tried a few things with no luck yet, as the attempt took over 15 minutes to run so I thought I had coded it badly.
thanks
JeffHi, Jeff,
Sure, that's possible.
The ROWNUM pseudo-column is assigned as the CONNECT BY clause (including ORDER SIBLINGS BY) is being applied, so you can use ROWNUM to capture the hierarchical order.
I don't have a copy of your table, so I'll use scott.emp to illustrate:
SELECT ename, empno, mgr
, ROWNUM AS r_num
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY mgr = PRIOR EMPNO
ORDER SIBLINGS BY ename
;Output:
ENAME EMPNO MGR R_NUM
KING 7839 1
BLAKE 7698 7839 2
ALLEN 7499 7698 3
JAMES 7900 7698 4
MARTIN 7654 7698 5
TURNER 7844 7698 6
WARD 7521 7698 7
CLARK 7782 7839 8
MILLER 7934 7782 9
JONES 7566 7839 10
FORD 7902 7566 11
SMITH 7369 7902 12
SCOTT 7788 7566 13
ADAMS 7876 7788 14The default CONNECT BY ordering guarantees that (for example) all of BLAKEs descendants will come after BLAKE and before anyone who is not a descendant of BLAKE. However, it says nothing about whether BLAKE will come before JONES, or vice-versa. If that's important, use ORDER SIBLINGS BY.
To store those numbers in your table, do the query above in a MERGE statement.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
If you're asking about a DML statement, such as MERGE, the sample data will be the contents of the table(s) before the DML, and the results will be state of the changed table(s) when everything is finished.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Edited by: Frank Kulash on Feb 7, 2012 11:46 AM -
Can we use both INSERT and UPDATE at the same time in JDBC Receiver
Hi All,
I would like to know is it possible to use both INSERT and UPDATE at the same time in one interface because I have a requirement in which I have to perform both the task.
user send the file which contains both new and old record and I need to save those in MS SQL database.
If the record exist then use UPDATE otherwise use INSERT.
I looked on sdn but didn't find any blog which perform both the things at the same time.
Interface Requirement
FILE -
> PI -
> JDBC(INSERT & UPDATE)
I am thinking to use JDBC Lookup but not sure if it good to use for bulk record.
Can somebody please suggest me something or send me the link of any blog or anything to solve this problem.
Thanks,Hi ,
If I have understood properly the scenario properly,you are not performing insert and update together. As you posted
"If the record exist then use UPDATE otherwise use INSERT."
Thus you are performing either an insert or an update which depends on outcome of a search if the records already exist in database or not. Obviously to search the tables you need " select * from ... where ...." query. If your query returns some results you proceed with update since this means there are some old records already in database. If your query returns no rows you proceed with "insert into tablename....." since there are no old records present in database.
Now perhaps the best method to do the searching, taking a decision to insert or update, and finally insert or update operation is to be done by a stored procedure in MS SQL database. A stored procedure is a subroutine available to applications accessing a relational database system. Here the application is PI server. If you need further help on how to write and call stored procedure in MS SQL you can look into these links
http://www.daniweb.com/web-development/databases/ms-sql/threads/146829
http://www.sqlteam.com/article/stored-procedures-parameters-inserts-and-updates
[ This part you can ignore, Since its not sure that you will face this situation
Still you might face some problems while your scenario runs. Lets consider this scenario, after the stored procedure searches the database it found no rows. Thus you proceed with an insert operation. If your database table is being accessed by multiple applications (or users) other than yours then it is very well possible that after the search operation completed with a null result, an insert/update operation has been performed by some other application with the same primary key. Now when you are trying to insert another row with same primary key you get an error message like "duplicate entry not possible for same primary key value". Thus you need to be careful in this respect. MS SQL has a feature called "exclusive locks ". Look into these links for more details on the subject
http://msdn.microsoft.com/en-us/library/aa213039(v=sql.80).aspx
http://www.mssqlcity.com/Articles/Adm/SQL70Locks.htm
http://www.faqs.org/docs/ppbook/r27479.htm
http://msdn.microsoft.com/en-US/library/ms187373.aspx
http://msdn.microsoft.com/en-US/library/ms173763.aspx
http://msdn.microsoft.com/en-us/library/e7z8d5hf(v=vs.80).aspx
http://mssqlserver.wordpress.com/2006/11/08/locks-in-sql/
http://www.mollerus.net/tom/blog/2008/03/using_mssqls_nolock_for_faster_queries.html
There must be other methods to avoid this problem. But the point is you need to be sure that all access to database for insert/update operations are isolated.
regards
Anupam -
Update some columns using case....
Hi ,
Is it possible to update some columns using case statement...????
For example when col1 is null then update to a value 'x' else update it to the value '*' , when col2 is null then update to a value y else update it to compute the running total up to that time....
This update statement is contained in db packaged procedure and it receives the values...as parameters....
How can i write down this update statement...?????
Many thanks,
SimonHi ,
Cant' it be used for two or more columns that have to be updated....????
i mean
update table set col_a = case when col_a is null then col_a else '*',
col_b = case when col_b is null then col_b else col_b+col_b_var
end
where .....The above in bold is running total.... This update is defined in a procedure and it receives numbers as parameters, so the need is to add them for every record it receives...., that's why i set above col_b+col_b_var... where col_b_var is the parameter of the procedure....
SORRY...IT IS POSSIBLE.....
Thanks , a lot
Simon
Message was edited by:
sgalaxy -
I have to migrate data from one table to another where one of the columns is the same in both tables. e.g. ID COLUMN
Is there a way to migrate data such that I can update all rows in the ID column in table 2 with corresponding values from ID column in table 1 using one update statement?
i.e.
TABLE 1 || TABLE 2
ID || ID
2 || 5
5 || 9
8 || 4
34 || 98
67 || 762
89 || 99
I want to use 1 update statement if possible to update all rows in ID column table 2 with values in ID column in table 1
Thanks
Edited by: 331991 on Feb 4, 2010 11:04 PMHi,
You can do this by using cursor.
At First you have to save the id mapping relationship in a temp table, for example MAPPING_TABLE.
Then you can do this as follow:
Declare
Cursor TO_UPDATE IS
SELECT NEW.ID MEW_ID, NEW.COL1, NEW COL2,...., MAP.OLD_ID
FROM NEW_TABLE NEW,
MAPPING_TABLE MAP
WHERE NEW.ID=MAP.NEW_ID
ORDER BY OLD_ID;
Begin
FOR REC IN TO_UPDATE LOOP
UPDATE OLD_TABLE
SET ID=REC.NEW_ID,COL1=REC.COL1, COL2=REC.COL2, ....
WHERE ID=REC.OLD_ID;
END LOOP;
COMMIT;
End; The better way, that you may let a column for control issue.
IF ATTRIBUTE1 is NULL for EACH ROW
Declare
Cursor TO_UPDATE IS
SELECT NEW.ID MEW_ID, NEW.COL1, NEW COL2,...., MAP.OLD_ID
FROM NEW_TABLE NEW,
MAPPING_TABLE MAP
WHERE NEW.ID=MAP.NEW_ID
ORDER BY OLD_ID;
Begin
FOR REC IN TO_UPDATE LOOP
UPDATE OLD_TABLE
SET ID=REC.NEW_ID,COL1=REC.COL1, COL2=REC.COL2, ...., ATTRIBUTE1 = REC.OLD_ID
WHERE ID=REC.OLD_ID
AND ATTRIBUTE1 IS NULL;
END LOOP;
COMMIT;
End; Best regards,
Zhxiang
Edited by: zhxiangxie on Feb 5, 2010 12:19 PM -
Update order document through DI API generates delete / insert ?
Hi,
I am developing with B1 88 PL10 and I am doing an update of userfields with DI API.
Looking on sql server profiling tool I found that the DI API generates delete / insert instead of update statements ?
Has someone an explanation ?
Regards,
GregoryHi Gregory,
There are couple of possible reasons for this. I think the most probable one is for performance and accuracy.
Delete / insert may actually take less time than Update alone.
Thanks,
Gordon -
How to update a column using hibernate
how to update a particular column using hibernate..
iam using oracle databaseI think you didn't get the point. This is a generic Java forum... not a Hibernate forum.
-
An update statement to use cumulative columns from the previous record
I need to create a update statement which updates a record based on a column from previous record and its column where they are grouped by another columns and ordered by date in ASC. Note that I need answer from SQL 2005+.
Suppose I have records:
TransactionID ProductID TransactionDate Quantity QuantityOnHand
1 1 1/2/2014
2 ? 2
2 1 1/3/2014
3 ? 5 = 3+2
3 1 1/4/2014
1 ? 6 = 5 + 1
4 1 1/5/2014
9 ? 15 = 6 + 9
I wrote this statement but did not work:
UPDATE it2 SET it2.QuantityOnHand =it2.Quantity + ISNULL(it1.QuantityOnHand,0)
FROM IT it1
LEFT JOIN (SELECT TransactionID,ProductID, Quantity,QuantityOnHand FROM IT GROUP BY ProductID ORDER BY TransactionDate) it2 ON It2.TransactionID>it1.TransactionID
WHERE It2.ProductID=it1.ProductID
This means update the record and get the QuantityOnHand from its previous record if any grouped by the ProductID that are ordered by TransactionDate. Note that in this example there is only one product ID. Here the QuantityOnHand column should be updated
and TransactionDate is in ordered!
Mike
DDL:
CREATE TABLE [dbo].[IT](
[TransactionID] [int] NOT NULL PRIMARY KEY,
[ProductID] [int] NULL,
[TransactionDate] [datetime] NULL,
[Quantity] [float] NULL,
[QuantityOnHand] [float] NULL
) ON [PRIMARY]
DML:
INSERT INTO [dbo].[IT] VALUES (1 ,1,'1/2/2014',2,NULL)
INSERT INTO [dbo].[IT] VALUES (2,1,'1/3/2014',4,NULL)
INSERT INTO [dbo].[IT] VALUES(3 ,1 ,'1/4/2014' ,2,NULL)
INSERT INTO [dbo].[IT] VALUES(4 ,1 ,'1/5/2014' ,9,NULL)Hi
please replace this text (which is not records but just text, and therefore we can not query it):
TransactionID ProductID Quantity QuantityOnHand
1 1
2 ? 2
2 1
3 ? 5 = 3+2
3 1
1 ? 6 = 5 + 1
4 1
9 ? 15 = 6 + 9
with a DDL+DML queries to help us help you.
>> DDL+DML are queries to create your relevant table and a query to insert the sample data.
Thanks
[Personal Site] [Blog] [Facebook]
Maybe you are looking for
-
Newbie, How Do you transfer a site from Remote Host to Local Web Server
already exists to my local IIS server. I cannot figure it out. I keep going in circles with Manage Sites, creating the folder in the Inetpub/wwwroot folder and I need some help to get the whole site locally so I can work on it. I use to use Frontpage
-
I want to delete all my email accounts
Since I upgraded my Ipad2 I only receive mails but cannot send any. I tried to delete the email accounts in the Mail app through the "Delete Account" option but after pressing it, a message confirming that the software is "deleting..." but this task
-
Problem with finding BADI for transaction F9K2
I Found many BADIs for the transaction...but am not able to decide which is suitable for my requirement.. my requirement is ..i need to keep check box removing radiobutton in the the transctio F9K2 in tab 'accounts statements' in Bk.st.orig column.
-
Additonal customs duty(ADC) in import
Hi, We have scenario of import capital goods. We have availed cenvat credit on bill of entry but the system is taking 50% credit of 14%,2% & 1% amount correctly but against 4% ADC(Additional duty of customs) it is availing 100% . Our client reqiurem
-
Using an old imac as a "dumb" terminal
How do I turn on an old computer so it is a "dumb" terminal? It is connected to the new one with firewire, but I have forgotten what keys to hold down on the startup of the old one.