Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to update column to column through any loop

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-01-16 : 05:18:41
hello all,

i have a table with huge records now i have column id users with

i lakh records and i have age column with 200 records ]

how i can update this 200 records to rest of the users

like

1 mohan 43

2 raju 45

3 kalli 43

4 asha 45

5 jisa 43

6 jaju 45

here example i have 6 records and age 43,45 need to update for all

P.V.P.MOhan

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-16 : 05:32:58
If you want to update age column ( which is NULL) with a random integer below 200

UPDATE YourTable
SET Age = CAST( RAND()*200 AS INT)
WHERE Age is NULL

--
Chandu
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-01-16 : 05:39:12
hi chandu i have some selected ID's in age column like 43,44,45 etc;

now for all users columns this 43,44,45 should be repeated...how i need to do this ???

P.V.P.MOhan
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-01-16 : 06:14:14
suggest me i am not able move a bit

P.V.P.MOhan
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-01-16 : 07:35:03
hey guys i am waiting for any suggestion??? help me out

P.V.P.MOhan
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-16 : 07:38:31
quote:
Originally posted by mohan123

hey guys i am waiting for any suggestion??? help me out
P.V.P.MOhan

Why you need that requirement?
provide expected output for us...

--
Chandu
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-16 : 07:52:16
This is not exact ur requirement:

Execute once...

declare @t tABLE( id int, users varchar(10), age int)
insert into @t
SELECT 1, 'mohan', 40 union all
SELECT 2, 'raju' ,null union all
SELECT 3, 'kalli', 43 union all
SELECT 4, 'asha', null union all
SELECT 5, 'jisa', 43 union all
SELECT 6, 'jaju', null union all
SELECT 7, 'jazxdfju', 45 union all
SELECT 8, 'jaju', null union all
SELECT 9, 'kagvewlli', 43 union all
SELECT 10, 'abxvsha', null union all
SELECT 11, 'vgsfsa', 40

;with cte AS (SELECT distinct id, age FROM @t WHERE age is NOT NULL)
UPDATE @t
SET age = c.age
FROM @t t1
cross apply (SELECT top 1 age FROM cte WHERE id >= t1.id ) c
where t1.age is null

SELECT * FROM @t


--
Chandu
Go to Top of Page
   

- Advertisement -