我有存储过程:
ALTER PROCEDURE ReplaceHtmlEntities
@companyName NVARCHAR(200)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @newCompanyName NVARCHAR(200);
SET @newCompanyName = @companyName;
SET @newCompanyName = REPLACE(@newCompanyName,'&','&');
SET @newCompanyName = REPLACE(@newCompanyName,'"','"');
SET @newCompanyName = REPLACE(@newCompanyName,''','''');
SELECT @newCompanyName;
END
GO
但它返回0:
DECLARE @companyName nvarchar(200), @result nvarchar(200)
SET @companyName = N'Company name &'
EXEC @result = [dbo].[ReplaceHtmlEntities] @companyName
SELECT @result
@result应该是'公司名称&'但它给了我' 0' 0 - 为什么?
答案 0 :(得分:1)
存储过程只能返回整数(ref)。
您可以从输出参数返回它,但我认为您应该创建一个函数:
CREATE FUNCTION ReplaceHtmlEntities
(@companyName NVARCHAR(200))
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @newCompanyName NVARCHAR(200);
SET @newCompanyName = @companyName;
SET @newCompanyName = REPLACE(@newCompanyName,'&','&');
SET @newCompanyName = REPLACE(@newCompanyName,'"','"');
SET @newCompanyName = REPLACE(@newCompanyName,''','''');
RETURN @newCompanyName;
END
GO
这就是你如何执行一个功能:
DECLARE @companyName nvarchar(200), @result nvarchar(200)
SET @companyName = N'Company name &'
SET @result = [dbo].[ReplaceHtmlEntities](@companyName)
SELECT @result
答案 1 :(得分:1)
在您调用它的地方,删除@result =
和行select @result
。
选择内部 SP将为您生成数据。
DECLARE @companyName nvarchar(200), @result nvarchar(200)
SET @companyName = N'Company name &'
EXEC [dbo].[ReplaceHtmlEntities] @companyName
但如果你把它变成了UDF,那就更好了,
Create Function dbo.ReplaceHtmlEntities(@input varChar(1000) )
returns table As
Return (Select Replace(Replace(REPLACE(
@input,'&','&'),
'"','"'),
''','''') OutVal)
然后你需要做的就是
Select OutVal from dbo.ReplaceHtmlEntities('Put the companyname here')
答案 2 :(得分:0)
SQL Server中的存储过程只能返回整数。存储过程结束时的SELECT将创建一个结果集而不是返回值。
要从存储过程输出值作为输出参数,请声明如下过程:
ALTER PROCEDURE ReplaceHtmlEntities
@companyName NVARCHAR(200),
@newCompanyName NVARCHAR(200) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET @newCompanyName = @companyName;
SET @newCompanyName = REPLACE(@newCompanyName,'&','&');
SET @newCompanyName = REPLACE(@newCompanyName,'"','"');
SET @newCompanyName = REPLACE(@newCompanyName,''','''');
END
GO
DECLARE @companyName nvarchar(200) = 'A & B'
EXEC ReplaceHtmlEntities @companyName = @companyName, @newCompanyName = @companyName OUTPUT
SELECT @companyName
GO