I am trying to insert data into one table, select from another table based on if condition but is not working for either if condition. Parameteres are provided corectly, I've checked that. What am I doing wrong?
SQL
CREATE PROCEDURE [dbo].[AddTestDetail]
@IdUserTest AS int,
@Complexitate AS nvarchar
AS
BEGIN
IF (@Complexitate = 'GENERAL')
BEGIN
INSERT INTO tblTestDetail (IdIntrebare, IdUserTest)
SELECT ti.IdIntrebare AS IdIntrebare, @IdUserTest
FROM (SELECT TOP 10 * FROM tblIntrebare ORDER BY NEWID()) ti JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura
WHERE tp.Sectiune = 'GENERAL';
END
IF (@Complexitate = 'SPECIFIC')
BEGIN
INSERT INTO tblTestDetail (IdIntrebare, IdUserTest)
SELECT ti.IdIntrebare AS IdIntrebare, @IdUserTest
FROM (SELECT TOP 10 * FROM tblIntrebare ORDER BY NEWID()) ti JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura
WHERE tp.Sectiune = 'GENERAL';
INSERT INTO tblTestDetail (IdIntrebare, IdUserTest)
SELECT ti.IdIntrebare AS IdIntrebare, @IdUserTest
FROM (SELECT TOP 10 * FROM tblIntrebare ORDER BY NEWID()) ti JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura
WHERE tp.Sectiune = 'SPECIFIC';
END
END

Anupam MaitiRank #295 · 8.5K reputationPosted Sep 17, 2023, 2:56 AM EDT
The SELECT statements for both 'GENERAL' and 'SPECIFIC' conditions are the same in the second block. Can you try below?
CREATE PROCEDURE [dbo].[AddTestDetail] @IdUserTest INT, @Complexitate NVARCHAR(50) AS BEGIN IF (@Complexitate = 'GENERAL') BEGIN INSERT INTO tblTestDetail (IdIntrebare, IdUserTest) SELECT TOP 10 ti.IdIntrebare, @IdUserTest FROM tblIntrebare ti INNER JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare INNER JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Sectiune = 'GENERAL'; END ELSE IF (@Complexitate = 'SPECIFIC') BEGIN INSERT INTO tblTestDetail (IdIntrebare, IdUserTest) SELECT TOP 10 ti.IdIntrebare, @IdUserTest FROM tblIntrebare ti INNER JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare INNER JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Sectiune = 'SPECIFIC'; END ENDIf it is working, Please accept my answer.
Marius VasileRank #785 · 2.9K reputationPosted Sep 17, 2023, 3:00 AM EDT
Thank you Anupam,
For the second block I try to insert 10 from GENERAL and 10 from SPECIFIC. And it should be random questions that's why I used ORDER BY NEWID()
Using only TOP 10 gives me first 10 questions in the table that meets condition, right?
Anupam MaitiRank #295 · 8.5K reputationPosted Sep 17, 2023, 3:02 AM EDT
Added the correct data type for the
@Complexitateparameter. Can you try with this.Marius VasileRank #785 · 2.9K reputationPosted Sep 17, 2023, 3:15 AM EDT
I tried with your suggestion. The problem I have now is that
SELECT ti.IdIntrebare AS IdIntrebare, @IdUserTest FROM (SELECT TOP 10 * FROM tblIntrebare ORDER BY NEWID()) ti JOIN tblRaspuns tr ON ti.IdIntrebare = tr.IdIntrebare JOIN tblProcedura tp ON ti.IdProcedura = tp.IdProcedura WHERE tp.Sectiune = 'GENERAL';is not returning 10 questions. In the table I have almost 50 questions. Why?