My first question: How to add the following procedure to the "Programmability-> Stored Procedures" folder.
USE [Tournaments]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spPrizes_Insert]
#PlaceNumber int,
#PlaceName nvarchar(50),
#PrizeAmount money,
#PrizePercentage float,
#id int = 0 output
AS
BEGIN
SET NOCOUNT ON;
insert into dbo.Prizes (PlaceNumber, PlaceName, PrizeAmount, PrizePercentage)
values (#PlaceNumber, #PlaceName, #PrizeAmount, #PrizePercentage);
select #id = SCOPE_IDENTITY();
END
and second question - I have error:
Msg 208, Level 16, State 6, Procedure spPrizes_Insert, Line 9
Invalid object name 'dbo.spPrizes_Insert'.
Why?
Related
I am getting this error
Msg 201, Level 16, State 4, Procedure sp_GetAllAirports, Line 0 [Batch Start Line 2]
Procedure or function 'sp_GetAllAirports' expects parameter '#AirportID', which was not supplied."
When I run
EXEC sp_GetAllAirports
The following is my stored procedure which shows #AirportID, what could be the issue?
IF OBJECT_ID('sp_GetAllAirports', 'P') IS NOT NULL
DROP PROCEDURE [dbo].[sp_GetAllAirports]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_GetAllAirports]
#AirportID INT,
#ICAOCode VARCHAR(4) NULL,
#AirportName VARCHAR(MAX),
#City VARCHAR(MAX),
#Lat DECIMAL(8,3),
#Long DECIMAL (11,3),
#Elevation INT,
#Country NVARCHAR(MAX)
AS
BEGIN TRANSACTION
BEGIN TRY
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
--SELECT * FROM tbl_Airports ORDER BY AirportID ASC
SELECT
AirportID, ICAOCode, AirportName, City,
Latitude, Longitude, Elevation, CountryFK
FROM
tbl_Airports
LEFT JOIN
tbl_Countries ON CountryID = tbl_Airports.CountryFK
WHERE
CountryID = tbl_Airports.CountryFK
ORDER BY
AirportID
END TRY
BEGIN CATCH
DECLARE #ErMessage NVARCHAR(MAX),
#ErSeverity INT,
#ErState INT
SELECT
#ErMessage = ERROR_MESSAGE(),
#ErSeverity = ERROR_SEVERITY(),
#ErState = ERROR_STATE()
IF ##TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
RAISERROR(#ErMessage, #ErSeverity, #ErState)
END CATCH
IF ##TRANCOUNT > 0
BEGIN
COMMIT TRANSACTION
END
GO
Your stored procedure is defined to expect arguments (AirportID, ICAOCode, AirportName, City, Lat, Long, Elevation and Country):
CREATE PROCEDURE [dbo].[sp_GetAllAirports]
#AirportID INT,
#ICAOCode VARCHAR(4) NULL,
#AirportName VARCHAR(MAX),
#City VARCHAR(MAX),
#Lat DECIMAL(8,3),
#Long DECIMAL (11,3),
#Elevation INT ,
#Country NVARCHAR(MAX)
AS
...
However, it doesn't use any of them. So you probably just need to remove them:
CREATE PROCEDURE [dbo].[sp_GetAllAirports]
AS
...
Alternatively, make sure the arguments are used in the SP (so it makes sense to expect arguments) and pass values accordingly (sp_GetAllAirports 1234) e.g.:
CREATE PROCEDURE [dbo].[sp_GetAllAirports]
#AirportID INT
AS
....
SELECT AirportID, ICAOCode, AirportName, City, Latitude, Longitude, Elevation, CountryFK
FROM tbl_Airports
LEFT JOIN tbl_Countries ON CountryID = tbl_Airports.CountryFK
WHERE CountryID = tbl_Airports.CountryFK
AND AirportID = #AirportID -- Using first argument here
ORDER BY AirportID
....
Or, finally, give the arguments default values, e.g:
CREATE PROCEDURE [dbo].[sp_GetAllAirports]
#AirportID INT = NULL,
#ICAOCode VARCHAR(4) = 'FOO',
...
AS
...
That way you won't have to explicitly pass any argument values. However, you'll still need to use the arguments to having the arguments make sense in the first place.
I am getting the below error while executing a stored procedure
USE [Smart2uat]
GO
/****** Object: StoredProcedure [dbo].[SPJOB_ALLLOC] Script Date: 2/11/2016 12:37:40 PM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[SPJOB_ALLLOC] AS
declare #cardno as varchar(8)
declare #iodate as datetime
declare #iotime as varchar(8)
declare #holdername as varchar(100)
declare #IO_MSKID as varchar(7)
declare #IO_LOCATION_CODE as varchar(3)
declare #IO_COMPANY_CODE as varchar(3)
declare #IO_ACTIVITY_CODE as varchar(6)
declare #IO_FIRST_NAME as varchar(20)
declare #IO_THIRD_NAME as varchar(20)
declare #IO_EMPLOYEE_CODE as varchar(3)
declare #rows as integer
declare curatt1 cursor for
select iodate,cardno,iotime,holdername from iodatatmp where isnull(cardno,'')<>'' and isnull(cardno,'') not like 'XXXX%' order by iotime
open curatt1
fetch next from curatt1 into #iodate,#cardno,#iotime,#holdername
WHILE ##FETCH_STATUS = 0
BEGIN
if not exists(select CardNo from iodata where iodate= #iodate and cardno= #cardno)
begin
select #IO_MSKID = ''
select #IO_MSKID=MSKID,#IO_LOCATION_CODE=LOCATION_CODE,#IO_COMPANY_CODE=COMPANY_CODE,#IO_ACTIVITY_CODE=ACTIVITY_CODE,#IO_FIRST_NAME=FIRST_NAME,#IO_THIRD_NAME=THIRD_NAME,#IO_EMPLOYEE_CODE=EMPLOYEE_CODE from Employee_Mast where card_number = #cardno
IF #IO_MSKID <> ''
insert into iodata(cardno,iodate,iotime,holdername,IO_MSKID,IO_LOCATION_CODE,IO_COMPANY_CODE,IO_ACTIVITY_CODE,IO_FIRST_NAME,IO_THIRD_NAME,IO_EMPLOYEE_CODE)
values(#cardno,#iodate,#iotime,#holdername,#IO_MSKID,#IO_LOCATION_CODE,#IO_COMPANY_CODE,#IO_ACTIVITY_CODE,#IO_FIRST_NAME,#IO_THIRD_NAME,#IO_EMPLOYEE_CODE)
end
fetch next from curatt1 into #iodate,#cardno,#iotime,#holdername
END
close curatt1
deallocate curatt1
delete from iodata where cardno like 'XXXX%'
error is
Msg 8152, Level 16, State 14, Procedure SPJOB_ALLLOC, Line 31
String or binary data would be truncated.
The statement has been terminated.
(0 row(s) affected)
Can you please help me..? I am attaching the table design of iodatatmp on which the cursor is fetching and the destination table - Iodata
I have checked the length of the variable...and seems everything fine....Kindly help me..Thanks in advance
The error you are getting is caused by the fact that the size of some of the varchar type columns in IODataTmp are larger than the equivalent column in IOData.
For instance IODataTmp.Holdername is declared as varchar(32) and yet IOData.Holdername is declared as varchar(20) and so if you have a value in IODataTmp.Holdername that is longer than 20 characters it will fail when it tries to insert into IOData.Holdername. Another issue you have is that variables you have declared in the stored procedure to hold the values are a different size again, #holdername is declared as varchar(100).
The solution is to ensure that all the column and variable sizes for a piece of data are the same.
My Stored procedure is like this
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE ph_GetAllStaffAddressByCamp
#Year INT,
#strYear VARCHAR(2) OUTPUT
AS
IF NULLIF(#Year, '') IS NULL
SET #Year = Exec [cs_GetCurrentYear] #strYear
SELECT DISTINCT [VolID], [CampID], [VolFName] FROM [vStaffJobAndCamp]
where CampCode Like #Year
As you can see I am trying to execute [cs_GetCurrentYear] inside this procedure (added the procedure below)
ALTER PROCEDURE [dbo].[cs_GetCurrentYear]
#strYear VARCHAR(2) OUTPUT
AS
SELECT #strYear = Year FROM tblCurrentYear
But this throws an error on compile .
And it looks like this
Msg 156, Level 15, State 1, Procedure ph_GetAllStaffAddressByCamp, Line 8
Incorrect syntax near the keyword 'Exec'.
You can create a table, and insert into it the result of your SP. Try
DECLARE #TheYear TABLE
(
TheYear Varchar(2)
)
INSERT INTO #theYear (TheYear)
Exec [cs_GetCurrentYear] #strYear
SET #Year = (SELECT TOP(1) TheYear FROM #TheYear)
You can modify your second stored procedure to a stored function and call it from your first stored procedure. Read more here: How to call a scalar function in a stored procedure
Below is the stored proc i have so far I keep getting this error when executed:
Msg 137, Level 16, State 1, Procedure db_recession_band_dates_save, Line 18
Must declare the scalar variable "#dates".
ALTER PROCEDURE [dbo].[Dates_Save]
#Loc VARCHAR(75),
#dates StartEndDateType READONLY
AS
BEGIN
DECLARE #id int
SELECT #id = MYINTFIELD FROM date_locations
IF #id IS NULL
BEGIN
INSERT INTO db_recession_bands VALUES (#loc)
SET #id = ##IDENTITY
END
INSERT INTO db_recession_band_dates VALUES (#id,#dates)
END
if StartEndDateType is a user defined table type then you treat it as if it were a table.
Change this:
INSERT INTO db_recession_band_dates VALUES (#id,#dates)
Into something like
INSERT INTO db_recession_band_dates (<COLUMN LIST>) -- don't do blind inserts it will hurt you at some point in the future
SELECT #id, <COLUMN LIST>
FROM #dates
Hey all,
I have a stored procedure and I need to call it within another stored procedure, but I want the first one to return a value (field value).
CREATE PROCEDURE rnd_STR
(
#Length int
)
#alphaVar varchar(10) OUTPUT
AS
SET #alphaVar = 'blah'
#procedure body
END
GO
DECLARE #alphaVar varchar(10)
EXEC rnd_STR #alphaVar output
SELECT #alphaVar
ERRORS
Msg 102, Level 15, State 1, Procedure rnd_STR, Line 6
Incorrect syntax near '#alphaVar'.
Msg 137, Level 15, State 1, Procedure rnd_STR, Line 8
Must declare the scalar variable "#alphaVar".
Msg 2812, Level 16, State 62, Line 4
Could not find stored procedure 'rnd_STR'.
(1 row(s) affected)
didn't work !!
How can I call it??
BTW, the returned #ID is a string
You say #alphaVar is varchar(10). In that case you need to use an output parameter as below. Return can only be used for integer types in stored procedures.
CREATE PROCEDURE rnd_STR
#Length int,
#alphaVar varchar(10) OUTPUT
AS
BEGIN
SET #alphaVar = 'blah'
/* Rest of procedure body*/
END
GO
DECLARE #alphaVar varchar(10)
EXEC rnd_STR 10, #alphaVar output
SELECT #alphaVar
Alternatively you could use a scalar UDF rather than a stored procedure.
You're calling syntax is wrong.
DECLARE #newId int
EXEC #newId = rnd_STR, #length = 10
See EXECUTE in the reference.
Try this:
EXEC #alphaVar = rnd_STR 10
A work around of getting this code is to execute the stored procedure in your Management Studio itself and copy the SQL code