Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update columns dynamically


update columns dynamically

Author
Message
Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2928 Visits: 4076
declare @updatedef table ( id int identity, tablename varchar(30), columnname varchar(50), Status_fl  bit)
declare @max int ,@cnt int ,@lstr varchar(200),@tablename varchar(30),@columnname varchar(50)
set nocount on

insert into @updatedef (tablename,columnname,Status_fl )
select 'emp', 'fname',1 union
select 'emp', 'age',1 union
select 'emp', 'Phone' ,1 union
select 'EmpSalary', 'Salary' ,1

select @max = MAX(id) from @updatedef
set @cnt = 1
while (@cnt < = @max)
begin
select @lstr = '',@tablename = '',@columnname = ''
select @columnname = columnname, @tablename = tablename from @updatedef
where id = @cnt and Status_fl = 1
set @lstr = 'UPDATE dbo.' + @tablename + ' SET ' + @columnname + ' = REPLACE(' + @columnname + ' ''S'', ''t'')'
print @lstr
set @cnt = @cnt + 1
end






BUT above code ..need CASE statement too to decide which columns need what kind of replace text.

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
mxy
mxy
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 2928
thanks for your help
Bhuvnesh
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
Do be careful here. The fine code that Bhuvnesh posted will have issues if you have gaps in your identity column.


declare @updatedef table ( id int identity, tablename varchar(30), columnname varchar(50), Status_fl bit)
declare @max int ,@cnt int ,@lstr varchar(200),@tablename varchar(30),@columnname varchar(50)
set nocount on

insert into @updatedef (tablename,columnname,Status_fl )
select 'emp', 'fname',1 union
select 'emp', 'age',1 union
select 'emp', 'Phone' ,1 union
select 'EmpSalary', 'Salary' ,1

--Notice what happens here when we delete a row and then insert another to generate a gap in the identity
delete @updatedef where id = 4
insert into @updatedef (tablename,columnname,Status_fl )
select 'EmpSalary', 'Salary' ,1

select @max = MAX(id) from @updatedef
set @cnt = 1
while (@cnt < = @max)
begin
select @lstr = '',@tablename = '',@columnname = ''
select @columnname = columnname, @tablename = tablename from @updatedef
where id = @cnt and Status_fl = 1
set @lstr = 'UPDATE dbo.' + @tablename + ' SET ' + @columnname + ' = REPLACE(' + @columnname + ' ''S'', ''t'')'
print @lstr
set @cnt = @cnt + 1
end



Since we are just going to build some dynamic and execute it we can avoid a loop entirely. We can also completely ignore the identity column so gaps there won't be an issue. I also added a new column to @updatedef to indicate if the column is a number type.


declare @updatedef table
(
id int identity,
tablename varchar(30),
columnname varchar(50),
Status_fl bit,
IsNumber bit
)
declare @max int ,@cnt int ,@lstr varchar(200),@tablename varchar(30),@columnname varchar(50)
set nocount on


insert into @updatedef (tablename,columnname,Status_fl, IsNumber )
select 'emp', 'fname', 1, 0 union
select 'emp', 'age', 1, 1 union
select 'emp', 'Phone' ,1, 1 union
select 'EmpSalary', 'Salary' ,1, 1

delete @updatedef where id = 4
insert into @updatedef (tablename,columnname,Status_fl )
select 'EmpSalary', 'Salary' ,1

;with UpdateValues as
(
select 'update dbo.' + tablename + ' set ' as Prefix,
STUFF((select columnname + ' = replace(' + columnname + case when IsNumber = 1 then ', 1, 2)' else ', ''S'', ''t'')' end + ', '
from @updatedef u2
where u2.tablename = u1.tablename
for XML path('')), 1, 0, '') as UpdateColumns
from @updatedef u1
group by tablename
)

select Prefix + left(UpdateColumns, LEN(UpdateColumns) - 1)
from UpdateValues



This will generate the update statements you need to execute. I would just run this, then copy the output and paste it into a new window to run it.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
mxy
mxy
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 2928
thanks Much Sean i am going to test it now and let you know
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search