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.
| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-03-13 : 15:37:29
|
| Hi, I have a table as the one below. I have a page that brings back all the users questions. I would liek to be able to update all 5 rows in the table when the user submits their changes.How can I do this? QID(int) UserID Response (varchar 300)1 / 212 / This is my first response2 / 212 / This is my second response3 / 212 / This is my third response4 / 212 / This is my fourth response5 / 212 / This is my fifth responseThanks againMike |
|
|
Jay99
468 Posts |
Posted - 2002-03-13 : 15:45:47
|
| What does the next users answer look like in the table?1 / 212 / This is my first response2 / 212 / This is my second response3 / 212 / This is my third response4 / 212 / This is my fourth response5 / 212 / This is my fifth response1 / 213 / This is my first response2 / 213 / This is my second response3 / 213 / This is my third response4 / 213 / This is my fourth response5 / 213 / This is my fifth responseor1 / 212 / This is my first response2 / 212 / This is my second response3 / 212 / This is my third response4 / 212 / This is my fourth response5 / 212 / This is my fifth response6 / 213 / This is my first response7 / 213 / This is my second response8 / 213 / This is my third response9 / 213 / This is my fourth response10 / 213 / This is my fifth responseJay |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-03-13 : 17:21:02
|
| jay, it looks like this1 / 212 / This is my first response2 / 212 / This is my second response3 / 212 / This is my third response4 / 212 / This is my fourth response5 / 212 / This is my fifth response1 / 213 / This is my first response2 / 213 / This is my second response3 / 213 / This is my third response4 / 213 / This is my fourth response5 / 213 / This is my fifth responsethanks, mike |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-03-13 : 18:20:24
|
| You can either:create an sp to which you pass the user id, and their five responsesORcall the update code 5 times....ORdelete and re-insertegfor i = 1 to 5sSQL = "update answers set response = '" & answer(i) & "'"sSQL = " where UserId = " & iUserId & " and Qid = " & irst.Open sSQL, cnnext--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-03-13 : 18:23:39
|
| Ok I would like to know how to do it via stored procedure..How can I pass it 5 variables and have it update 5 rows at once?I dont' want to loop thru any inserts since speed is a MAJOR consideration .. thanksmike |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2002-03-13 : 18:32:29
|
in your page...sSQL = "exec spAnswers " & iUserId for i = 1 to 5sSQL = sSQL & ",'" & answer(i) & "'"next irst.Open sSQL, cnthen create your spcreate spAnswers @userid int,@answer1 nvarchar(300),@answer2 nvarchar(300),@answer3 nvarchar(300),@answer4 nvarchar(300),@answer5 nvarchar(300)asupdate answersset response = @answer1where userid = @userid and qid = 1update answersset response = @answer2where userid = @userid and qid = 2update answersset response = @answer3where userid = @userid and qid = 3update answersset response = @answer4where userid = @userid and qid = 4update answersset response = @answer5where userid = @userid and qid = 5returngoI mean, if you're really keen you could probably produce quite an elegant and generalised procedure using dynamic SQL and the whole bang - but there's no shame in having your solution implemented and working within 4 seconds either..... --I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2002-03-14 : 02:08:45
|
| thanks, worked perfectly! |
 |
|
|
joldham
Wiseass Yak Posting Master
300 Posts |
Posted - 2002-03-14 : 08:49:56
|
| Generalised, but maybe not eloquant:create spAnswers @userid int, @answers nvarchar(4000),@start_value intasDECLARE @counter intDECLARE @answer_start intDECLARE @answer_end intDECLARE @final_answer nvarchar(4000)SET @counter = 1SET @answer_start = 1SET @answer_end = CHARINDEX(',', @answers, 1)WHILE @answer_end <> 0 BEGIN SET @final_answer = LTRIM(SUBSTRING(@answers, @answer_start, (@answer_end)-(@answer_start))) UPDATE answers SET Response = @final_answer WHERE UserId = @userid AND QID = @start_value SET @counter = @counter + 1 SET @start_value = @start_value + 1 SET @answer_start = @answer_end+1 SET @answer_end = CHARINDEX(',', @answers, @answer_start+1) END SET @answer_end = (LEN(@answers)-@answer_start+1) SET @final_answer = LTRIM(SUBSTRING(@answers, @answer_start, @answer_end))goThis one lets you specify as many values (up to 4000 at a time) in a CSV format for @answer parameter, as well the starting QID.JeremyEdited by - joldham on 03/14/2002 11:42:16 |
 |
|
|
|
|
|
|
|