Help with Cursors or query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Help with Cursors or query

Hi.
I’m trying to select data from a row in a table, add a string to that and insert the altered data into another field in the same row.
Current table
ID (primary key)
NAME (FRIST NAME)
ADDRESS (STREET ADDRESS)
Combo (NULL) DESIRED RESULT
ID (primary key)
NAME (FRIST NAME)
ADDRESS (STREET ADDRESS)
Combo (ID + NAME) So I need a query that will first select the values and then insert the combined values into the combo field and do that for every row in the table. Any help would be greatly appreciated.
Thanks.

update yourtable
set Combo = convert(varchar(15),ID) + NAME
where …
You can even add a new computed column in your table which will be computed on every insert.
Modify your current table like this: CREATE TABLE table_name
(
ID int,
NAME varchar(50),
ADDRESS varchar(100),
Combo AS(CONVERT(VARCHAR(10),ID)+NAME)
)
Indeed a computed column could server as well.
One should think about that….
How about using it when selection? Select cols, CONVERT(VARCHAR(10),ID)+NAME as new_col from table Madhivanan Failing to plan is Planning to fail
Please check if this works for you.. I have created a sample based on data: Create table sql
(
ID int primary key clustered,
NAME varchar(100),
ADDRESS varchar(1000),
Combo varchar(1000) NULL
)
go
insert into SQL(id,name,address) values(1,’Guru’,’Bangalore’)
go begin tran
update A
set Combo = cast(a.ID as varchar(100))+’ ‘ + a.NAME
from
SQL a join SQL B
on
a.id = b.id
and
a.Name = b.Name
and
a.Address = B.Address
commit tran
1GuruBangalore1 Guru HTH
]]>