sp_sqlexec - 指定的许多参数

时间:2016-05-17 10:20:13

标签: sql sql-server

我收到错误'指定的参数太多'但找不到问题

非常感谢任何帮助

3个参数在程序开始时指定

DECLARE
 @WhereStatment nvarchar(500),
 @ParamList nvarchar(500) = N'@p1 int, @p2 varchar(256), @p3 int'

SET @WhereStatment = ''

-- Check for Status Filter
IF @StatusFilter <> 'All'
SET @WhereStatment = @WhereStatment + ' EntryStatus =  @StatusFilter'
ELSE
BEGIN
SET @StatusFilter = 'Invoiced (Complete)'
SET @WhereStatment = @WhereStatment + ' EntryStatus <> @StatusFilter'
END

-- Check for User Filter
IF @UserFilter > 0 
SET @WhereStatment = @WhereStatment + ' AND EobID = @UserFilter'

-- Check for Warehouse Filter
IF @WarehouseFilter > 0
SET @WhereStatment = @WhereStatment + ' AND WarehouseNumber = @WarehouseFilter'

-- Add WhereStatement
Set @WhereStatment = '
SELECT 
tblWCS_SQE_Entries.RowID, SaleNum, tblWCS_Company_UserAccounts.Fullname, Ets

FROM tblWCS_SQE_Entries

INNER JOIN tblWCS_Company_UserAccounts
ON tblWCS_SQE_Entries.EobID = tblWCS_Company_UserAccounts.RowID

WHERE  
'
+ @WhereStatment;

EXEC sp_sqlexec @WhereStatment, @ParamList,
                @p1 = @UserFilter, 
                @p2 = @StatusFilter, 
                @p3 = @WarehouseFilter

2 个答案:

答案 0 :(得分:2)

sp_sqlexec存储过程确实只有一个参数。可能你想要sp_executesql

答案 1 :(得分:1)

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = '
SELECT tblWCS_SQE_Entries.RowID, SaleNum, tblWCS_Company_UserAccounts.Fullname, Ets
FROM dbo.tblWCS_SQE_Entries
JOIN dbo.tblWCS_Company_UserAccounts ON tblWCS_SQE_Entries.EobID = tblWCS_Company_UserAccounts.RowID
WHERE '
+ CASE WHEN @StatusFilter = 'All'
    THEN ' EntryStatus <> ''Invoiced (Complete)'''
    ELSE ' EntryStatus =  @StatusFilter'
  END
+ CASE WHEN @WarehouseFilter > 0 THEN ' AND WarehouseNumber = @WarehouseFilter' ELSE '' END
+ CASE WHEN @UserFilter > 0 THEN ' AND EobID = @UserFilter' ELSE '' END

EXEC sys.sp_executesql @SQL
                     , N'@p1 int, @p2 varchar(256), @p3 int'
                     , @p1 = @UserFilter
                     , @p2 = @StatusFilter
                     , @p3 = @WarehouseFilter