So we’re told that Stored Procs are optimized and we should NEVER be putting sql statements into code.
But I don’t want to make 10,000 stored procedure for every single type of query or db manipulation that I need.
So I’ve started doing something like this (putting all functions into a single sproc):
CREATE PROCEDURE [dbo].[spTABLENAME]
@Function nvarchar = null,
@ID int = null,
@MoreVariables int = null
AS
BEGIN
SET NOCOUNT ON;
IF @Function = 'UPDATE'
BEGIN
UPDATE UPDATESTUFF WHERE ID = @ID;
END
ELSE IF @Function = 'INSERT'
BEGIN
INSERT INTO TABLENAME (STUFF)
END
ELSE IF @Function = 'SELECT'
BEGIN
SELECT * FROM TABLENAME WHERE ID= @ID
END
ELSE IF @Function = 'DELETE'
BEGIN
DELETE * FROM TABLENAME WHERE ID = @ID
END
END
Can someone tell me if there is anything WRONG with doing things this way?
It’s fairly common to use a single “UPSERT” procedure, but one that also can potentially return data doesn’t feel right to me…
Another thing to keep in mind is that if you use individual stored procs, you can get some extra benefit from cache plans and stuff that you might not benefit from with an all-in-one routine.