Use an output parameter of store procedure in update Statement - sql

Hi I have a store procedure which insert data into a table1 and there is another store procedure which insert data in table2 I need to insert table2.ID in table1 while updating I am calling second store procedure in first and it returns me the Id as an output parameter but it didn't get updated.
SP1 :
ALTER PROCEDURE [dbo].[updateSP]
#prm1 varchar(30),
#prm2 varchar(20),
#prm3 varchar(20),
#prm3 varchar(max)
AS
BEGIN
SET NOCOUNT OFF;
DECLARE #ID int
exec SPInsert #ID OUTPUT, #prm1, #prm2 , #prm3
update tbl1 set status = prm1, updated_by = prm2, updated_date = GETDATE(), reason_id = #ID where student_no = prm3
END
SP2 :
ALTER PROCEDURE [dbo].[SPInsert]
#prm1 varchar(30),
#prm2 varchar(20),
#prm3 varchar(20),
#prm3 varchar(max),
#ID output,
AS
BEGIN
Insert into tbl2 (student_no, reason_txt, created_by, created_date)Values(#prm1 ,#prm2 ,#prm3 , GETDATE())
Select #ID = Scope_Identity()
END

Related

sql case statement then insert into table

i have stored procedure. the code looks like:
use nisa1415;
go
alter procedure dep.AddLessons
(
#LessonsTable nvarchar(50),
#LesDepId int,
#TeId int,
#GradeId varchar(10),
#Activated int,
#GroupId int,
#TaskTable nvarchar(50)
)
as
begin select #LessonsTable=
case #LessonsTable when 'dep.les_Biology' then
insert into dep.les_Biology(LesDepId,TeId,GradeId,Activated,GroupId,TaskTable)
values(#LesDepId,#TeId,#GradeId,#Activated,#GroupId,#TaskTable) end
return
end
it gives me an error:Incorrect syntax near 'insert'
what's the problem that i coudn't find?
ALTER procedure AddLessons
(
#LessonsTable nvarchar(50),
#LesDepId int,
#TeId int,
#GradeId varchar(10),
#Activated int,
#GroupId int,
#TaskTable nvarchar(50)
)
as
begin
if #LessonsTable = 'dep.les_Biology'
begin
insert into dep.les_Biology(LesDepId,TeId,GradeId,Activated,GroupId,TaskTable)
values(#LesDepId,#TeId,#GradeId,#Activated,#GroupId,#TaskTable) end
return
end

SQL Server Stored Procedure - Return varchar id of inserted row

I'm working on a cascading insertion where a stored procedure should return an id of the inserted row. This would not be a problem if the id of the table was an int. However, the id is a varchar and therefore I cannot use SCOPE_IDENTITY().
This is my procedure so far:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
INSERT INTO nextlabel.[Article] (name, article_nr, category, container_id, count_per_container, picture_id )
VALUES (#name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
SET #id = SCOPE_IDENTITY()
Where the last row is not correct since the column id is a varchar.
How can I return the id?
Try this:
CREATE PROCEDURE NEW_ARTICLE
#id varchar(50) OUTPUT,
#name varchar(100),
#articleNr varchar(50),
#gategory varchar(50),
#containerId varchar(50),
#contPerContainer int,
#pictureId varchar(50)
AS
SET NOCOUNT OFF
SET #id = newid()
INSERT INTO nextlabel.[Article] (id, name, article_nr, category, container_id, count_per_container, picture_id)
VALUES (#id, #name, #articleNr, #gategory, #containerId, #contPerContainer, #pictureId)
GO

How can I insert the results of a stored procedure into a new table

ALTER procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL
as
select
count(STAFF_ID) as countexel
from
TbStudentSurvey
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
select
Score as scoreexel
from
TbStaffScoreMaster
where
Status = 'Excellent'
exec [dbo].[staffscorecard]
GO
CREATE TABLE #temp ( countexel int, scoreexel int)
GO
INSERT INTO #temp (countexel , scoreexel)
EXEC [dbo].[staffscorecard]
GO
SELECT *
FROM #temp
GO
For a given staffid to calculate countexel and scoreexel you can re-write your stored procedure as:
create table TbStudentSurvey (STAFF_ID int,FEEDBACK varchar(20));
insert into TbStudentSurvey values (1,'excellent'),(1,'excellent'),(2,'excellent');
create table TbStaffScoreMaster (Score int,[Status] varchar(20));
insert into TbStaffScoreMaster values(100,'Excellent');
Go
create procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL,
#countexel int output,-- Explicitly declare output variables to fetch these values
#scoreexel int output
as
Begin
select
#countexel = count(STAFF_ID)
from
TbStudentSurvey
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
select
#scoreexel = Score
from
TbStaffScoreMaster
where
Status = 'Excellent'
End
GO
and then instead of using a temp table use a table variable because when you use temp tables,the table has to match the exact column layout as of the stored procedure.
--CREATE TABLE #temp (countexel int, scoreexel int)
--GO
--Create a table variable:
declare #temp table (countexel int, scoreexel int)
declare #countexel int, #scoreexel int,#STAFF_ID int;
--set value of staff Id for which you want to get countexel and scoreexel.
set #STAFF_ID = 1;
EXEC [dbo].[staffscorecard] #STAFF_ID ,#countexel output,#scoreexel output
INSERT #temp values (#countexel ,#scoreexel);
SELECT *
FROM #temp
GO
Method 2:
You can also write as:
alter procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL
as
Begin
select
count(STAFF_ID) as countexel , Score as scoreexel
from
TbStudentSurvey TSS
inner join TbStaffScoreMaster TSM on TSM.Status = TSS.FEEDBACK
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
group by STAFF_ID,Score
End
GO
declare #temp table (countexel int, scoreexel int)
declare #STAFF_ID int;
set #STAFF_ID = 1;--set value of staff Id for which you want to get countexel and scoreexel.
INSERT #temp EXEC [dbo].[staffscorecard] #STAFF_ID
SELECT *
FROM #temp
GO
Hope this helps!!

Pass Output Value of one stored procedure to another stored procedure

I want to use output value of one stored procedure in another stored procedure .
Stored procedure 1:
Create PROCEDURE [dbo].[usp_AddUpdateUser]
#UserId INT,
#Email varchar(50),
#FirstName varchar(50)
AS
BEGIN
MERGE [User] AS target
USING (SELECT #UserId) AS source (Id)
ON target.Id = source.Id
WHEN MATCHED THEN
UPDATE
SET Email = #Email,
FirstName = #FirstName
WHEN NOT MATCHED THEN
INSERT (Email, FirstName)
VALUES (#Email, #FirstName)
OUTPUT inserted.Id;
END
Now I want to use the inserted Id of above stored procedure to below stored procedure:
ALTER PROCEDURE usp_AddUpdateDealer
(#Id INT,
#DealerName varchar(55),
#Email varchar(55),
#UserId INT)
AS
BEGIN
DECLARE #NewUserId INT
EXEC #NewUserId = usp_AddUpdateUser #UserId, #Email, #DealerName
MERGE Dealer AS target
USING (SELECT #Id) AS source (Id) ON target.Id = source.Id
WHEN MATCHED THEN
UPDATE
SET #DealerName = #DealerName,
Email = #Email,
UserId = #NewUserId
WHEN NOT MATCHED THEN
INSERT (DealerName, Email, UserId)
VALUES (#DealerName, #Email, #NewUserId)
OUTPUT inserted.Id;
END
#NewUserId not gives the output value.
How can I got the output option of the usp_AddUpdateUser stored procedure to use that in next statement?
ALTER PROCEDURE usp_AddUpdateDealer
(
#Id INT,
#DealerName varchar(55),
#Email varchar(55),
#UserId INT
)
AS
BEGIN
DECLARE #t table(NewUserId INT )
INSERT #t(NewUserId)
EXEC #NewUserId = usp_AddUpdateUser #UserId,#Email,#DealerName
DECLARE #NewUserId INT
SELECT #NewUserId = NewUserId FROM #t
MERGE Dealer AS target
USING (SELECT #Id) AS source (Id)
ON target.Id = source.Id
WHEN MATCHED THEN
UPDATE
SET #DealerName = #DealerName,
Email = #Email,
UserId=#NewUserId
WHEN NOT MATCHED THEN
INSERT (DealerName,Email,UserId)
VALUES (#DealerName,#Email,#NewUserId)
OUTPUT inserted.Id;
END

Update the table using stored procedure

I have a table and I have 50 columns in it. From my code behind at first I am inserting 10 values using a stored procedure, after that in second page based on userid I want to update other 40 columns. So I am updating the table and the userid column of the table is an identity column which is auto incremented so how to get the user id for update stored procedure?
CREATE PROCEDURE sp_update
(#FormFiledBy varchar(50), #MaritalStatus varchar(50),
#Height varchar(50), #Religion varchar(50), #Caste varchar(100),
#MotherTongue varchar(50), #Education varchar(100),
#Occupation varchar(50), #CountryofResidence varchar(50),
#EducationDetails varchar(100), #AnnualIncome varchar(50),
#CountryOfBirth varchar(50), #BirthPlace varchar(50),
#TimeOfBirth nchar(10), #StarSign varchar(100),
#Gothram varchar(50), #Rassi varchar(50), #HavinChildren varchar(10),
#PhysicalStatus varchar (100)
)
AS
BEGIN
UPDATE Profile_Master
SET FormFiledBy = #FormFiledBy,
MaritalStatus = #MaritalStatus,
Height = #Height,
physicalStatus = #physicalStatus,
Religion = #Religion,
Caste = #Caste,
MotherTongue = #MotherTongue,
Education = #Education,
Occupation = #Occupation,
CountryofResidence = #CountryofResidence,
EducationDetails = #EducationDetails,
AnnualIncome = #AnnualIncome,
CountryOfBirth = #CountryOfBirth,
BirthPlace = #BirthPlace,
TimeOfBirth = #TimeOfBirth,
StarSign = #StarSign,
Gothram = #Gothram,
Rassi = #Rassi,
HavinChildren = #HavinChildren,
PhysicalStatus = #PhysicalStatus
WHERE
????
END
In your initial procedure, when you insert the data, you should retrieve and return the userid value in an OUTPUT parameter so that you can then supply it to your update procedure.
You can use SCOPE_IDENTITY() for this fairly easily.
As requested, some sample code (simplified, but you should see the pattern):
CREATE PROCEDURE add_row #data1 varchar(20), #data2 varchar(20), #id int OUTPUT
AS
BEGIN
INSERT INTO Table (Data1, Data2) VALUES (#data1, #data2)
SELECT #id = SCOPE_IDENTITY()
END
GO
CREATE PROCEDURE update_row #id int, #data3 varchar(20), #data4 varchar(20)
AS
BEGIN
UPDATE Table SET Data3 = #data3, Data4 = #data4
WHERE Id = #id
END
GO
you don't need update your primary key, but you must pass UserId as parameter of your stored procedure.
Update Profile_Master
Set ....
Where UserId = #UserId --your parameter
Nota : yuo must ensure that your primary key is just UserId
You just have to pass SCOPE_IDENTITY() from fist Stored Procedure as below:
#Id int OUTPUT,
---
SET #Id = SCOPE_IDENTITY()
And in the second Stored Procedure Apply the Id as an argument an Update the record based on that.
#Id VARCHAR(15)
Update Profile_Master
set ....
........
where UserId = #Id