Marius Vasile

Marius Vasile

  • 583
  • 1.8k
  • 138.8k

Stored procedure for inserting data into table

Sep 17 2023 6:29 AM

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? 

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

 


Answers (4)