程序需要参数' @ params'类型&n; ntext / nchar / nvarchar'

时间:2017-03-08 12:42:43

标签: sql-server vb.net stored-procedures

错误:过程需要参数' @ params'类型&n; ntext / nchar / nvarchar'。

目标:根据Sql传回一个值

代码:(VB.NET)

With command
    .Connection = connection
    .CommandText = "spAuditLoggerSystemUser" 'include audit names
    .CommandType = CommandType.StoredProcedure
    .Parameters.Clear()
    .Parameters.AddWithValue("@EmployeeID", CInt(vEmployeeID))

    Dim parameter As SqlParameter = command.Parameters.Add("@LineID", SqlDbType.Int)
    parameter.Direction = ParameterDirection.Output
    command.ExecuteNonQuery()
    vLineID = command.Parameters("@LineID").Value.ToString()

    .ExecuteNonQuery()
End With

存储过程:

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spAuditLoggerSystemUser]
(
    @LineID INT OUTPUT,
    @EmployeeID int  
)

AS

BEGIN

SET NOCOUNT ON;
DECLARE @SQL NVARCHAR(MAX) 


SELECT @SQL = 'SELECT ID  
FROM taylorwoodrow.dbo.TblEmployeeDetails WHERE EmployeeID = ''' + (LTRIM(RTRIM(@EmployeeID))) + '''';

EXEC sp_executesql @SQL,@LineID OUT; 
PRINT @LineID 

END 

如果执行以下操作,我将返回(正确的)打印值:

DECLARE @SQL NVARCHAR(MAX)
DECLARE @EmployeeID AS INT
DECLARE @LineID NVARCHAR(MAX)
SET @EmployeeID = '1213'

SELECT @SQL = 'SELECT ID  
FROM DB.dbo.TblEmployeeDetails WHERE EmployeeID = ''' + (LTRIM(RTRIM(@EmployeeID))) + '''';

EXEC sp_executesql @SQL,@LineID OUT; 
PRINT @LineID

我使用Execute stored procedure with an Output parameter?作为参考

1 个答案:

答案 0 :(得分:1)

您无需在存储过程中使用动态SQL。尝试:

ALTER PROCEDURE [dbo].[spAuditLoggerSystemUser]
(
    @LineID INT OUTPUT,
    @EmployeeID int  
)

AS

BEGIN

SET NOCOUNT ON;

SELECT @LineID = ID  
FROM taylorwoodrow.dbo.TblEmployeeDetails 
WHERE EmployeeID = @EmployeeID 

END