Как передать параметры для динамической функции и динамически выполнить хранимую процедуру с помощью EXEC()
Hi there, I have created a Function called split string which splits the delimited characters into row by row. This was done because I have created a stored procedure where it will not supports IN operator. And this function in the stored procedure is working fine. Now, I am planning to use the same function in another stored proc, this time I passed the parameters dynamically (keeping those where clause columns in the single quotes as shown below with EXEC(@parameter), but it is not working. MARKET IN (SELECT Item FROM dbo.SplitString( ' + @stringcolumn + ',' + ''''') here dbo.SplitString is a split function. I tried to print my parameters, in that the where clause is showing NULL. I removed that expression from the where clause and ran, then it is working fine with no errors. Please help me.
Что я уже пробовал:
Привет
Если я выберу столбец col1 (флажок), то он должен отображаться в отчете вместе со столбцами в списке выбора. Как мудрый для col2, col3 и т. д
вот мой код:
ALTER PROC [dbo].[spGetanswer] ( @a VARCHAR(10) = NULL, @b NVARCHAR(200) = NULL, @b VARCHAR(15) = NULL, @d NVARCHAR(MAX)= NULL, @StartDate VARCHAR(50) = NULL, @EndDate VARCHAR(50) = NULL, @col1 BIT = 0, @col2 BIT = 0, @col3 BIT = 0, @col4 BIT = 0, @col5 BIT = 0 ) AS SET NOCOUNT ON SET DATEFORMAT MDY DECLARE @SQLSELECT NVARCHAR(4000) DECLARE @SQLFROM NVARCHAR(100) DECLARE @SQLWHERE NVARCHAR(1000) DECLARE @SQLGROUP NVARCHAR(3000) DECLARE @SQLORDER NVARCHAR(3000) DECLARE @StringUnion NVARCHAR(MAX) SET @SQLSELECT = ' SELECT SUM(IsNull(SALES,0)) , IsNull(CUSTOMER,'''') as Customer ' SET @SQLFROM = ' FROM dbo.VW_getanswer ' SET @SQLWHERE= ' WHERE (CONVERT(datetime,[MONTH]) BETWEEN ' + '''' + @StartDate + '''' + ' AND ' + '''' + @EndDate + '''' + ' ) AND COMPANY =''' + @COMPANY + ''' 'AND b IN (SELECT Item FROM dbo.SplitString( ' + @b + ',' + ''''') AND c IN (SELECT Item FROM dbo.SplitString( ' + @c + ',' + ''''') ) AND d IN ( SELECT Item FROM dbo.SplitString( ' + @d + ',' + ''''')) ' SET @SQLGROUP = ' GROUP BY CUSTOMER ' SET @SQLORDER = ' ORDER By SUM(IsNull(SALES,0)) DESC , CUSTOMER ' IF (@col1 = 1 ) BEGIN SET @SQLSELECT = @SQLSELECT + ' ,col1 ' SET @SQLGROUP = @SQLGROUP + ' ,col1' SET @SQLORDER = @SQLORDER + ' ,col1 ' END IF (@col2 = 1 ) BEGIN SET @SQLSELECT = @SQLSELECT + ' , col2 ' SET @SQLGROUP = @SQLGROUP + ' , col2 ' SET @SQLORDER = @SQLORDER + ' , col2 ' END IF (@col3 = 1 ) BEGIN SET @SQLSELECT = @SQLSELECT + ' , col3 ' SET @SQLGROUP = @SQLGROUP + ' , col3 ' SET @SQLORDER = @SQLORDER + ' , col3 ' END IF (@col4 = 1 ) BEGIN SET @SQLSELECT = @SQLSELECT + ' , col4 ' SET @SQLGROUP = @SQLGROUP + ' , col4 ' SET @SQLORDER = @SQLORDER + ' , col4 ' END IF (@col5 = 1 ) BEGIN SET @SQLSELECT = @SQLSELECT + ' , col5 ' SET @SQLGROUP = @SQLGROUP + ' , col5 ' SET @SQLORDER = @SQLORDER + ' , col5 ' END SET @StringUnion = @SQLSELECT + @SQLFROM + @SQLWHERE + @SQLGROUP + @SQLORDER EXECUTE (@StringUnion)