INSERT return value of stored procedure - sql

I'm trying to INSERT a RETURN value for a stored procedure and I just can't get it to work.
I have one table called "person.person_ids" with fields "id int, uid uniqueidentifier"
and a bunch of other tables called for example "person.employee", "person.client" etc. These tables all get their "id"s as foreign keys from the "person.person_ids" table.
The procedure does this:
CREATE PROCEDURE PERSON.NewPerson
AS
BEGIN
DECLARE #ret INT;
INSERT INTO PERSON.ID_PERSON
VALUES (NEWID());
SET #ret = SCOPE_IDENTITY()
RETURN #ret;
END
GO
and my INSERT looks like this:
INSERT INTO PERSON.EMPLOYEE VALUES
(EXECUTE PERSON.NewPerson, 1, '15434235', '10768348153', '1962-3-2', '1999-10-2', 'PETER', '', 'SMITH', 'HAMMER')
GO
Basically I'm trying to create a stored procedure of function that I can call to automatically generate a new record in "person.person_ids" whenever I insert a new record into "person.employee".
I tried using OUTPUT from INSERT but I could not get it to work and INSERT isn't allowed inside of functions.

Here is your code with the issues fixed and some recommended improvements. The comments explain what and why.
SP:
CREATE PROCEDURE PERSON.NewPerson
(
-- Use an output parameter to get values out of an SP
#NewId INT OUT
)
AS
BEGIN
-- Recommended to always list the columns you are inserting to
-- Personally my preference is to select them (because that scales to multiple inserts), I never use the 'values' clause.
INSERT INTO PERSON.ID_PERSON (uid)
SELECT NEWID();
SET #NewId = SCOPE_IDENTITY();
-- The return statement is for a status for the SP, usually 0 for success, some other int for an error
RETURN 0;
END
GO
Calling SP:
DECLARE #MyNewId INT;
-- Run the SP before your insert to get your new value
EXEC PERSON.NewPerson #MyNewId OUT;
-- Then insert - ideally with a list of columns
INSERT INTO PERSON.EMPLOYEE
SELECT #MyNewId, 1, '15434235', '10768348153', '1962-3-2', '1999-10-2', 'PETER', '', 'SMITH', 'HAMMER'

Related

Insert trigger in SQL Server

I am trying to write a trigger that inserts in a log table the date, name of the table I inserted in, the name of the operation (insert, delete, update) and the number of insertion.
Here is my code:
CREATE TRIGGER [dbo].[Inser]
ON [dbo].[Avion]
AFTER INSERT
AS
BEGIN
DECLARE #codAv int
DECLARE #NumeAv varchar(100)
DECLARE #MotorAv varchar(100)
SELECT #codAv = INSERTED.codA FROM INSERTED
SELECT #NumeAv = INSERTED.NumeA FROM INSERTED
SELECT #MotorAv = INSERTED.Motor FROM INSERTED
SELECT ##ROWCOUNT AS INSERTED;
INSERT INTO LogM (DataM, Numele, Tipul, Numar)
VALUES(GETDATE(), 'Avion', 'Inserare', ##ROWCOUNT);
PRINT 'INSERT trigger fired.'
END
I have a stored procedure where I have something like 20 insertion of this type:
INSERT INTO Avion (Motor,NumeA)
VALUES ('Junkers','Focke-Wulf');
all separated from one another. When I run that code, the table LogM will be populated with 20 new rows of this type:
5 Nov 27 2016 8:58PM Avion Inserare 1.00
I want to make my trigger to count all the insertion stored in a procedure, then insert in LogM only one entry, with the ROWCOUNT not being 1.00, but the number of insertion I made in that stored procedure, and I don't have any idea how can I do this.
Thank you.
Instead of passing ##ROWCOUNT you can not just pass the ID?
##ROWCOUNT works to return the number of rows affected, as you enter one at a time, it will always be one.
CREATE TRIGGER [dbo].[Inser]
ON [dbo].[Avion]
AFTER INSERT
AS
BEGIN
DECLARE #codAv int
DECLARE #NumeAv varchar(100)
DECLARE #MotorAv varchar(100)
SELECT #codAv=INSERTED.codA FROM INSERTED
SELECT #NumeAv=INSERTED.NumeA FROM INSERTED
SELECT #MotorAv=INSERTED.Motor FROM INSERTED
INSERT INTO LogM
(DataM,Numele,Tipul,Numar)
VALUES(GETDATE(),'Avion','Inserare',#codAv);
PRINT 'INSERT trigger fired.'
END

Stored procedures - writing errors out to another table

I have two tables and a simple stored procedure. I want to use the stored procedure to insert into table1 with one of the parms being the primary key.
Here is where I am having trouble:
I want to have it so that if the parm/primary key the user has entered into the stored procedure and run is already on the table then it writes out the fields to a second table as well as the error description.
How do you capture / output this error information?
Stored procedure (sans the error logging):
CREATE PROCEDURE procedure1
#Primary INT,
#Info NVARCHAR
AS
BEGIN
SET NOCOUNT ON
INSERT INTO Table1
(
Primary ,
Info
)
VALUES
(
#Primary ,
#Info
)
END
Thanks
CREATE PROCEDURE dbo.InsertSomething
(
#PrimaryKey INT
, #Info NVARCHAR(MAX)
)
AS
BEGIN
IF EXISTS (SELECT 1 FROM dbo.Table1 WHERE ID = #PrimaryKey)
BEGIN
INSERT INTO dbo.Table2 (ID, Info)
VALUES (#PrimaryKey, #Info);
RAISERROR (15600,-1,-1, 'Your custom message');
END
ELSE
BEGIN
INSERT INTO dbo.Table1 (ID, Info)
VALUES (#PrimaryKey, #Info);
END
END
All you have to do is a simple check if record already exists in first table, and if it does just insert into it and throw error.

Inserting output parameters from stored procedure directly

I have a stored procedure that returns a pair of output parameters - the ID and the computed value. Is it possible to use a trigger with an insert statement, that inserts those two values directly? Something like this
CREATE TRIGGER Trig_FirstTable ON SecondTable AFTER UPDATE
AS
BEGIN
INSERT INTO FirstTable (OtherID, OtherValue)
VALUES (#otherID, #otherValue)
FROM StoredProcedure inserted.ID, inserted.Value, #otherID OUTPUT, #otherValue OUTPUT
END
According to the MSDN documentation you can use INSERT into with EXEC.
They give the following example:
--INSERT...EXECUTE procedure example
INSERT author_sales EXECUTE get_author_sales
But I think your stored procedure needs a SELECT statement to return the data instead of only filling the output parameters.
You can insert from SP like that:
drop procedure uspTest
GO
create procedure uspTest
AS
select 1 as id, 'x' as val
union all
select 2,'y'
GO
drop table #temp
GO
create table #temp (
id int,
val char(1)
)
GO
insert into #temp (id,val)
EXECUTE uspTest
GO
select
*
from #temp
But you cannot select a subset of columns, so this method will obviously fail if you add more outputs to your SP in the future:
insert into #temp (id)
EXECUTE uspTest
Another way is to store SP results in variables, and then use them for insert.

Stored Procedure - Knowing the next ID

I am creating a stored procedure to create a new customer so for instance,
CREATE PROCEDURE Customer_Create
#customer_arg
#type_arg
AS
BEGIN
INSERT INTO Customer (Customer_id, Type_id)
VALUES (#Customer_arg,#type_arg)
End;
If I have several foreign keys in my statement and they are all ID's is there a way for me to pull the NEXT ID number automatically without having to know what it would be off the top of my head when I run the execute statement? I would like to just have it pull the fact that the ID will be 2 because the previous record was 1
EXECUTE Customer_Create 16,2
Is it something wnith output? If so how does this work code wise
I suspect that what you want to do is return the new id after the record is inserted. For that:
CREATE PROCEDURE Customer_Create (
#customer_arg,
#type_arg,
#NewCustomerId int output
) AS
BEGIN
INSERT INTO Customer(Customer_id, Type_id)
VALUES (#Customer_arg, #type_arg);
#NewCustomerId = scope_identity();
End;
There are several other choices for getting the identity, which are explained here.
To get to the last inserted IDENTITY value you should use the OUTPUT clause like this:
DECLARE #IdentValues TABLE(v INT);
INSERT INTO dbo.IdentityTest
OUTPUT INSERTED.id INTO #IdentValues(v)
DEFAULT VALUES;
SELECT v AS IdentityValues FROM #IdentValues;
There are several other mechanisms like ##IDENTITY but they all have significant problems. See my Identity Crisis article for details.
In your case you can also experiment with #IDENTITY like this
DECLARE #NextID int
--insert statement goes here
SET #NextID = ##Identity`
Here are couple good resources for getting familiar with this
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
http://blog.sqlauthority.com/2013/03/26/sql-server-identity-fields-review-sql-queries-2012-joes-2-pros-volume-2-the-sql-query-techniques-tutorial-for-sql-server-2012/

Stored procedure return the primary key after insert?

I want a stored procedure to return the primary key of the new record after it gets executed. I think it will be returned by OUT parameter in the procedure. But how to select the newly inserted row ID ? I don't want to use select MAX(row_id) as it is a multi user environment.
Any procedure sample will be appreciated.
My platform is ISeries DB2 V5 R4. Thanks.
Edit
The row id Column is not an identity column. It uses a sequence for the key which gets generated via a trigger before insert on table.
Edit
Here is what I am trying to do
Begin Stored procedure
Insert into Employees;
(row id gets automatically generated by trigger)
Return row id ;
I want to avoid a select in returning row id.
just set the out parameter to the column that contains the PK.
CREATE PROCEDURE DB2TBL.DO_STUFF (IN Param1 INT, IN Param2 CHAR(32),OUT Param3 INT)
/* Param1 is primary key */
LANGUAGE SQL
P1: BEGIN
DECLARE OUTPARAM INT;
/* Do the stored procedure */
SET OUTPARAM = Param1;
--UPDATED---
Hi Popo,
First off could you give more detail on what you mean when you say the rowid is assigned by a trigger?
If you had a real identity column you would use the IDENTITY_VAL_LOCAL() function like this right after the INSERT: SELECT IDENTITY_VAL_LOCAL() INTO myrowid FROM SYSIBM.SYSDUMMY1; I'm not 100% on that syntax because I generally use embedded SQL and it works differently there so you might have to play with it. IBM documentation is at http://publib.boulder.ibm.com/infocenter/iseries/v6r1m0/index.jsp?topic=/db2/rbafzscaidentity.htm.
However since you are doing something more complicated, I think this alternate method might work. You'll need to re-format your INSERT to be wrapped in a SELECT.
SELECT myrowid
INTO myrowid
FROM FINAL TABLE (
INSERT INTO myfile (myrowid, other_stuff) VALUES (default, 'blah')
)
You'll need to adjust for the proper field names and so on but I think this will do the trick. There's not much documentation but if you want to see it go to http://publib.boulder.ibm.com/infocenter/iseries/v6r1m0/index.jsp?topic=/db2/rbafzbackup.htm and scroll all the way down to the bottom of the page.
Cheers
CREATE PROCEDURE ASF_InsertNewAuthorRequest
(IN #REQUESTTYPE CHAR(1), IN #UserID VARCHAR(18), IN #DATECREATED TIMESTAMP, IN #REQUESTSTATUS CHAR(1))
LANGUAGE SQL
DYNAMIC RESULT SETS 1
P1: BEGIN
DECLARE cursor1 CURSOR WITH RETURN for
SELECT IDENTITY_VAL_LOCAL FROM SYSIBM.SYSDUMMY1;
INSERT INTO AFS_REQUEST
( REQUESTTYPE, "UserID", DATECREATED, REQUESTSTATUS )
VALUES
( #REQUESTTYPE, #UserID, #DATECREATED, #REQUESTSTATUS );
OPEN cursor1;
END P1
INSERT INTO [User] (columns)
OUTPUT inserted.userId
VALUES (#values)
This will return the newly created userId column value... very simple.