March 3, 2010 at 1:02 pm
Hi ,
Here is the update statement:
update set
deletedate=getdate()
from
dbo.A
where
deletedate is null
and UserID in (select UR.UserID from
dbo.B UR
inner join dbo.C R on R.RoleID = UR.RoleID
where UR.DeleteDate is null and RCode in ('SN','CM','DI','MC','ES','IS','SU','FE','RCM'))
Now all i want to do is delete everything which is related to Table A
i.e. i have Tables e,f,g,h,i which has userID columns to join with table A and since i am updating Table A before updating i want to delete corresponding rows in e,f,g,h,i tables.
UserId column is the primary key in Table A and Foreign key in tables e,f,g,h,i ...
March 3, 2010 at 3:29 pm
Hi,
I have created a procedure to do this and created a cursor inside a procedure.
March 3, 2010 at 10:27 pm
itskumar2004 (3/3/2010)
Hi ,Here is the update statement:
update set
deletedate=getdate()
from
dbo.A
where
deletedate is null
and UserID in (select UR.UserID from
dbo.B UR
inner join dbo.C R on R.RoleID = UR.RoleID
where UR.DeleteDate is null and RCode in ('SN','CM','DI','MC','ES','IS','SU','FE','RCM'))
Now all i want to do is delete everything which is related to Table A
i.e. i have Tables e,f,g,h,i which has userID columns to join with table A and since i am updating Table A before updating i want to delete corresponding rows in e,f,g,h,i tables.
UserId column is the primary key in Table A and Foreign key in tables e,f,g,h,i ...
Is there a reaon why you don't just write separate deletes for each table?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2010 at 5:24 am
itskumar2004 (3/3/2010)
execute sp_executesql @ExecSql,@ExecSql1,@ExecSql2,@ExecSql3,@ExecSql4,@ExecSql5,@ExecSql6,@ExecSql7,@Execsql8--print @ExecSql,@ExecSql1,@ExecSql2,@ExecSql3,@ExecSql4,@ExecSql5,@ExecSql6,@ExecSql7
i cant say where the exactly problem lies but you can see where the problem occuring by print "sp_executesql" , see if it can help you.
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 4, 2010 at 6:27 am
Jeff Moden (3/3/2010)
itskumar2004 (3/3/2010)
Hi ,Is there a reaon why you don't just write separate deletes for each table?
Yes,each procedure i am calling will insert the row into history table and update the table i want to delete..
i.e. tables e,f,g,h,i,j,k has history tables like ehistory,fhistory etc....
March 4, 2010 at 7:31 am
Hi ,
I found where i am getting error ,for each and every userid the below set statement is getting more than one row.
i.e. for userid =1 the usertoterritory table is returning more than one row .similarly the other set statements...
can anyone give me any ideas to raplace equal to for the below set statement...
Set @UserToTerritoryID = (select UserToTerritoryID from dbo.tUserToTerritory where UserID=@UserID)
Set @UserToRegionID=(select UserToRegionID from dbo.tUserToRegion where UserID=@UserID)
Set @UserToAreaID=(select UserToAreaID from dbo.tUserToArea where UserID=@UserID)
Set @UserToCorporateGroupID=(select UserToCorporateGroupID from dbo.tUserToCorporateGroup where UserID=@UserID)
Set @UserToBusinessUnitID=(select UserToBusinessUnitID from dbo.tUserToBusinessUnit where UserID=@UserID)
Set @UserToPortfolioID=(select UserToPortfolioID from dbo.tUserToPortfolio where UserID=@UserID)
Set @UserToMarketID=(select UserToMarketID from dbo.tUserToMarket where UserID=@UserID)
Set @UserToChannelID=(select UserToChannelID from dbo.tUserToChannel where UserID=@UserID)
March 4, 2010 at 1:06 pm
I have done it by creating cursors inside a cursor..
i got it.
March 4, 2010 at 1:23 pm
itskumar2004 (3/4/2010)
I have done it by creating cursors inside a cursor..i got it.
Damn, I felt Jeff almost got a Heart attack :w00t: Are you sure there is no way to write this solution as a set based?
Cheers,
J-F
March 5, 2010 at 11:30 am
I don't know i need to look in google or some articles.
March 5, 2010 at 8:11 pm
J-F Bergeron (3/4/2010)
itskumar2004 (3/4/2010)
I have done it by creating cursors inside a cursor..i got it.
Damn, I felt Jeff almost got a Heart attack :w00t: Are you sure there is no way to write this solution as a set based?
Heh... saw that and went out for a couple of beers in hopes that it would cure the sudden blindness imparted to my eyes. 😛 Yowch!
--Jeff Moden
Change is inevitable... Change for the better is not.
March 5, 2010 at 8:14 pm
itskumar2004 (3/5/2010)
I don't know i need to look in google or some articles.
So what have you Googled for, so far?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2010 at 12:47 pm
Set @UserToTerritoryID = (select UserToTerritoryID from dbo.tUserToTerritory where UserID=@UserID)
i have used select instead of set like and called the variable in a procedure
select @usertoterritoryid=usertoterritoryid
from tusertoterritory
where userid=@userid
@sql='exec '+ 'dbo.procedurename ''' + cast(@usertoterritoryid as varchar(10)) +'''
The procedure updates tusertoterritory table based on @usertoterritoryid.
This time i am not getting any errors ,but the query is not doing what it is supposed to do ,i think its because for each and every userid the table tusertoterritory has more than one usertoterritoryid i.e for userid 1 it has 1,2,3,4,5,6 usertoterritoryid's and it is updating only one usertoterritoryid.
Can you explain in detail why i should not use cursors...
March 12, 2010 at 8:43 am
Cursors tend to be a very slow way of updating or deleting multiple rows. Sometimes there isn't any other way but if there is then not using a cursor is frequently better.
Is it possible to to the updates in the same procedure that you're pulling @usertoterritoryid? If that's the case you can just join onto tusertoterritory.
March 22, 2010 at 2:00 pm
I am sorry for the late reply...
Anyway, i have replaced the cursors with while loop .
here you go ,
Create procedure procedurename
(
@columnname int,
)
as
Declare @column1ID int;
Declare @sql nvarchar(200);
Declare @e int;
Declare @f int;
set @f=(select count(*) from tableA where columnname=@columnname and Date is null)
set @e=1;
while (@e<=@f)
begin
select @column1ID=column1ID from table A where columnname=@columnname and Date is null
select @sql='exec '+ 'dbo.procedurename ''' + ''','''+cast(@column1ID as varchar(50)) + ''''
Execute Sp_executesql @sql
set @e=@e+1;
End --While
end--stored procedure
this is the while loop i have used in my stored procedure.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply