Author |
Topic |
acribb
Starting Member
19 Posts |
Posted - 2008-06-19 : 16:29:22
|
I need a query that will update only 99 records per user.I have a specific list of users I need to update records for, so the end of the statement will be - "where username in ('123', '124', '125')".Thanks for any help.... |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 02:28:05
|
Is there any field in table which has unqiue values for a given user? |
 |
|
acribb
Starting Member
19 Posts |
Posted - 2008-06-20 : 08:43:07
|
The records I am querying have a user name field which I need to match up to my list of " where username in ('123', '1234', ..)" All of the users will have more than 100 records, so this will not be a problem. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 09:35:20
|
quote: Originally posted by acribb The records I am querying have a user name field which I need to match up to my list of " where username in ('123', '1234', ..)" All of the users will have more than 100 records, so this will not be a problem.
Nope i was asking if your table contain any field which will have unique values for a particular value of username. |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
acribb
Starting Member
19 Posts |
Posted - 2008-06-20 : 10:15:38
|
I am thinking the answer is no.table a-------idusernameaddresscity statelastmodifieddateEach of my usernames has over a 100 records in table a. For my test, I only need to update the lastmodifieddate, but only for 99 records of each username in my list.Does this make more sense? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 10:26:05
|
quote: Originally posted by acribb I am thinking the answer is no.table a-------idusernameaddresscity statelastmodifieddateEach of my usernames has over a 100 records in table a. For my test, I only need to update the lastmodifieddate, but only for 99 records of each username in my list.Does this make more sense?
is id an identity field? |
 |
|
acribb
Starting Member
19 Posts |
Posted - 2008-06-20 : 11:19:53
|
Yes. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 11:25:41
|
quote: Originally posted by acribb Yes.
Update tset t.Field1=value1,t.Field2=value2,..FROM (SELECT (SELECT COUNT(*) FROM YourTable WHERE username =t.username AND id <t.id) + 1 AS Seq,*FROM YourTable t WHERE t.username in ('123','124','125'))tWHERE t.Seq<100 |
 |
|
acribb
Starting Member
19 Posts |
Posted - 2008-06-20 : 13:24:26
|
Actually this doesn't work - I get the following error -37000(4421)[Microsoft][ODBC SQL Server Driver][SQL Server]Derived table 't' is not updatable because a column of the derived table is derived or constant.37000(8180)[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (0.02 secs) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 14:24:31
|
quote: Originally posted by acribb Actually this doesn't work - I get the following error -37000(4421)[Microsoft][ODBC SQL Server Driver][SQL Server]Derived table 't' is not updatable because a column of the derived table is derived or constant.37000(8180)[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (0.02 secs)
then try puttuing results onto temp table and updatingSELECT (SELECT COUNT(*) FROM YourTable WHERE username =t.username AND id <t.id) + 1 AS Seq,id,username,address,city, state INTO #TempFROM YourTable t WHERE t.username in ('123','124','125')UPDATE tSET t.field1=value1,t.field2=value2,...FROM YourTable tINNER JOIN #Temp tmpON tmp.id=t.idAND tmp.username=t.usernameDROP TABLE #TempWHERE tmp.Seq<100 |
 |
|
acribb
Starting Member
19 Posts |
Posted - 2008-06-20 : 16:03:52
|
I'm getting the following error -Incorrect syntax near the keyword 'WHERE'.on "WHERE tmp.Seq<100" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-20 : 19:08:13
|
quote: Originally posted by acribb I'm getting the following error -Incorrect syntax near the keyword 'WHERE'.on "WHERE tmp.Seq<100"
You should not use where clause to drop a tableIt is by mistake given by Visakh.MadhivananFailing to plan is Planning to fail |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-21 : 01:44:41
|
quote: Originally posted by madhivanan
quote: Originally posted by acribb I'm getting the following error -Incorrect syntax near the keyword 'WHERE'.on "WHERE tmp.Seq<100"
You should not use where clause to drop a tableIt is by mistake given by Visakh.MadhivananFailing to plan is Planning to fail
Ah my copy paste is awful . i overlapped the two statements what i meant was this:-UPDATE tSET t.field1=value1,t.field2=value2,...FROM YourTable tINNER JOIN #Temp tmpON tmp.id=t.idAND tmp.username=t.usernameWHERE tmp.Seq<100DROP TABLE #Temp Thanks for spotting it Madhi |
 |
|
acribb
Starting Member
19 Posts |
Posted - 2008-07-03 : 15:38:23
|
Works perfectly! Thanks for the help everyone... |
 |
|
|