简体   繁体   English

如何从存储过程返回字符串值

[英]How to return string value from the stored procedure

Alter procedure S_Comp(@str1 varchar(20),@r varchar(100) out)
as
declare  @str2 varchar(100)
set @str2  ='welcome to sql server. Sql server is a product of Microsoft'
if(PATINDEX('%'+@str1 +'%',@str2)>0)
    return @str1+'present in the string'
else 
    return @str1+'not present'

I am executing the above stored procedure.我正在执行上面的存储过程。 I am getting the following error :我收到以下错误:

Msg 245, Level 16, State 1, Procedure S_Comp, Line 8 Conversion failed when converting the varchar value 'Amruthanot present' to data type int.消息 245,级别 16,状态 1,过程 S_Comp,第 8 行将 varchar 值“Amruthanot present”转换为数据类型 int 时转换失败。

Please do help me resolving this请帮我解决这个问题

You are placing your result in the RETURN value instead of in the passed @r value. 您将结果放在RETURN值而不是传递的@r值中。

From MSDN 来自MSDN

(RETURN) Is the integer value that is returned. (RETURN)返回的整数值。 Stored procedures can return an integer value to a calling procedure or an application. 存储过程可以将整数值返回给调用过程或应用程序。

Changing your procedure. 改变你的程序。

ALTER procedure S_Comp(@str1 varchar(20),@r varchar(100) out) as 

    declare @str2 varchar(100) 
    set @str2 ='welcome to sql server. Sql server is a product of Microsoft' 
    if(PATINDEX('%'+@str1 +'%',@str2)>0) 
        SELECT @r =  @str1+' present in the string' 
    else 
        SELECT @r = @str1+' not present'

Calling the procedure 调用程序

  DECLARE @r VARCHAR(100)
  EXEC S_Comp 'Test', @r OUTPUT
  SELECT @r

change your 改变你的

return @str1+'present in the string' ;

to

set @r = @str1+'present in the string' 

Use SELECT or an output parameter. 使用SELECT或输出参数。 More can be found here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=100201 更多信息可以在这里找到: http//www.sqlteam.com/forums/topic.asp?TOPIC_ID = 100201

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=sql-server-ver15

Is the integer value that is returned.是返回的整数值。 Stored procedures can return an integer value to a calling procedure or an application.存储过程可以向调用过程或应用程序返回一个整数值。

Syntax syntaxsql语法syntaxsql

RETURN [ integer_expression ]返回 [ integer_expression ]

it only returns integer values.它只返回整数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM