Hi,
How to achieve this?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 13, 2011, 11:15 PM EDT
Try this
CREATE PROCEDURE PROCNAME
@CountryId integer ,
@CountryName VARCHAR(200) ,
@nType integer
AS
BEGIN
IF @nType=1
INSERT INTO Country VALUES(@CountryId,@CountryName);
ELSE IF @nType=2
UPDATE Country SET CountryName = @CountryName WHERE CountryId = @CountryID
ELSE IF @nType=0
DELET FROM Country WHERE CountryId = @CountryID
END
And use this Procedure Like
--To Insert
EXEC PROCNAME 1,'Bangladesh',1
--To UPDATE
EXEC PROCNAME 1,'Bangladesh',2
--To DELETE
EXEC PROCNAME 1,NULL,0
---------------------------------------------------
If this post helps you, then mark as "Correct Answer"
Thank You
Pravin MoreRank #1,419 · 1.2K reputationPosted Sep 13, 2011, 11:41 PM EDT
create procedure InDelUp
@valueClause varchar(max),
@SetClause varchar(max),
@whereClause varchar(max),
@Table_name varchar(max),
@case int -- will be 1 for insert,2 for update ,3 for Delete
as
Begin
declare @qry varchar(max)
if(@case=1)
begin
set @qry='insert into '+@Table_name+' values('+@valueClause+')';
exec(@qry)
end
if(@case=2)
begin
set @qry='Update '+@Table_name+' set '+@SetClause +' where '+@whereclause+'';
exec(@qry)
end
if(@case=3)
begin
set @qry='Delete from '+@Table_name+' where '+@whereclause+' ';
exec(@qry)
end
End
this is way...pass parameter accordingly.......u can change parameter n query according to ur requirment.....
hope it solve ur problem
if u dont need perticular parameter in any case pass it anything may be just blank string as we r not going to use it.....
Thanx,
Pravin.
B M SuchitraRank #2,922 · 492 reputationPosted Sep 14, 2011, 7:56 PM EDT
I also wanted to know how a single stored procedure will help to save data in many tables... I want the id of the previous table to store in another table.. In single stored procedure how do i do it?
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 14, 2011, 9:06 PM EDT
Use SCOPE_IDENTITY() Function for your purpose.
Consider, The ID Column of first table is AutoIncrement. You Can do this by,
Insert Into TableName1 Values("Name");
DECLARE @ID Integer
Select ID = @ID from TableName1 Where ID = SCOPE_IDENTITY()
then,
Insert Into TableNAme2 Values (@ID) --Store the ID of Previous table
--------------------------------------------------------------------------
If this Post Helps you, then mark as "Correct Answer"
Thank You
Pravin MoreRank #1,419 · 1.2K reputationPosted Sep 14, 2011, 9:26 PM EDT
when u are calling sproc for insert u pass @valueClause....so now pass @whereClause too which contain values eith coulmn u r inserting.....then immidiatly after insert query execute select query i.e
set @qry='insert into '+@Table_name+' values('+@valueClause+')';
exec(@qry)
set @qry1='select id from '+@Table_name+' where '+@whereClause+' '
int @newID=exec(@qry1)
now u have newID then just use it to insert to another table like......
set @qry2='insert into '+@Table_name2+' values('+@valueClause+')';
exec(@qry2)
hope this will solve ur problem.....
Thanx,
Pravin
B M SuchitraRank #2,922 · 492 reputationPosted Sep 14, 2011, 9:38 PM EDT
I tried to write a procedure for update and insert like this.
create procedure sp_processmaster
@ProcessID int,
@ProcessName varchar(50),
@ProcessCode varchar(20),
@Description varchar(MAX)
here i have 4 parameters... for insert i need 3 parameters since processid is autoincremented.. I get and error here.. For update it works fine
Pravin MoreRank #1,419 · 1.2K reputationPosted Sep 14, 2011, 9:55 PM EDT
set @qry='insert into table_name values('+ProcessName +','+ProcessCode+','+@Description+')'
exec(@qry)
Thanx
Pravin
Nethra R SRank #6,347 · 269 reputationPosted Nov 2, 2011, 3:12 AM EDT
Is that one stored procedure enough for the whole database?
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 2, 2011, 3:33 AM EDT
and if you want it for more than database then pass table name as databasename..tablename
thanks,
Pravin.
B M SuchitraRank #2,922 · 492 reputationPosted Nov 14, 2011, 11:23 PM EST
How to write this dynamic stored procedure in dataaccess layer? instead of writing in database? And how to call this and pass parameters?
Nethra R SRank #6,347 · 269 reputationPosted Nov 22, 2011, 8:18 PM EST
You have missed out declaring column names in your dynamic SP.
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 22, 2011, 8:29 PM EST
i gave it in thread started by You here.... and parameters of sp is enough no need of extra columns
http://www.c-sharpcorner.com/Forums/Thread/146676/single-stored-procedure-for-insert-update-deleteis-it-g.aspx
still m pasting it here....
@valueClause varchar(max),
@SetClause varchar(max),
@whereClause varchar(max),
@Table_name varchar(max),
@case int -- will be 1 for insert,2 for update ,3 for Delete
actually , these both threads are same(quetion) 1 is strated by you and another by Suchitra...its confusing...n you are toggling between these two.....
i hope you will able to solve your problem with the help of my reply....
Thanks
Pravin.