SQL Server - stored procedure - sql

I am trying to create a stored procedure in SQL Server 2008. According to the parser, the syntax is OK. However, when I try to execute the stored procedure and pass actual values, the following error comes up:
Msg 201, Level 16, State 4, Procedure
SaveOneTimeDonation, Line 0
Procedure or function 'SaveOneTimeDonation' expects parameter '#donation', which was not
supplied.
Strange enough, the data is actually inserted into the table so I don't know why it is displaying this error.
How can I solve this problem please? Here is the code:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[SaveOneTimeDonation]
#donation float,
#date nvarchar
AS
INSERT INTO OneTime_Trans(Donation, Trans_Date) VALUES (#donation, #date)
exec SaveOneTimeDonation

The last line is executing the stored procedure. There it is missing the parameters.
exec SaveOneTimeDonation
Good luck!

Stored procedure is with parameters so at the end you should give parameters when you try to execute it.

Related

How to solve this SQL Server error I'm getting?

As you can see below I am trying to execute a stored procedure by passing the two parameters, I get error like
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near 'C:\SQL_Backup\Test.bak'.
Code:
EXEC Proc_RestoreSQL_Database ('C:\SQL_Backup\Test.bak','DMTest')
Stored procedure being executed:
ALTER PROCEDURE [dbo].[Proc_RestoreSQL_Database]
#DISK nvarchar(1000),
#DBName varchar(1000)
AS
BEGIN
-- Create DB with script(DMUtilityTest)
CREATE DATABASE [#DBName]
-- restore
RESTORE DATABASE [#DBName]
FROM DISK = N'#DISK'
END
There is no need to CREATE the database if you are immediately going to restore it.
Just execute the restore directly. Do not wrap the database name parameter in [], nor the filename parameter in quotes '', you only need this if you are embedding the actual name, not parameters.
You should also use more sensible parameter types
CREATE OR ALTER PROCEDURE [dbo].[Proc_RestoreSQL_Database]
#DISK nvarchar(255),
#DBName sysname
AS
RESTORE DATABASE #DBName
FROM DISK = #DISK;
GO
This obviously assumes you are going to restore to the same location. You may want to pass new locations using MOVE TO #newFile
To execute it, again do not wrap in parenthesis (), just specify the parameter names
EXEC Proc_RestoreSQL_Database #DISK = 'C:\SQL_Backup\Test.bak', #DBName = 'DMTest';

I have a super simple stored procedure that is giving me issues

I have a super simple stored procedure that is giving me issues
The code is below
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Kris Nelson
-- Create date: 08/17/2020
-- Description: Taking datetime and turning it into a string.
-- =============================================
alter PROCEDURE sp_timeconversion
-- Add the parameters for the stored procedure here
#endtime datetime
AS
BEGIN
DECLARE #temptime time
DECLARE #militaryendtime varchar(20)
SELECT #temptime = CAST(#endtime AS TIME)
SELECT #militaryendtime = SUBSTRING(CONVERT(varchar, #temptime),0,6)
RETURN #militaryendtime
END
GO
When running
EXEC sp_timeconversion #endtime = '2020-08-17 16:43:56.583'
I get this error
Msg 245, Level 16, State 1, Procedure sp_timeconversion, Line 17
Conversion failed when converting the varchar value '16:43' to data type int.
Though I never convert to an int and I'm unsure where it's getting that?
Any advice?
Sorry I know it's a very simple question but I can't seem to understand what's throwing it for a loop.
The return argument to a stored proc is supposed to return a status or some other integer value. You cant return a string.
You need to use either define a return argument or use a simple
select #militaryendtime
which will return it as a 1 line, 1 column result set.
You could also consider making this code a function rather than a stored procedure which expects to have a returned value by default.
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-function-transact-sql?view=sql-server-ver15

If stored procedure returns 1, throw error, else continue

I have a stored procedure which is calling another stored procedure (cannot change this to a function). Depending on the result of the called stored procedure, I'd either like to throw an error, or carry on.
The stored procedure that is being called is looking for data in a table and returning 1 if it finds the data, or null if no data is returned.
How would the SQL look if I was using the following logic? If the stored procedure returns a value of 1, throw an error. If the stored procedure returns a NULL, carry on.
DECLARE #returnvalue INT
EXEC #returnvalue = Stored_procedure_name
IF #returnvalue = 1
BEGIN
--You could use THROW (available in SQL Server 2012+):
--THROW <error_number>, <message>, <state>
THROW 50000, 'Your custom error message', 1
END

How to execute a stored procedure after it is created?

I'm trying to execute a stored procedure directly after its creation however it is not getting called. It looks like the stored procedure is not yet created during the execution call.
Here is how the script looks like:
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
EXEC sp_Transfer_RegionData
The script runs fine however the needed table is not populated. After replacing the execution part with:
IF OBJECT_ID('sp_Transfer_RegionData') IS NOT NULL
begin
exec [dbo].[sp_Transfer_RegionData]
print 'tada'
end
I could see that the stored procedure does not exist when it has to be executed. Couldn't find a solution for this in the internet...
So how to make the SQL script run sync so that the stored procedure would already exist during the execution part?
You need a GO after you created the SP otherwise you have created a recursive SP that calls itself "indefinitely" which is 32 times in SQL Server.
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
Try this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE sp_Transfer_RegionData
AS
BEGIN
INSERT INTO Region (regionName)
SELECT column1
FROM openquery(ITDB, 'select * from db.table1')
END
GO
EXEC sp_Transfer_RegionData

Failed to call a stored procedure within another stored procedure

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[SD_Sproc_Insurance_Insert]
-- Add the parameters for the stored procedure here
(
#HCSInsuranceID bigint,
#HCSInsuranceCode varchar(10),
#HCSInsuranceName varchar(100),
#IsPPS bit,
#IsActive bit
)
AS
BEGIN TRAN InsuranceInsert
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
INSERT INTO SD_Sproc_ToGRS_Insurance(HCSInsuranceID ,HCSInsuranceCode, HCSInsuranceName, IsPPS ,IsActive)
VALUES (#HCSInsuranceID ,#HCSInsuranceCode, #HCSInsuranceName, #IsPPS, #IsActive);
COMMIT TRAN InsuranceInsert
The SD_Sproc_ToGRS_Insurance is the stored that I'll call.. I'm having a problem to call this one. Anyone suggest? That I'm doing the right path to call a stored procedure?
The above is SQL Server syntax. Use the exec command like so to call a stored procedure.
exec storedProcName #param1Name, #param2Name