根据案例或If语句选择列

时间:2013-02-12 06:43:00

标签: sql sql-server sql-server-2008 tsql

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

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"

如何查看上述情况的查询或存储过程..

谢谢&问候..

4 个答案:

答案 0 :(得分:0)

这是创建存储过程的示例,也许您只需要一个参数来指示您要执行的操作

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

答案 1 :(得分:0)

使用单=1代替true

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

答案 2 :(得分:0)

试试这个

表: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

答案 3 :(得分:0)

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

使用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