简体   繁体   English

如何将SELECT语句作为参数传递给SQL Server中的存储过程?

[英]How to pass the SELECT statement as a parameter to stored procedure in SQL Server?

What is wrong with my code? 我的代码有什么问题? I want to pass the select statement and where clause variable values: 我想传递选择语句和where子句变量值:

ALTER PROC sp_ac_getItemLookupCode 
    @itemlookupcode varchar(20) = null,
    @select varchar(30)
AS
BEGIN
    SELECT DISTINCTT
        @select
    FROM
        [ReportHQMatajer].[dbo].[JFC_ItemDailySalesParent] pp
    WHERE 
        ItemLookupCode LIKE @itemlookupcode+'%' 
END

Above is my stored procedure. 以上是我的存储过程。 I execute the following code, but it returns the column Name only 我执行以下代码,但仅返回“ Name ”列

sp_ac_getItemLookupCode @select='ItemLookupCode',@itemlookupcode=1

It is not returning all the values. 它没有返回所有值。 It returns the column name as a result 结果返回列名

Use Dynamic query via using SP_EXECUTESQL system stored proecures as next:- 通过使用SP_EXECUTESQL系统存储的过程使用动态查询,如下所示:

ALTER PROC sp_ac_getItemLookupCode 
@itemlookupcode varchar(20)= null,
@select varchar(30)
AS
BEGIN
declare @Query nvarchar(2000)
set @Query = 
    'select distinct ' + @select + '
    FROM
        [ReportHQMatajer].[dbo].[JFC_ItemDailySalesParent] pp
    WHERE 
        ItemLookupCode like ''' + @itemlookupcode+ + '%'' '
exec sp_executesql @Query
END

If you want to pass column names as a parameter then you have to make your dynamic query, as next:- 如果要将列名作为参数传递,则必须进行动态查询,如下所示:

Exec ('select distinct '+@select+'
    FROM
        [ReportHQMatajer].[dbo].[JFC_ItemDailySalesParent] pp')

If you want to pass column names (or table names or function names or the like), you need to use dynamic SQL: 如果要传递列名(或表名或函数名等),则需要使用动态SQL:

ALTER PROC sp_ac_getItemLookupCode (
    @itemlookupcode varchar(20) = null,
    @select varchar(30)
) AS
BEGIN
    declare @sql = nvarchar(max);

    set @sql = '
select distinct @select
from [ReportHQMatajer].[dbo].[JFC_ItemDailySalesParent] pp
';

    set @sql = replace(@sql, '@select', @select);

    if (@itemlookupcode is not null)
    begin
        set @sql = @sql + 'where ItemLookupCode like ''' + @itemlookupcode + '%'''
    end;

    exec sp_executesql @sql;
END;

I added the functionality that if @itemlookupcode is NULL , then it returns all rows. 我添加了以下功能:如果@itemlookupcodeNULL ,则它将返回所有行。

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

相关问题 如何将SELECT语句结果作为参数传递给SQL Server存储过程中的函数? - How to pass SELECT statement result as parameter to function in stored procedure in SQL Server? SQL Server 2000-使用SELECT语句的存储过程参数值 - SQL Server 2000 - Stored procedure parameter values with a SELECT statement 如何将列的字符串数组作为参数传递给SQL Server存储过程 - How to pass string array of Columns as parameter to SQL Server Stored procedure 如何将模式作为参数传递给sql server中的存储过程? - How to pass schema as parameter to a stored procedure in sql server? 在SQL Server存储过程中循环执行select语句 - Loop the select statement in sql server stored procedure 是否可以将select语句的结果作为参数的值传递到存储过程中? - Can I pass the result of a select statement as a value for a parameter into a stored procedure? SQL Server存储过程中SELECT语句的ORDER BY子句中的IF语句 - IF statement in ORDER BY Clause of a SELECT Statement in a SQL Server Stored Procedure 如何在选择语句 SQL Server 中将列名作为参数传递 - How to pass column name as parameter in select statement SQL Server 如何将变量传递给 SQL 服务器存储过程? - How to pass a variable to SQL Server stored procedure? 如何在Select语句中的sql存储过程中设置值以输出参数? - How Can I Set Value To output parameter in sql stored procedure within select statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM