Getting an error in sql - sql

I am getting this error:
Procedure or function 'NewEmployee' expects parameter '#LastName', which was not supplied
This is what I have:
CREATE PROCEDURE NewEmployee1 (
#LastName nvarchar(75)
,#FirstName nvarchar(50)
,#HireDate datetime
,#Birthdate datetime
,#Title nvarchar(30))
WITH EXECUTE AS CALLER
AS
BEGIN
Set NOCOUNT ON;
Begin Try
Begin Transaction;
INSERT INTO Employees (LastName,FirstName,HireDate,BirthDate,Title)
VALUES (#LastName, #FirstName, #HireDate,#Birthdate,#Title)
COMMIT TRANSACTION;
End Try
Begin Catch
--Rollback any active or uncommitable transactions before
--inserting information in the errorLog
If ##Trancount > 0
Begin
Rollback Transaction;
End
Execute NewEmployee1 ;
End Catch;
End;
Exec NewEmployee
#LastName = 'Halpert',
#FirstName = 'Jim',
#HireDate = '11/14/2011',
#BirthDate = '04/02/1971',
#Title = 'Sales';
Should I have it like #lastName nvarchar(75) = Null?

You have the line Execute NewEmployee1 ; which attempts to execute the stored procedure with no parameters.
If you want default values assigned to parameters, you need to do as you suggested...
CREATE PROCEDURE NewEmployee1 (
#LastName nvarchar(75) = NULL
,#FirstName nvarchar(50) = NULL
,#HireDate datetime = NULL
,#Birthdate datetime = NULL
,#Title nvarchar(30) = NULL
)
Or replacing the NULLs with any appropriate default value.

You define the procedure as NewEmployee1, but then you call NewEmployee.
EDIT:
I think that calling Execute NewEmployee1 ; in the CATCH statement could potentially cause an infinite loop if there were some problem calling the stored procedure with the default values... Are you sure you want to call the SP again in the CATCH?

I see
CREATE PROCEDURE **NewEmployee1**
while you later on call it using
Exec NewEmployee
withiout the 1 at the end of the name.
Probably you have another procedure NewEmployee, which does not have a param #LastName.

Related

How to return an id and use it directly in another stored procedure?

I want his stored procedure to return the inserted id
ALTER PROCEDURE [dbo].[InsertAddress_DBO]
#Name VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Address]([Address_Name])
OUTPUT INSERTED.Address_Id
VALUES (#Name)
END
This one the same
ALTER PROCEDURE [dbo].[InsertDocumentation_DBO]
#Texte VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Documentation]([Documentation_Text])
OUTPUT inserted.Documentation_Id
VALUES (#Texte)
END
And this one to use them and return her own -
like using the inserted id to put it into the next stored procedure as a parameter
ALTER PROCEDURE [dbo].[InsertEstablishmentByStrings_DBO]
#Establishment_Name VARCHAR(50),
#Address_Name VARCHAR(50),
#Documentation_Text VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Address_ID INT ,
#Documentation_ID INT
EXEC #Address_ID = [dbo].[InsertAddress_DBO]
#Name = "rue de la banchiesserie 85 Golback"
EXEC #Documentation_ID = [dbo].[InsertDocumentation_DBO]
#Texte = "né en 55555 restaurant fabuleux"
INSERT INTO [dbo].[Establishment]([Establishment_Name],[Address_Id],[Documentation_Id])
OUTPUT inserted.Establishment_Id
VALUES (#Establishment_Name,#Address_ID,#Documentation_ID)
END
However, I always get an error, because the stored procedure doesn't return the id when I execute it.
What is wrong in my code?
I would like to get the code I could use again and again in each stored procedure I have to execute. I already tried ##Identity, indent, scoped,... nothing works.
If you want to return something from stored procedure to the context of SQL query execution you may use a return statement or an output parameter. I would suggest you to use the second option. The first one is generally intended to return status of procedure execution.
ALTER PROCEDURE [dbo].[InsertAddress_DBO]
#Name VARCHAR(50),
#Address_ID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Address]([Address_Name])
VALUES (#Name)
SET #Address_ID = SCOPE_IDENTITY()
END
Than you can use returned value in your outer procedure
ALTER PROCEDURE [dbo].[InsertEstablishmentByStrings_DBO]
#Establishment_Name VARCHAR(50),
#Address_Name VARCHAR(50),
#Documentation_Text VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Address_ID INT ,
#Documentation_ID INT
EXEC [dbo].[InsertAddress_DBO]
#Address_ID = #Address_ID OUTPUT,
#Name = "rue de la banchiesserie 85 Golback"
...
END
An OUTPUT INSERTED clause you used doesn't returns data to the query execution context but send them to the output stream.
Your stored procedures should look like this, using an OUTPUT parameter, not trying to consume a RETURN value (which should never contain data) using a resultset. Also [don't] [put] [everything] [in] [square] [brackets] [unless] [you] [have] [to], [because] [all] [it] [does] [is] [hamper] [readability], and don't surround string literals with "double quotes" because that means something else in T-SQL.
CREATE OR ALTER PROCEDURE dbo.InsertAddress_DBO
#Name varchar(50),
#Address_Id int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.Address(Address_Name)
VALUES (#Name);
SELECT #Address_Id = SCOPE_IDENTITY();
END
GO
CREATE OR ALTER PROCEDURE dbo.InsertDocumentation_DBO
#Texte varchar(50),
#Doc_Id int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.Documentation(Documentation_Text)
VALUES (#Texte);
SELECT #Doc_Id = SCOPE_IDENTITY();
END
GO
Now, your main procedure can do this:
CREATE OR ALTER PROCEDURE dbo.InsertEstablishmentByStrings_DBO
#Establishment_Name varchar(50),
#Address_Name varchar(50),
#Documentation_Text varchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Address_ID INT ,
#Documentation_ID INT
EXEC dbo.InsertAddress_DBO
#Name = #Address_Name,
#Address_Id = #Address_ID OUTPUT;
EXEC dbo.InsertDocumentation_DBO
#Texte = Documentation_Text,
#Doc_Id = #Documentation_ID OUTPUT;
INSERT dbo.Establishment
(Establishment_Name, Address_Id, Documentation_Id)
OUTPUT inserted.Establishment_Id,
inserted.Address_ID, inserted.Documentation_ID
VALUES (#Establishment_Name,#Address_ID,#Documentation_ID);
END
GO
And you call it like this:
EXEC dbo.InsertEstablishmentByStrings_DBO
#Establishment_Name = 'Gaston''s',
#Address_Name = 'rue de la banchiesserie 85 Golback',
#Documentation_Text = 'né en 55555 restaurant fabuleux';
And get results like this:
Establishment_Id
Address_ID
Documentation_ID
1
1
1
Fully working example on db<>fiddle

Procedure call inside procedure

First I need to check if spOBJ_CopyInspectiontask has any errors, if there are no errors, only then proceed to the insert statement.
DECLARE #tblSystemId TABLE
(
OldSysId int, NewSysId int,
OldTaskId int, NewTaskId int,
TaskObjectIds varchar(max) null
);
INSERT INTO #tblSystemId (OldSysId , NewSysId, OldTaskId, NewTaskId, TaskObjectIds)
EXEC dbo.spOBJ_CopyInspectiontask #UserId, #TargetCustomerId, #TargetContractId, #TargetSiteId, #SourceCustomerId, #OldTaskId, #SystemName, #Checkall, #CopyReports, #Return OUTPUT, #ObjectIds OUTPUT;
How can I check that?
Perhaps you want a try/catch block:
BEGIN TRY
INSERT INTO #tblSystemId (OldSysId , NewSysId, OldTaskId, NewTaskId,TaskObjectIds )
EXEC dbo.spOBJ_CopyInspectiontask #UserId,#TargetCustomerId, #TargetContractId, #TargetSiteId,
#SourceCustomerId, #OldTaskId, #SystemName, #Checkall, #CopyReports, #Return OUTPUT, #ObjectIds OUTPUT;
END TRY
BEGIN CATCH
-- do something here in the event of failure
END CATCH;

Execute a stored procedure from another, and return two variables

I have a stored procedure called clients with 3 parameters: the first one for user input, and the last two are OUTPUT parameters.
This is the code:
CREATE PROCEDURE clients
(#name NVARCHAR(100),
#id_client int OUTPUT,
#messg varchar(1) OUTPUT)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRAN
IF NOT EXISTS (SELECT name FROM client WHERE name = #name)
BEGIN
INSERT INTO client(name) VALUES (#name);
SET #id_client = SCOPE_IDENTITY();
SET #messg = 'o'
COMMIT TRAN
END
ELSE
BEGIN
SELECT #id_client = id_client
FROM client
WHERE name = #name;
SET #messg = 'o'
COMMIT TRAN
END
END TRY
BEGIN CATCH
SET #messg = 'e'
ROLLBACK TRAN
END CATCH
END
I need to call this stored procedure from another the second one is called updateS, and I'm trying the following:
CREATE PROCEDURE updateS
(#clientname VARCHAR(100))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #id INT;
DECLARE #msg VARCHAR(1);
EXEC clients #clientname, #id, #msg; --Problem here to retrieve the id
END
This stored procedure has a parameter for the name of the client, but I need to retrieve the id of the client, but it doesn't work as I'm trying.
Basically I need to get the id and use it in the second stored procedure.
Any question post on comments.
You need to specify OUTPUT when you execute the stored procedure as well as when you define it:
EXEC clients #clientname, #id OUTPUT, #msg OUTPUT;
Did you miss the output keyword while passing the parameter to the stored procedure? Moreover, I would change the clients stored procedure and make the parameter as char(1) rather like #messg char(1) OUTPUT

Stored procedure return conversion error when there should be no conversion

I have a stored procedure that just returns a varchar(1000), but when I run using the following block:
declare #P1 VARCHAR(1000)
SET #P1=''
PRINT 'P1='+#P1
exec Test #PhoneNumber='1234567890',#AgentID=N'Test AgentID',#SP_RETURN=#P1 output
PRINT 'P1='+#P1
I get
Msg 50000, Level 16, State 1, Procedure Test, Line 50
Conversion failed when converting the varchar value 'Complete' to data type int..
Msg 245, Level 16, State 1, Procedure Test, Line 60
Conversion failed when converting the varchar value 'Exception raised' to data type int.
This is the stored procedure:
Create PROCEDURE [dbo].[Test]
#PhoneNumber as CHAR(10),
#AgentID as VARCHAR(100),
#SP_RETURN as VARCHAR(1000) OUTPUT,
#UTCDateTimeOverride as Datetime = NULL
AS
BEGIN
BEGIN TRY
SET #SP_RETURN = N'Complete'
RETURN #SP_RETURN
END TRY
BEGIN CATCH
SET #SP_RETURN = N'Exception raised'
RETURN #SP_RETURN
END CATCH
END
go
Any suggestions?
stored procedure always return integer, which is used to return the error code
you are returning a varchar field causing the exception
The error is caused by this
RETURN #SP_RETURN
do this instead
BEGIN TRY
SET #SP_RETURN = N'Complete'
RETURN 0 -- success
END TRY
BEGIN CATCH
SET #SP_RETURN = N'Exception raised'
RETURN ERROR_NUMBER() -- error code
A stored Procedure can only return an Int value. Varchar values are "returned" via OUTPUT variables.
Create PROCEDURE [dbo].[Test]
#PhoneNumber as CHAR(10),
#AgentID as VARCHAR(100),
#SP_RETURN as VARCHAR(1000) OUTPUT,
#UTCDateTimeOverride as Datetime = NULL
AS
BEGIN
BEGIN TRY
SET #SP_RETURN = N'Complete'
--RETURN #SP_RETURN not needed
END TRY
BEGIN CATCH
SET #SP_RETURN = N'Exception raised'
-- no need for return statement here
END CATCH
END
go
Now when executing the proc you will do something like ...
DECLARE #RETURN_Value VARCHAR(1000);
Exec [dbo].[Test] #PhoneNumber = 'Some Value'
,#AgentID = 'Some Value'
,#SP_RETURN = #RETURN_Value OUTPUT
Now the #RETURN_Value variable will have the values
You're already specifying #SP_RETURN as OUTPUT. You don't need it in your RETURN statements as well. Just "RETURN"
It is good practice to use return in stored procedures just to give feedback is procedure executed successfully or not. To return values it is better it is better to use output parameters.
Using output parameters you are able to:
1.return more then 1 one value (using return you are able to return just one value);
2.return any data type (with return keyword you can return only int).

Receiving an error: The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION in SQL Script

Was trying to perform an update to a database for an application, and then SQL server threw me a Commit has no corresponding being transaction. It was kind enough to point me to the procedure but I cannot seem to find what the issue is.
This is my code:
ALTER PROCEDURE [dbo].[sp_tblUser_Update] #UserID INTEGER,
#UserName NVARCHAR(20),
#Password NVARCHAR(10),
#Yard NVARCHAR(50),
#NewRole NVARCHAR(50),
#FullName NVARCHAR(50),
#Email NVARCHAR(50),
#BCConEmails BIT,
#CrewStatusReadOnly BIT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE tblUser
SET UserName = #UserName,
UserPassword = #Password,
Yard = #Yard,
Fullname = #FullName,
EmailAddress = #Email,
NewRole = #NewRole,
BCConEmails = #BCConEmails,
CrewStatusReadOnly = #CrewStatusReadOnly
WHERE ( UserID = #UserID )
END
GO
COMMIT
You don't need the COMMIT line as the END in your code is what terminates the previous BEGIN. If you want to add a transaction you should put a BEGIN TRANSACTION before the update statement and move the COMMIT before the END
begin tran
delete from Customerages
where Age=25
commit;
begin tran
delete from Customerages
where Age=25
rollback;