Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE FUNCTION F_All
	(
	 @tache int,
	 @IdEtud int,
	 @Nom varchar(20),
	 @Postnom varchar(20),
	 @Sexe varchar(1),
	 @Tel varchar(20)
	)
	returns varchar(100)
	BEGIN
	Declare @request AS varchar(100)
		if (@tache=1)
		begin
			select @request=INSERT INTO tEtudiant (Nom,Postnom,Sexe,Tel) VALUES (@Nom,@Postnom,@Sexe,@Tel)
		end
		else if (@tache=2)
		begin
			select @request=UPDATE tEtudiant SET Nom=@Nom,Postnom=@Postnom,Sexe=@Sexe,Tel=@Tel WHERE IdEtud=@IdEtud		
		end
		else if (@tache=3)
		begin
			select @request=DELETE FROM tEtudiant WHERE IdEtud=@IdEtud	
		end
		else if (@tache=4)
		BEGIN
			select @request=SELECT *FROM tEtudiant WHERE IdEtud=@IdEtud	
		END
	RETURN @request
	END


What I have tried:

Please I want to insert in my database with a function
Posted
Updated 1-Apr-21 0:33am
v2
Comments
CHill60 1-Apr-21 5:23am    
What is wrong with this code?
Richard MacCutchan 1-Apr-21 6:26am    
Just look at its name. :))
CHill60 1-Apr-21 7:56am    
Well, yeah. I did wonder if this was a troll in the making or just a "young un" getting frustrated

1 solution

You cannot do that.
Microsoft wrote:
User-defined functions cannot be used to perform actions that modify the database state.

CREATE FUNCTION (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900