存储过程的动态参数 [英] Dynamic parameters to stored procedure

查看:32
本文介绍了存储过程的动态参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为存储过程创建动态参数时遇到问题.我在存储过程中有两个 nvarchar 参数,它们是用逗号分隔的单词组合.例如:

I have a problem in creating dynamic parameters to stored procedure. I have two nvarchar parameters in a stored procedure that are combination of words separated with comma. For example:

    'Login, check, take, return' and 'admin, yes, no, yes' 

我必须将这些参数包含在 where 子句中,如

I have to include these parameters in the where clause like

    login=admin and check=yes and take=no and return=yes

问题在于参数中的字数是动态的.它可以从 1 到 20 个字不等.

The problem is that the number of words in the parameters are dynamic. It can vary from 1 to 20 words.

有没有办法解决这个问题?

Is there any solution to handle this?

推荐答案

  • 定义 20 个参数,默认为 NULL
  • 只设置你想要的
  • 在 WHERE 子句中执行 (@Param IS NULL or Column = @Param)
  • 如果您有完全动态的 SQL 和随机参数名称,那么它就是其中之一

    If you have completely dynamic SQL and random parameter names then it's one of

    • 你不应该使用存储过程
    • 您需要为不同的用例编写更多的存储过程

    您不会有一个完全动态的 .net 方法吧?

    You wouldn't have a completely dynamic .net method would you?

    这篇关于存储过程的动态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆