Click here to Skip to main content
15,923,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Here i am using the cursor in my store procedure, i am planning to take out my cursor in my SP , please give some solution how to avoid the cursor statement to normal update statement with dynamic .

Example Below:

Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'Test'(variable) and age = 18(variable)

Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'kumar'(variable) and age = 19(variable)

3 Update Tablename set columnname(variable) = value from table A join Table B on A.condition = B.Condition where name = 'babu'(variable) and age = 30(variable)

This is how my cursor will work 300 Combination dynamically pick the data from table and update into the main table

I am trying to take out the cursor , and update statement should work similar like this , instead of writing 300 update statement i want to write one update where all the 300 combination should execute.

below the code i have to bring another solution.

SQL
BEGIN DECLARE @Type VARCHAR(100)
DECLARE @TargetColumn VARCHAR(100)
DECLARE @SourceColumn VARCHAR(100)
DECLARE @SQL varchar(max)

DECLARE a_cursor CURSOR STATIC
FOR
SELECT [Type],[SourceColumn],[TargetColumn] FROM ref.tblEdsMap
GROUP BY [Type],[SourceColumn],[TargetColumn]
OPEN a_cursor
FETCH NEXT FROM a_cursor INTO @Type,@SourceColumn,@TargetColumn
WHILE @@FETCH_STATUS = 0
BEGIN

SET @SQL = 'UPDATE GCT SET GCT.' + @TargetColumn + ' = map.[TargetValue]
from EdsMap map
JOIN Table GCT
ON GCT.' + @SourceColumn + ' = map.[SourceValue]
where map.[Type]=''' + @Type + ''' and map.SourceColumn=''' + @SourceColumn+ ''''
Exec (@SQL)
PRINT @SQL
FETCH NEXT FROM a_cursor INTO @Type,@SourceColumn,@TargetColumn
END
CLOSE a_cursor
DEALLOCATE a_cursor
END
Posted
Updated 24-Jul-13 8:27am
v4
Comments
ZurdoDev 24-Jul-13 13:47pm    
If the cursor works, why change it?
Member 9781397 24-Jul-13 13:50pm    
Hi Friend, Cursor is working fine but i have 95Lak records is there in the table and i have to update 30 column with different combination so performance wise i fell very bad and am looking for better approach to avoid cursor

1 solution

hi,


see if it works fine for you...


BEGIN DECLARE @Type VARCHAR(100)
DECLARE @TargetColumn VARCHAR(100)
DECLARE @SourceColumn VARCHAR(100)
DECLARE @SQL varchar(max)
Declare @tblcount int
Declare @count int
 

SELECT ROW_NUMBER() OVER (ORDER BY [Type]) AS Row,[Type],[SourceColumn],[TargetColumn] into #temp 
FROM ref.tblEdsMap
GROUP BY [Type],[SourceColumn],[TargetColumn]


set @tblcount=(select COUNT(*) from #temp)
set @count=0

while(@count<@tblcount)
begin 

	select @Type=[Type],@SourceColumn=[SourceColumn],@TargetColumn=[TargetColumn] from #temp 
	where Row=@count;
	 
	SET @SQL = 'UPDATE GCT SET GCT.' + @TargetColumn + ' = map.[TargetValue]
	from EdsMap map
	JOIN Table GCT
	ON GCT.' + @SourceColumn + ' = map.[SourceValue]
	where map.[Type]=''' + @Type + ''' and map.SourceColumn=''' + @SourceColumn+ ''''
	Exec (@SQL)

	set @count=@count+1;
end

drop table #temp
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900