So, I tried to find a solution for this for two days and I failed to have one functioning. My query is:
string sqlQuery = "INSERT INTO tblTestDetail (IdIntrebare, IdRaspunsCorect, IdUsetTest) SELECT ti.IdIntrebare AS IdIntrebare, tr.IdRaspuns AS IdRaspunsCorect, @IdUserTest FROM (SELECT TOP5 * FROM tblIntrebare ti ORDER BY NEWID()) JOIN tblRaspuns tr ON ti.IdIntrebare=tr.IdIntrebare WHERE tr.Raspuns='DA')";
I have 3 table, tblIntrebare, tblRaspuns and tblTestDetail. tblIntrebare and tblRaspuns are related through FK IdIntrebare
tblTestDetail contains 4 fields, its own Id, FK IdUserTest, IdIntrebare and IdRaspunsCorect
What I want to do select 5 random IdIntrebare and its respective IdRaspuns from tblIntrebare JOIN tblRaspuns and INSERT INTO tblTestDetail along with IdUserTest which is always provided by
cmd.Parameters.AddWithValue("@IdUserTest", txtIdUserTest.Text);
I am using asp.net c# but is mainly sql server issue. How it should be done, please?

Mohammad HussainPosted Aug 17, 2023, 12:15 PM
Try this.
Cr BhargaviPosted Aug 17, 2023, 3:32 PM
Marius VasilePosted Aug 17, 2023, 12:46 PM
I had an error, was not Raspuns='DA' but Corect='DA' and now is working perfectly. Thank you very much both of you
Marius VasilePosted Aug 17, 2023, 12:27 PM
Thanks again Mohammad, it simply does not add values. I am sure txtIdUserText has value. Table tblTestDetail is
Marius VasilePosted Aug 17, 2023, 11:52 AM
Thank you Mohammad, I get no error nut no data is written. Have I mentioned that all values should be int type?
I tried to convert
and use it as
but I still have no data written. My post is:
Marius VasilePosted Aug 17, 2023, 11:45 AM
Thank for the answer Abhishek, I get no error but no data is written. I will look into it more to see if I'm doing domething wrong
Mohammad HussainPosted Aug 17, 2023, 11:39 AM
Could you please try this SQL Query. I have made minor change in your query. It should work. Thanks...
INSERT INTO tblTestDetail (IdIntrebare, IdRaspunsCorect, IdUserTest)
SELECT ti.IdIntrebare AS IdIntrebare, tr.IdRaspuns AS IdRaspunsCorect, @IdUserTest
FROM (
SELECT TOP 5 * FROM tblIntrebare ORDER BY NEWID()
) ti
JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare
WHERE tr.Raspuns = 'DA';
Abhishek YadavPosted Aug 17, 2023, 11:26 AM
To achieve your goal of selecting 5 random IdIntrebare and its respective IdRaspuns from tblIntrebare and tblRaspuns, and then inserting them into tblTestDetail along with the provided IdUserTest, you can use the following SQL query:
This query performs the following steps:
Selects the top 5 random rows from tblIntrebare by using the
TOP 5clause and ordering them randomly withORDER BY NEWID().Joins the selected rows with tblRaspuns using the matching IdIntrebare values.
Filters the rows based on the condition
tr.Raspuns = 'DA'to select only the rows with Raspuns equal to 'DA'.Inserts the selected IdIntrebare, IdRaspunsCorect, and the provided IdUserTest into tblTestDetail.
Make sure to execute this SQL query using a SqlCommand object with the necessary parameter values specified.