存储过程中调用函数 [英] calling function in stored procedure

查看:292
本文介绍了存储过程中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮忙吗?我如何在c#中的存储过程中调用函数?

Can anyone help? How can I call a function in a stored procedure in c#?

推荐答案

假设您有一个函数名dbo.ufn_SplitText(),它的值是NVARCHAR(MAX), NVARCHAR(1),说它采用类似
的值 "abc,cde,def"和,"并返回

abc
cde
def

因此,您需要在存储过程(如
)中调用此函数
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'','','')



如果您需要从其他表中获取函数值,请使用ths:

假设表dbo.tbl_test包含"ID和值"列,而您所需的列号为@colnumber INT



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


您是在谈论数据库中用户定义的函数udf还是程序中的函数.
当您从存储过程中调用udf时,您可以直接使用存储过程中的代码进行调用.
在sql服务器中,从存储过程调用udf可能像这样
-假设存储过程将两个值传递给函数以获取
两个数字的和.
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


如果您创建了StoredProcedure并想在SQLServer中执行它,则需要使用
exec或execute Command,然后执行Storedprocedure

并在C#中使用,您需要使用CommandType定义它必须用作StoredProcedure,如下所示


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;




有关更多信息,请访问下面的链接,以获取有关C#存储过程的大量解决方案
单击此处 [




for More information Navigate the below link to get lots of Solution about stored procedure with C#
Click Here[^]


这篇关于存储过程中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆