开发者

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

开发者 https://www.devze.com 2023-03-25 03:27 出处:网络
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 proce

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消