Solved

How to update the table with information of the update command written in table

Posted on 2014-03-05
2
198 Views
Last Modified: 2014-04-14
example
Table name mstupdates, which contains the fields table, field,old,new
table      field      old      new
mstchvs      aa      1      19
transbcc1       bb      32      343

table refers to table to be updated
field refers to column name
old refers to old value
new refers to new value
of the table.

How to use update command.

update table set field=nv where ov=ov
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39906231
Dynamic-SQL:
declare	@ls_SQL		varchar(1000)
,	@ls_table	varchar(100)
,	@ls_field	varchar(100)
,	@li_old		int
,	@li_new		int

declare lcur_whatever cursor for
	select	[table]
	,	[field]
	,	[old]
	,	[new]
	from	mstupdates

open lcur_whatever

while (1=1) begin
	fetch next from lcur_whatever into @ls_table, @ls_field, @li_old, @li_new
	if @@fetch_status <> 0 break

	SET @ls_SQL = 'update '+ @ls_table +' set '+ @ls_field +' = '+ Str(@li_new) +' where '+ @ls_field +' = '+ Str(@li_old)
	print @ls_SQL

	exec (@ls_SQL)
	if @@error <> 0 break
end

close lcur_whatever
deallocate lcur_whatever

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39999684
tx
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 108
CROSS APPLY 4 69
SQL query with cast 38 64
Query group by data in SQL Server - cursor? 3 85
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question