将操作作为参数添加到Sql存储过程 [英] Add operations as parameter to Sql store procedure

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

问题描述

我想在sql中创建一个动态过滤器



在哪里



i想要例如ID(>,<,> =,< =,=)@ ID这些操作由用户动态更改为界面中的下拉列表



有什么建议吗?



我用这个作为选择



I want to create a dynamic filter in sql

on the where

i want for example "ID" (>,<,>=,<=,=) @ID where these operation changed dynamically by the user as a dropdownlist in the interface

Any suggestions?

im using this as select

 @v1 varchar(50),
	   @v2 int 

DECLARE cur cursor for
SELECT abc
	FROM 
	sometable
	WHERE  
		(@lv1 IS NULL OR @v1 =@lv1) 
	AND (@lv2 IS NULL OR @v2 =@lv2) 





i希望在这些运算符上过滤v2参数(>,<,> =,< =,=)



i want the v2 parameter to be filtered on these operators (>,<,>=,<=,=)

推荐答案

创建sql查询作为字符串,然后通过exe语句执行exexute你也可以在那里执行
create sql query as string then exexute by exe statement in sql you can do rnd on that as well






我从来没有尝试过相同但可以做到的。从我的观点来看下面的评论



1.使用If Else,因为你将从前端传递参数。



2.您也可以通过使用动态SQL来检查它。









查看此更新版....







Hi,

I have never tried the same but it can be done. From my point of view check below comments

1. Using "If Else" as you will pass parameter from front end.

2. You can also check the same by making use of dynamic SQLs.




Check this updated one....



Quote:我添加了我的查询,你可以告诉我如何做上述评论
Quote:Ive added my query could u tell me how to do the above comments







--PR_DYNAMIC_OPERATORS '>'
ALTER PROC PR_DYNAMIC_OPERATORS
(
@PA_OPERATOR VARCHAR(100)
)
AS
BEGIN

--First Option
/*
IF @PA_OPERATOR = '='
BEGIN

SELECT 1 WHERE 1 = 1

END

IF @PA_OPERATOR = '>'
BEGIN

SELECT 1 WHERE 2 > 1

END



--.
--.
--.
--.
--.
--and So On

*/


--Second Option

DECLARE @SQL VARCHAR(200)

SET @SQL = 'SELECT 1 WHERE 2 ' + @PA_OPERATOR + ' 1'
PRINT(@SQL)
EXEC (@SQL)


END











希望这会对你有所帮助。



干杯






Hope this will help you.

Cheers


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

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