| Author |
Topic |
|
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2012-10-25 : 03:41:21
|
| Hi All,Below is the sample code i was trying and my scenario is how can the inserted data is rollbacked?i used cursor for validation (comaparion of task in @temp table with @task table) In output,the 2nd row in @temp does not satisfy the taskname in @task tableso I want to rollback the 1st inserted data.declare @pcode varchar(100)declare @rnum intdeclare @tname varchar(100)declare @temp table(projectcode varchar(100),revisionnum int,taskname varchar(100))declare @temp2 table(projectcode varchar(100),revisionnum int,taskname varchar(100))insert @tempselect 'P1',1,'CM' union allselect 'P1',1,'M' union allselect 'P1',1,'MM'declare @task table(taskid int,taskname varchar(100),projectcode varchar(100),revisionnum int)insert @taskselect 1,'CM','P1',1 UNION ALLselect 2,'PM','P1',1 UNION ALLselect 3,'MM','P1',1select * from @tempselect * from @taskdeclare curname cursorfor select * from @tempopen curnamefetch next from curname into @pcode,@rnum,@tnamewhile @@fetch_status=0begin if exists(select 1 from @task where taskname=@tname and projectcode=@pcode and revisionnum=@rnum) begin begin tran t1 insert @temp2 select * from @temp where projectcode=@pcode and revisionnum=@rnum and taskname=@tname end else begin rollback break; end fetch next from curname into @pcode,@rnum,@tnameendclose curnamedeallocate curnameselect * from @temp2 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-25 : 04:45:02
|
| For what you have thereinsert @temp2 select t1.projectcode, t1.projectcode, t1.tasknamefrom @temp t1join @task t2on t1.projectcode = t2.projectcodeand t1.taskname = t2.tasknameand t1.revisionnum = t2.revisionnumNo need for cursor or transactionBetter to not insert rather than rollback. With a temp table you would probably delete or mark as deleted if you wanted to do this.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2012-10-25 : 05:12:37
|
| i want to get all the records in @temp only if all rows in @temp satisfy the condition of valid task in @taskelse there should be no any row inserted in @temp2.so as i used cursor for validating each row,1st row is get inserted into @temp2so i was thinking it could be rollbacked ? |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-25 : 05:38:18
|
| In that caseinsert @temp2select t1.projectcode, t1.projectcode, t1.tasknamefrom @temp t1where not exists(select *from @templeft join @task t2on t1.projectcode = t2.projectcodeand t1.taskname = t2.tasknameand t1.revisionnum = t2.revisionnumwhere t2.projectcode is null)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-25 : 05:38:48
|
| Hi,One thing you can do is:while @@fetch_status=0beginif exists(select 1 from @task where taskname=@tname and projectcode=@pcode and revisionnum=@rnum)beginbegin tran t1insert @temp2select * from @temp where projectcode=@pcode and revisionnum=@rnum and taskname=@tnameendelsebeginrollbackdelete from @temp2break;endfetch next from curname into @pcode,@rnum,@tnameend--Chandu |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-25 : 05:47:50
|
| Get rid of the transaction - it doesn't do anything.Better to just insert if all rows fulfill the requirement as in my previous post.Why insert just to delete later? (And why have a cursor?)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-25 : 05:52:19
|
| As of now i provided temporary solution for that ...I think he is doing some R&D....--Chandu |
 |
|
|
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2012-10-25 : 06:30:03
|
| @tempprojectcode revisionnum tasknameP1 1 CMp1 1 PMp1 1 MM@tasktaskid projectcode revisionnum taskname1 P1 1 CM2 p1 1 PM3 p1 1 MMbut nigel your query doesnt fills the data fo above conditionplz have a look if i am not right. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-10-25 : 07:31:19
|
| It should - there are a couple of typos which I guess you found - maybe you corrected them incorrectly :).insert @temp2 (projectcode, revisionnum, taskname)select projectcode, revisionnum, tasknamefrom @tempwhere not exists(select *from @temp t1left join @task t2on t1.projectcode = t2.projectcodeand t1.taskname = t2.tasknameand t1.revisionnum = t2.revisionnumwhere t2.projectcode is null)I'm also guessing that you want it per project rather than for the whole table? If so make the not exists bit a correlated subquery.Don't try to overcomplicate it - it's a pretty simple requirement.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Vishal_sql
Posting Yak Master
102 Posts |
Posted - 2012-10-25 : 10:27:30
|
| Thanks Nigel.Its great solution over the cursor. |
 |
|
|
|