Combined Procedure for Triggers - sql-server

How would I create a stored procedure that is capable of taking in a flag value such as (1,2,3) where 1=insert, 2=delete, 3=update.
So for example if flag is 1, the procedure will do insert, and so forth.
I know how to create stored procedures and have already made and tested my triggers, but I can't find any examples of this and dont know where to start.
Thanks.

You can use normal If else statement:
CREATE PROCEDURE <ProcedureName> (#Flag int)
AS
BEGIN
IF(#Flag = 1)--INSERT
BEGIN
<Insert statement>
END
ELSE IF(#Flag = 2) --DELETE
BEGIN
<Delete statement>
END
ELSE IF (#Flag = 3) --UPDATE
BEGIN
<Update statement>
END
END
GO

I would use CTEs and conditions.
To give you an example of what I mean:
CREATE OR REPLACE FUNCTION public.test (_flag integer) RETURNS void AS
$func$
WITH delete AS
(DELETE FROM
atable
WHERE
_flag = 1),
insert AS
(INSERT INTO
atable (acolumn)
SELECT
'Hello World'
WHERE
_flag = 2)
UPDATE
atable
SET
acolumn = 'Hello World'
WHERE
_flag = 3;
$func$
LANGUAGE sql COST 100 STRICT;
You'll notice each statement has its own flag-checking condition.

Related

How can I increase stored procedure performance with multiple if else statement?

I have a stored procedure with multiple if-elseif- else statements. When I try to remove this if elseif statement and execute only single portion of that query, it returns results very fast, but when use this query with multiple another queries using if-elseif case statement, then it takes too much time...
For example:
if #Status = 1
begin
select .....
end
else if #Status = 2
begin
select .....
end
else if #Status = 3
begin
select .....
end
else if....
There are many more else if statements in this stored procedure..
Although it may be in some "don't do this" books, you can try to create a stored procedure for each status value and call it directly by building a dynamic TSQL statement ("exec yourproc_" + statusValue).
create proc youproc_1 as
begin
select your_data_for_status1;
end;
create proc youproc_2 as
begin
select your_data_for_status2;
end;
etc...

Procedure or function 'sp_InsertImages' expects parameter '#Img_path', which was not supplied

i have passed img_path in my code nd procedure but it show me error please anyone help me.
my procedure is :
create proc sp_InsertImages
(
#Img_path nvarchar(max),
#product_id numeric(18,0),
#IsCover_Img bit
)
as
begin
if(select COUNT(*) from tbl_productImg where product_id=#product_id)<5
begin
insert into tbl_productImg(Img_path,product_id,IsCover_Img)values(#Img_path,#product_id,#IsCover_Img)
select ##IDENTITY
end
else
begin
select -1
end
end
Your Command Type must be set to StoredProcedure like this
cmd.CommandType = CommandType.StoredProcedure;
To get the Product ID from the Procedure, call ExecuteScalar() instead of ExecuteNonQuery() like below
product_ID = Convert.ToDouble(sql.ExecuteScalar());

Sql Server Stored procedure returns -1

I am writing code on entity framework 6.0, linq on asp.net, sql server platform.
I've written a stored procedure which supposed to return an output value (0 or 1)
Firstly I wrote SP by using output parameter as follows:
ALTER proc [dbo].[checkUser]
(
#oNo int,
#pw varchar(50),
#val int output
)
as
begin
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
set #val=1
end
if not exists(select * from Users where #oNo=sNo and #pw=password)
begin
set #val=0
end
end
Didnt work. It returned -1. Then I changed SP like this:
ALTER proc [dbo].[checkUser]
(
#oNo int,
#pw varchar(50)
)
as
begin
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
return 1
end
else
begin
return 0
end
end
Each ways stored procedure returns only -1
Can you help me please, where am i doing wrong?
Try to replace "return" by "select".
if EXISTS (select * from Users where #oNo=sNo and #pw=password)
begin
select 1
end
else
begin
select 0
end

MSSQL nvarchar IF PROCEDURE exec with nvarchar input

I want know how to release IF with update and how to exec procedure with nvarchar input.
There are table Dictionary with 2 values ('Orig','Translated')
I need procedure that adds or replace "Trasnslated' depending on input. There must be 2 inputs, no more and no less. For example
CREATE PROCEDURE Translate_Orig (#Orig nvarchar(32),#Translated nvarchar(32))
AS
BEGIN
UPDATE Dictionary
IF EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=#Orig)
SET Dictionary.Translated=#Translated
ELSE INSERT INTO Dictionary VALUES (#Orig, #Translated);
END
GO
SET #Orig = N'Orig'
SET #Translated = N'traslated'
EXEC Translate_Orig (#Orig,#Translated);
CREATE PROCEDURE Translate_Orig (#Orig nvarchar(32),#Translated nvarchar(32))
AS
BEGIN
IF EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=#Orig)
BEGIN
UPDATE Dictionary
SET Dictionary.Translated=#Translated
END
ELSE INSERT INTO Dictionary VALUES (#Orig, #Translated);
END
GO
SET #Orig = N'Orig'
SET #Translated = N'traslated'
EXEC Translate_Orig (#Orig,#Translated);
Make sure you are just running the CREATE PROCEDURE through GO as a statement.
There EXEC that works fine. No brackets.
And "SET Dictionary.Translated=#Translated WHERE Dictionary.Orig=#Orig"
CREATE PROCEDURE Translate_Orig (#Orig nvarchar(32),#Translated nvarchar(32))
AS
BEGIN
IF EXISTS (SELECT * FROM Dictionary WHERE Dictionary.Orig=#Orig)
BEGIN
UPDATE Dictionary
SET Dictionary.Translated=#Translated WHERE Dictionary.Orig=#Orig
END
ELSE INSERT INTO Dictionary VALUES (#Orig, #Translated);
END
GO
DECLARE #Orig nvarchar(32);
DECLARE #Translated nvarchar(32);
SET #Orig = N'Name'
SET #Translated = N'Name_Translated'
EXEC Translate_Orig #Orig,#Translated;

Help with an IF in SQL SERVER Stored procedure

can anyone help me with construction of an IF in a stored procedure in sql server.
Basically I have a simple stored procedure but I now need to pass in a new input parameter which depending if it is true I pass the value D and if its false I pass the value A. But the change is in the middle of a subquery.. let me explain... here is the stored procedure. basically if I send in True for ReturnOldStatus I execute the subquery ItemStatus='D' and if it is false then i pass in ItemStatus='A'
CREATE PROCEDURE [dbo].[MyTempStoredProc]
(
#IdOffice Int,
#ReturnOldStatus bit
)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM Offices
WHERE
IdOffice = #IdOffice
AND (V.OffType NOT IN (
SELECT * FROM MiscOff
WHERE
ItemStatus= 'D') // This needs to be ItemStatus ='A' if FALSE is passed in on the input param
Any ideas??
Thanks
I would solve it like this:
declare #itemStatus varchar(1);
if (#inputParam = 'FALSE')
begin
set #itemStatus = 'A'
end
else
set #itemStatus = 'D'
SELECT * FROM Offices
WHERE
IdOffice = #IdOffice
AND (V.OffType NOT IN (
SELECT * FROM MiscOff
WHERE ItemStatus= #itemStatus)
)
T-Sql is not my native language, so there may be errors in there...
Just use the T-SQL if statement:
IF #ReturnOldStatus = 0
BEGIN
--Some statements
END
ELSE
BEGIN
--Some other statements
END
I think this will suffice for your problem. If not, look into the DECLARE/SET statements in TSQL.
CREATE PROCEDURE [dbo].[MyTempStoredProc]
(#IdOffice Int,
#ReturnOldStatus bit )
AS BEGIN
SET NOCOUNT ON;
SELECT * FROM Offices
WHERE IdOffice = #IdOffice
AND (V.OffType NOT IN (SELECT * FROM MiscOff
WHERE (ItemStatus= 'D' AND #ReturnOldStatus = 1)
OR
(ItemStatus= 'A' AND #ReturnOldStatus = 0)
)

Resources