如何在select查询中使用传递的参数到存储过程,参数名称与select查询中的字段名称相同 [英] How to use passed parameters to a stored procedure in select query with the parameter name same as the field name in the select query

查看:265
本文介绍了如何在select查询中使用传递的参数到存储过程,参数名称与select查询中的字段名称相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的存储过程:



 创建  PROCEDURE  DuplicationCheckAccountLedger( IN  aname  varchar  50 ),OUT errflag 位<​​/ span>,OUT errFieldName  varchar  10 ))
BEGIN
声明 cnt int 默认 0 ;
选择计数(名称)进入 cnt 来自 account_ledger 其中 name = aname;
如果 cnt = 0
那么
set errflag = 0 ;
set errFieldName = ' none';
else
set errflag = 1 ;
set errFieldName = ' name';
end if ;
end //





而不是将名称字段作为'aname',我想把它作为'名字'传递。查询想读作:

从account_ledger中选择count(name)到cnt,其中name = name;



但是,我知道上面的查询存在一些问题,左侧和右侧总是相等的,因为选择查询将引用两个表的字段。



有没有我们在c#中使用的'this'运算符这样的场景使用的前缀是什么?



我尝试了什么:



我试着谷歌搜索这个东西..但我找不到一个好的关键字来搜索这个查询。

解决方案

< blockquote>对表格字段使用点表示法:



 选择计数(  1 进入 cnt 来自 account_ledger 其中 account_ledger.name = aname; 


Here is my stored procedure:

CREATE PROCEDURE DuplicationCheckAccountLedger (IN aname varchar(50), OUT errflag bit, OUT errFieldName varchar(10))
BEGIN
	declare cnt int default 0;
	select count(name) into cnt from account_ledger where name = aname;	
	if cnt = 0 
	then		
		set errflag = 0;
		set errFieldName = 'none';
	else 
		set errflag = 1;
		set errFieldName = 'name';
        end if;
end //



Instead of passing name field as 'aname', I would like to pass it as 'name' only. The query would like to read as:
select count(name) into cnt from account_ledger where name = name;

But, I know there is some problem with the above query that left hand side and right hand side will always be equal since select query will refer to table's field for both.

Is there any prefix to be used for such scenarios like something 'this' operator as we use in c#?

What I have tried:

I tried googling for this thing.. but I am not finding a good keyword to search for this query.

解决方案

Use dot notation for table fields:

select count(1) into cnt from account_ledger where account_ledger.name = aname;	


这篇关于如何在select查询中使用传递的参数到存储过程,参数名称与select查询中的字段名称相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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