I've inherited an SSRS report which I need some assistance in understanding how the scalar variable works as I just don't get it.
The stored procedure starts off as below and there is no issue running the stored procedure with the #FromPeriod and #ToPeriod.
CREATE PROCEDURE [dbo].[_SP_STOREDPROCEDURE]
(#FromPeriod int, #ToPeriod Int)
AS
set nocount on
declare #v_FromPeriod int
declare #v_ToPeriod int
declare #cnt int, #periods int
declare #v_periodbegindate datetime
declare #v_YearBegindate datetime
ETC. ETC.
Next, I then have the other script which is run on an adhoc basis and this is the part I need help on:
declare #Period int
declare #PriorPeriod int
select #period = cur_per from tbm_Parms
if right(#Period, 2) <> '01'
begin
set #PriorPeriod = #Period - 1
end
exec [_SP_STOREDPROCEDURE] #PriorPeriod, #PriorPeriod
How does this work?
My stored procedure has #FromPeriod and #ToPeriod defined, yet using the adhoc script it is passing #PriorPeriod instead. It works perfectly but I don't understand how/why it works.
Any pearls of wisdom would be appreciated.
Thanks
Your stored procedure "[dbo].[_SP_STOREDPROCEDURE]" simply applies the same value to both parameters.
Your parameters are:
#FromPeriod int, #ToPeriod Int
You invoke with:
exec [_SP_STOREDPROCEDURE] #PriorPeriod, #PriorPeriod
Assuming that #PriorPeriod = 5, that is just the same as:
exec [_SP_STOREDPROCEDURE] 5, 5.
Now #FromPeriod contains 5. And #ToPeriod /also/ contains 5.
But you haven't given us enough code from _SP_STOREDPROCEDURE to explain why, according to you, that works perfectly. But I'm guessing that the answer is in the WHERE clause in your stored procedure.
Related
CREATE PROC add_person
(
#id tinyint,
#name nvarchar(max),
#surname nvarchar(max),
#salary int,
#job nvarchar(max)
)
AS
BEGIN
INSERT INTO information
VALUES(#id,#name,#surname,#salary,#job)
END
I want to write this code as a function. But the concept of "return" confuses me. That's why I couldn't.
I tried to write the code above as a function. This code came out.
CREATE FUNCTION add_person
(
#id tinyint,
#name nvarchar(max),
#surname nvarchar(max),
#salary int,
#job nvarchar(max)
)
RETURNS TABLE
AS
BEGIN
RETURN INSERT INTO information -- not work
VALUES(#id,#name,#surname,#salary,#job)
END
If you want to return the newly created table, you can use the stored procedure to do that. If you're using SQL Server, the code would be:
BEGIN
INSERT INTO information -- not work
VALUES(#id,#name,#surname,#salary,#job);
SELECT * FROM information WHERE id = ##identity; -- this is the primary key just created.
END
Functions are much more limited in their functionality than are stored procedures.
Although insert is allowed, it is only allowed in local variables. As the documentation says:
INSERT, UPDATE, and DELETE statements modifying local table variables.
On the other hand, a stored procedure can return a value. Normally, this is a status code, where 0 means everything succeeded, and any other value means that the process failed.
In a stored procedure, is setting something a parameter and declaring something different?
For example: if I needed to make something a parameter which would be right or wrong?
Alter Prodedure dbo.users_checkin
#userid INT
AS
Begin
OR
Alter Proc
Begin
Declare #userid INT
Declare #date1 DATE
Thank you!
I've got two stored procedures:
SP1
CREATE PROCEDURE GetAge
#Birthday datetime,
#BirthDayAge INT OUTPUT
AS
SELECT #BirthDayAge = YEAR(GETDATE()-DATEPART(dy, #Birthday) + 1)-YEAR(#Birthday);
SP2
CREATE PROCEDURE AgeProc
#Age int
AS
DECLARE #BirthDayAge INT;
EXEC GetAge #Age, #BirthDayAge OUTPUT
SELECT ... FROM ... WHERE #BirthdayAge = #Age;
For some reason or other, no results are being returned in the second procedure when tested. Am I doing something wrong in either of the stored procedures?
WHERE #BirthdayAge = #Age;
you are comparing 2 variables.
Shouldnt one of this be a table column?
also, you are passing an integer to a datetime, that may cause issues
Environment sql server 2005 sp3
I have a stored proc that takes an INT as input. I want to CAST a CHAR to an INT during the call to the stored proc. it seems I cannot do that. I get a syntax error before #foo. I do not see it can someone help me find it please.
CREATE PROCEDURE testme
#test AS INT
AS
BEGIN
SELECT #TEST
END
DECLARE #foo AS CHAR(6)
set #foo = '11test'
EXEC testMe #test = CAST(Substring(#foo,1,2) as int)
first off all, you can't cast '11test' as an int
second, if the value can be converted to an int, you don't need to cast, an implicit cast will happen
DECLARE #foo AS CHAR(6)
set #foo = '2'
EXEC testMe #test =#foo
If you want to test if it can be converted to an int, grab the IsInt function from here: IsNumeric, IsInt, IsNumber and use that to test before making the proc call
EDIT
here is how you can do it
DECLARE #foo AS CHAR(6)
set #foo = '11test'
SET #foo = CAST(Substring(#foo,1,2) as int)
EXEC testMe #test = #foo
you can't pass functions to procs, this is why GETDATE() doesn't work either, either use an intermediate variable or cast back to the same variable
You cannot convert a char field to int when it contains non-numeric characters.
I would suggest creating a function that loops through the chars and removes any that are non-numeric.
Created a stored procedure in SQL 9 (2005) and have since upgraded to SQL 10 (2008). Since then, the following stored procedure has stopped working and thrown up the above error:
ALTER PROCEDURE [dbo].[GetModifiedPages]
#vPortalUID nvarchar(32) = ''
AS
BEGIN
-- Convert GUID to UI
DECLARE #nPortalUID AS uniqueidentifier
SET #nPortalUID = CAST(#vPortalUID AS uniqueidentifier)
The passed in param #vPortalUID contains: 2A66057D-F4E5-4E2B-B2F1-38C51A96D385. I execute the stored proc like this:
EXEC GetModifiedPages '2A66057D-F4E5-4E2B-B2F1-38C51A96D385'
It falls over. I have tried Convert aswell. Still no joy. Have also had the value going in with { } around it. I removed these programatically and manually as above.
If you are interested I am running the SP from an ASP Classic page, although that should not affect this as the above code was run using SSMS.
Thanks in advance for your help.
James
this fails:
DECLARE #vPortalUID NVARCHAR(32)
SET #vPortalUID='2A66057D-F4E5-4E2B-B2F1-38C51A96D385'
DECLARE #nPortalUID AS UNIQUEIDENTIFIER
SET #nPortalUID = CAST(#vPortalUID AS uniqueidentifier)
PRINT #nPortalUID
this works
DECLARE #vPortalUID NVARCHAR(36)
SET #vPortalUID='2A66057D-F4E5-4E2B-B2F1-38C51A96D385'
DECLARE #nPortalUID AS UNIQUEIDENTIFIER
SET #nPortalUID = CAST(#vPortalUID AS UNIQUEIDENTIFIER)
PRINT #nPortalUID
the difference is NVARCHAR(36), your input parameter is too small!