I am having a procedure where i need to check the status in the table which is present in other server and need to update it.
Here is my stored procedure ..please check
ALTER PROCEDURE [dbo].[testproceudre]
AS
DECLARE @Status INT
DECLARE @UpdatedDateTime DATETIME
DECLARE @STSFLG INT
--dynamic query variables
DECLARE @SQLString NVARCHAR(4000)
DECLARE @ParmDefinition NVARCHAR(4000)
DECLARE @DBSTRING VARCHAR(1000)
DECLARE @TABLESTR VARCHAR(200)
BEGIN
SET @DBSTRING = '[server name].[db name]'
SET @TABLESTR = ''
DECLARE @SYSproUploadflag INT
DECLARE @DpCodeExist INT
SET @SYSproUploadflag = 0
SET @DpCodeExist = 0
SET @TABLESTR = @DBSTRING+'.[dbo].[otherservertablename]'
SET @ParmDefinition = N'@Status INT,@UpdatedDateTime DATETIME'
SET @SQLString ='SELECT StatusFlg,ConfirmedDate FROM localtablename
JOIN '+ @TABLESTR +' ON
IssuesId=SFAIssueId WHERE Statusflg=4'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@Status = @Status,
@UpdatedDateTime = @UpdatedDateTime
BEGIN
------------------------------------------------------------------------
IF @SYSproUploadflag = 0
BEGIN
DECLARE PARKCURSOR CURSOR FOR
SELECT STATUS FROM [server name].[db name].[dbo].[otherservertablename]
JOIN localtablename ON SFAISSUEID = ISSUESID
OPEN PARKCURSOR
FETCH NEXT FROM PARKCURSOR
INTO @STSFLG
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF(@STSFLG = 3)
BEGIN
UPDATE issue2 SET status = 4,UpdatedDateTime=ConfirmedDate,
SOURCEOFUPDATION = 'SFA'
FROM [server name].[db name].[dbo].[otherservertablename] issue2 JOIN
tblAHCIssues issue1
ON issue2.sfaissueid=issue1.issuesid
END
IF(@STSFLG = 2)
BEGIN
UPDATE issue2 SET status = 7,UpdatedDateTime=ConfirmedDate,
SOURCEOFUPDATION = 'SFA'
FROM [server name].[db name].[dbo].[otherservertablename] issue2 JOIN
localtablename issue1
ON issue2.sfaissueid=issue1.issuesid
END
FETCH NEXT FROM PARKCURSOR INTO @STSFLG
END
CLOSE PARKCURSOR
DEALLOCATE PARKCURSOR
END
END
END
My Prob is if the status is 2 in all rows it is updating as 7
if 3 in all rows it is updating as 4
but if some are 3 and some are 2, then the first row what is there , with respect to that only it is updating all the values.that means if 1st row is having status as 3 and second row as 2 it is updating both as 4.but it should update first as 4 and second as 7.can any one help me what mistake i am doing???
There’s no where clause in your update – each of those updates will update exactly the same set of rows (i.e. all those rows in issue2 where there is a matching issue in localtablename).
This means that the end result will depend on what the last status ID was in your cursor.