How to call a scalar-valued from with a stored procedure - sql

say I have a scalar-valued function with takes two Guids as parameters:
dbo.CarDistribution
The two Guids area ModelId and AreaId.
The function returns another Guid.
Could someone please explain to me how I call this function within a stored procedure and set the result returned to a variable.
I tried calling it with:
CarDistribution('a Guid in here', 'another Guid in here')
but I get the error
'Incorrect syntax near 'speciesareapresence_eval_internal''
Thanks!

declare #NewGUID uniqueidentifier
Select #NewGUID = dbo.CarDistribution (ModelId, AreaId)
From dbo.YourTable

Related

SQL Server table-valued function Parameters were not supplied for the function-

I keep getting the error : "Parameters were not supplied" for a very simple table-valued function. I cannot figure out what is the issue. I narrowed the function down to :
create FUNCTION udf_XX_OddFCST()
RETURNS #output TABLE (
articlecode nvarchar(50)
)
AS
BEGIN
insert into #output(articlecode) values ('abc');
RETURN
END
So I get the error when executing
select * from udf_XX_OddFCST
Any help would be greatly appreciated.
kind regards
you need to use parentheses in your function call :
SELECT * FROM udf_XX_OddFCST()
however as it has been mentioned in the comments, it would be more simpler and more efficient using iTVF:
CREATE FUNCTION udf_XX_OddFCST()
RETURNS TABLE
AS
RETURN (select 'abc' as articlecode)
SELECT * FROM udf_XX_OddFCST()

Conversion failed : converting from a character string to uniqueidentifier

I have the following function which returns the following result set when this string is called.
SELECT item
FROM dbo.DelimitedSplit8K('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2',',')
Result set:
item
------------------------------------
985B773F-5E36-47D4-9E84-E0CE35B34337
32237666-86F3-41FD-BCDE-794571CDAEA2
Now the purpose of this function is to get the two or more IDs cause I will be passing multiple IDs from my C# program to one variable in my stored procedure.
Problem
The problem seems to be simple enough but I am not sure as to why it's occurring.
CREATE PROC [dbo].[usp_printMulitTest]
#multiApplicationId_FK uniqueidentifier = '',
#pDelimiter CHAR(1) = NULL
AS
;WITH image_CTE(imgBinary, imgCode, appID) AS
(
SELECT
[image], imageCode_FK, app
FROM
[dbo].Images
WHERE
CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER)
IN ((SELECT item
FROM dbo.DelimitedSplit8K(CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER),',')))
)
SELECT *
FROM image_CTE
This stored procedure works fine when I hard code the variables.
However, when I convert it to this
WHERE CAST(app AS UNIQUEIDENTIFIER)
IN((SELECT item FROM dbo.DelimitedSplit8K(CAST(#multiApplicationId_FK AS UNIQUEIDENTIFIER),',' )))
to get the results for the app IDs that I pass in, I get an error
Conversion failed when converting from a character string to uniqueidentifier
While looking for solutions two that were pointed out is the incorrect formation of the unique identifier and not using cast, however I checked the numbers and used a cast/convert and there has been no change.
Grateful for assistance in this.

Alter a SQL server function to accept new optional parameter

I already have a function in SQL Server 2005 as:
ALTER function [dbo].[fCalculateEstimateDate] (#vWorkOrderID numeric)
Returns varchar(100) AS
Begin
<Function Body>
End
I want to modify this function to accept addition optional parameter #ToDate. I am going to add logic in function if #Todate Provided then do something else continue with existing code.
I modified the function as:
ALTER function [dbo].[fCalculateEstimateDate] (#vWorkOrderID numeric,#ToDate DateTime=null)
Returns varchar(100) AS
Begin
<Function Body>
End
Now I can call function as:
SELECT dbo.fCalculateEstimateDate(647,GETDATE())
But it gives error on following call:
SELECT dbo.fCalculateEstimateDate(647)
as
An insufficient number of arguments were supplied for the procedure or
function dbo.fCalculateEstimateDate.
which as per my understanding should not happen.
Am I missing anything?
From CREATE FUNCTION:
When a parameter of the function has a default value, the keyword DEFAULT must be specified when the function is called to retrieve the default value. This behavior is different from using parameters with default values in stored procedures in which omitting the parameter also implies the default value.
So you need to do:
SELECT dbo.fCalculateEstimateDate(647,DEFAULT)
The way to keep SELECT dbo.fCalculateEstimateDate(647) call working is:
ALTER function [dbo].[fCalculateEstimateDate] (#vWorkOrderID numeric)
Returns varchar(100) AS
Declare #Result varchar(100)
SELECT #Result = [dbo].[fCalculateEstimateDate_v2] (#vWorkOrderID,DEFAULT)
Return #Result
Begin
End
CREATE function [dbo].[fCalculateEstimateDate_v2] (#vWorkOrderID numeric,#ToDate DateTime=null)
Returns varchar(100) AS
Begin
<Function Body>
End
I have found the EXECUTE command as suggested here T-SQL - function with default parameters to work well. With this approach there is no 'DEFAULT' needed when calling the function, you just omit the parameter as you would with a stored procedure.

Using SYSTEM_USER as a parameter to a table valued function

I've created a table valued function:
CREATE FUNCTION TestFunction
(
#username VARCHAR(80)
)
RETURNS TABLE
AS
RETURN
(
SELECT 0 AS TestValue
)
Then try to call it as so:
SELECT TestValue
FROM dbo.TestFunction(SYSTEM_USER)
but get the error:
Incorrect syntax near the keyword 'SYSTEM_USER'
I've even tried making this a table valued function which is not inline, but I get the same error.
Am I missing something? Why am I getting this error?
On my 2k8 server I can only reproduce that with SQL Server 2000 (80) Compatibility level set, check the level of your 2005 database.
Meantime you can;
declare #su varchar(30) = SYSTEM_USER
select * from dbo.TestFunction(#su)

Passing parameters from Stored Procedure to Function (inside Stored Procedure)

I have a problem and I have researched Stackoverflow for answers without any luck. I have manage to build a Stored Procedure that uses a function called GetSubtree_relvalue within it. The stored procedure and its function works just fine when the function has its parameters hard coded. But now I want the function to inherent the parameters that is sent in with the Stored procedure when it is executed.
When I replace XX and USA with #attribute_id and USA with #Client it doesn't return anything. I have debugged it so far so that I can conclude that the function doesn't get any values with it when it runs. This, even though these parameters in the Stored Procedure carries exactly the same value as the one that I have hard coded . (I checked with a regular select #client within the Procedure and it returns USA.)
Am I parametrizing the function wrongly? Do I need to initiate the functions parameter to be able to send it/pass it on to the function? How do get functions to inhere parameters?
I would be glad for all input regarding passing parameters forward from SP to functions.
Using SQL server 2008
Thanks
/Daniel
Function values hard coded
Insert into att_value_lookup (t.attribute_ID, t.att_value)
Select
t.attribute_ID, t.att_value
From
(Select attribute_id, att_value from relvalue
where attribute_id in (
Select attribute_id from (
select attribute_id
from dbo.GetSubtree_relvalue('XX','USA'))
) as t
Function values parameterized
Insert into att_value_lookup (t.attribute_ID, t.att_value)
Select
t.attribute_ID, t.att_value
From
(Select attribute_id, att_value from relvalue
where attribute_id in (
Select attribute_id from (
select attribute_id
from dbo.GetSubtree_relvalue('#attribute_id','#client'))
) as t
remove the quotes!!
dbo.GetSubtree_relvalue('#attribute_id','#client')
should be:
dbo.GetSubtree_relvalue(#attribute_id,#client)
you only need them when passing in literal string values like 'XX' and 'USA', not when passing in variables. You were actually passing in strings that contained the variable names: '#attribute_id', '#client' and not the values contained within the variables.
you can check it out:
DECLARE #x varchar(10)
SET #x='wow wee!!'
print '#x'
print #x
Remove the quotes from around your parameters. You're currently passing strings that happen to be the names of the parameters, but not passing the parameters themselves.
from dbo.GetSubtree_relvalue(#attribute_id,#client)