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.
Related
I have a stored procedure like
CREATE PROCEDURE GetSerial (#param1 int, #param2 int)
AS
BEGIN
-- do some insert/updates, so I can't use function
DECLARE #value AS int;
SET #value = 3;
return #value;
END
Now I declare a table variable
DECLARE #Serials AS TABLE
(
ID int,
Value int
)
Now I wanna fill this table like
INSERT INTO #Serials (ID, Value)
SELECT 1, GetSerial(1,2) -- *How can I call this?
So, can anyone help me how can i call the GetSerial stored procedure inside the SELECT statement to fill my table?
I recommend you avoid getting into this pattern/thinking, because stored procedures only return INTs, and those ints are really intended to describe how well the operation went, not a result/data from the operation. Example: 0 => failed, 1=> succeeded. Not GetAgeInYears() => 29
https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/return-data-from-a-stored-procedure?view=sql-server-2017 has a lot of info and concrete examples but in your specific case you'd need to execute the procedure and capture the result code into a variable then insert that:
DECLARE #ret INT;
EXEC #ret = GetSerial(1,2);
INSERT INTO #Serials VALUES(1, #ret);
Really you'd be better off using an output parameter or resultset if you have many values to return. See the above link for more
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.
I am using SQL Server 2012, i have a script by which i am inserting values to a table, in that script i have to convert the format of some DateTime variables on the basis of two parameters.
I can do it using CASE or if condition in sql. I am not allowed to make any Function or procedure in the database to which i can refer.
Is there any other way like creating a temporary function or temporary procedure within the script to apply condition alter the format for Datetime values?
Yes you can:
CREATE PROCEDURE #usp_TempOne
#Input INT,
#Output INT OUTPUT
as
SET #Output = #Input * 2
RETURN
GO
DECLARE #i INT = 10, #o INT;
EXEC #usp_TempOne #i, #o OUTPUT
SELECT #o
I have defined a table-valued function X with 11 parameters. Their types are nvarchar(30), nvarchar(30), datetime, datetime, nvarchar(15), nvarchar(4), xml, nvarchar(8), nvarchar(80), bit, and bit respectively. This is for Microsoft SQL Server 2012. When I call the function with
select * from
X('A','B','2014-01-01','2014-12-31',null,null,'<C><D>E</D></C>',null,null,1,0)
I run into this error:
Parameters were not supplied for the function X
It is apparently different from the following two:
An insufficient number of arguments were supplied for the procedure or function X
Procedure or function X has too many arguments specified.
Is this related to two of the intended parameter values being null? How can I overcome the problem and define/call a table-valued function such as this one with 11 parameters, some of which may carry null?
UPDATE The problem remains if I pass in arbitrary strings instead of null. So there must be another (perhaps stupid) mistake.
The correct way to define a function like the one you describe is the following:
CREATE FUNCTION X
(
-- Add the parameters for the function here
#a nvarchar(30),
#b nvarchar(30),
#c datetime,
#d datetime,
#e nvarchar(15),
#f nvarchar(4),
#g xml,
#h nvarchar(8),
#i nvarchar(80),
#j bit,
#k bit
)
RETURNS
#output TABLE
(
-- Add the column definitions for the TABLE variable here
data nvarchar(250)
)
AS
BEGIN
INSERT INTO #output (data)
VALUES (#a + #b)
RETURN
END
GO
Given the above definition, this:
select * from
X('A','B','2014-01-01','2014-12-31',null,null,'<C><D>E</D></C>',null,null,1,0)
yields the following result:
data
----
AB
If your function does not need any parameters, but you are still getting this error:
Parameters were not supplied for the function
select * from [dbo].[myfunction]
Try adding () after the function call:
select * from [dbo].[myfunction]()
I want to modify my inline function so that it can handle two variables as input. With just one it worked just fine.
FUNCTION [dbo].[TBL_UDF_HowOften]
(
-- Add the parameters for the function here
#membername as varchar(15),
#tablename as varchar(15)
)
RETURNS #ergebnis TABLE
(
participated float,
percent float,
WonWen1 float,
WonWen2 float,
WonWen3 float
)
AS
BEGIN
DECLARE #wintimes float
DECLARE a lot of other stuff...
SELECT #wintimes = COUNT(DidWin)
FROM #tablename
WHERE DidWin = 1 AND membername = #membername
... and so on
Well, #membername is recognized but #tablename is marked with "Must declare the table variable "#tablename"."
You can't use a scalar variable as the table name in a 'from' clause. You would need to use dynamic sql, which I do not think can be done inside a function.