Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone help? How can I call a function in a stored procedure in c#?
Posted
Updated 28-Oct-10 18:52pm
v2

Say you have a function name dbo.ufn_SplitText() it takes the value NVARCHAR(MAX) and NVARCHAR(1), say It Takes the value like
"abc,cde,def" and "," and it returns

abc
cde
def

So you need to call this function in stored procedure like
SELECT * FROM dbo.ufn_SplitText('abc,cde,def',',')


If you need to take the function value from other table then use like ths:

Suppose a table dbo.tbl_test contains ID and Values column and your required column number is @colnumber INT

DECLARE @Variable  NVARCHAR(MAX)
SELECT @Variable = [Values] FROM tbl_test WHERE ID = @colnumber
SELECT * FROM dbo.ufn_SplitText(@Variable,',')


If your separator is not ',' then use your separator like '/' ..
Hope this will help you. Good luck
 
Share this answer
 
v3
Comments
Hiren solanki 29-Oct-10 1:47am    
Good answer.
arindamrudra 29-Oct-10 1:49am    
Thanks Hiren
thatraja 29-Oct-10 1:54am    
Yeah, good answer
Are you talking about the user defined function udf in database or a function in the program.
When you are calling udf from the stored procedure you can simply call with the stored procedure as it is in the code.
In sql server calling udf from stored procedure may look like this
--assume the stored procedure pass two values to the function to get
sum of the two numbers.
create procedure addTwoNumber
@a int,
@b int
as
begin
declare @c int
--assume the function returns int
set @c = add(@b,@c)
print @c
end
 
Share this answer
 
v2
Comments
Hiren solanki 29-Oct-10 1:47am    
upto the point answer.
If you Created StoredProcedure and Want to execute it in SQLServer then you need to use
exec or execute Command followed by Storedprocedure

and to use in C# you need to use CommandType to define it have to use as a StoredProcedure as given below


SqlCommand cmd = new SqlCommand("StoredProcedureCommand",Connection);
           
 cmd.CommandType = CommandType.StoredProcedure;



for More information Navigate the below link to get lots of Solution about stored procedure with C#
Click Here[^]
 
Share this answer
 
I believe you want to call C# method from inside stored procedure. It depends on the database you are using and the version of it.

If it is SQL 2005/2008, you can .Net load assemblies and call the methods inside those assemblies. Here[^] is an article that will help you doing that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900