if you don't get SQL, get the hell out while you still can
I was given an excel sheet of Q&A's to populate previously defined tables.
all well and good, but the tables are split up. 1 for questions, and 1 for answers. So, if i simply inserted the records, i would loose the coorelation between the two tables.
first step, import the Q&A file into a table called cv_res_ques2
id, question, answer are the 3 fields. 82 total rows.
Now, if i just simply imported these, i would loose the relational nature of the answer to each question--- because there CAN be more than 1 answer associated with each question. (one to many).
I needed to create a cursor loop that would insert the question, then the answer with respect to the question based off of the insertation id.
here's the SQL code i used to come up with it.
looped through 82 times, and voila! i hate 2 tables, relational, and my Q&A website is fully functional
are you a nerd?
I was given an excel sheet of Q&A's to populate previously defined tables.
all well and good, but the tables are split up. 1 for questions, and 1 for answers. So, if i simply inserted the records, i would loose the coorelation between the two tables.
first step, import the Q&A file into a table called cv_res_ques2
id, question, answer are the 3 fields. 82 total rows.
Now, if i just simply imported these, i would loose the relational nature of the answer to each question--- because there CAN be more than 1 answer associated with each question. (one to many).
I needed to create a cursor loop that would insert the question, then the answer with respect to the question based off of the insertation id.
here's the SQL code i used to come up with it.
Code:
declare @ques varchar(4000)
declare @ans varchar(4000)
declare @qid int
declare @id_list int
declare my_cursor cursor for
SELECT [id] AS 'id_list' FROM cv_res_ques2
OPEN my_cursor
FETCH NEXT FROM my_cursor into @id_list
while @@fetch_status = 0
begin
set @ques = (select question from cv_res_ques2 where id = @id_list)
set @ans = (select answers from cv_res_ques2 where id = @id_list)
insert into questions
(requestdate, requestbyID, office, assignedto, product, question,
qstatus, closeddate, closedbyid, archiveflag)
values(
'11/03/2005 12:00:00 PM', 32, 'Company', 32, NULL, @ques,
'RESOLVED', '11/02/2005 12:00:01 PM', 32, NULL
)
select @qid = @@identity
insert into answers
(qid, AnswerDate, answerbyid, answer)
values(@qid, '11/03/2005 12:01:00 PM', 32, @ans)
FETCH NEXT FROM my_cursor into @id_list
end
CLOSE my_cursor
DEALLOCATE my_cursor
looped through 82 times, and voila! i hate 2 tables, relational, and my Q&A website is fully functional
are you a nerd?