根据Case或If语句选择列



我不想根据条件/参数显示列我想要的是

if(@SelectFlag==true)
    select name,Address,salary,CreatedBy from employee
else (@UpdateFlag==true)
    select name,Address,salary from employee    
    -- in this case i don't require the column "CreatedBy"

在上述情况下,我如何进行查询或存储过程。。

谢谢&当做

使用单个=1而不是true

if @SelectFlag = 1
  select name, Address, salary, CreatedBy from employee
else
if @UpdateFlag = 1
  select name, Address, salary from employee    

这里有一个创建存储过程的例子,也许您只需要一个参数来指示您要做什么

CREATE PROCEDURE procedurenameHere(@EventFlag INTEGER)
AS
    -- @EventFlag
    -- 1 for SELECT
    -- 2 for UPDATE
    IF @EventFlag = 1
        SELECT  name,Address,salary,CreatedBy 
        FROM    employee
    ELSE IF @EventFlag = 2
        SELECT  name,Address,salary, NULL AS CreatedBy 
        FROM    employee
GO

执行:

EXEC procedurenameHere 1

试试这个

表:emp
列:姓名,地址,工资,信用通过

declare My_Block
    p_select_flag emp.SelectFlag%type;
    p_updateFlag  emp.UpdateFlag%type;
BEGIN
    select selectFlag into p_select_flag from emp where name='ASIF';
    select update into p_updateFlag from emp where name='ASIF';
IF p_select_flag = 'TRUE' THEN
    select name,Address,salary,CreatedBy from employee;
ELSIF UpdateFlag = 'TRUE' then
    select name,Address,salary from employee;
END IF;
END My_Block

U可以用参数在SP内部调用,并将一个参数作为FLAG作为Integer:传递

使用single=和1,2而不是true。

1表示选择,2表示更新

if @FLAG = 1
  select name, Address, salary, CreatedBy from employee
else
if @FLAG = 2
  select name, Address, salary from employee  

最新更新