sql - 如何在 sql server 2008 中声明时在游标中使用 if 条件?

标签 sql sql-server sql-server-2008

我有如下查询

Declare Cur_1 cursor for 

        if @EmployeeId <> is not null
            begin
                select EmployeeID from tbl_Employee where EEmployeeStatus='Active' and EmployeeID = @EmployeeId
            end
        else
            begin
                select EmployeeID from tbl_Employee where EEmployeeStatus='Active'
            end

Open Cur_1

    Fetch next from Cur_1 into @EmpId   
    While @@FETCH_STATUS=0
    Begin

    end
Close Cur_1
deallocate Cur_1

但它不起作用。它告诉我语法错误。

它在 if 条件下显示语法错误..

请帮帮我......

最佳答案

 if @Feild is not null
      begin
            Declare your_Cursor_Name cursor for     
                    select Feild1 from Your_table where 1=1
        end
    else
        begin
            Declare your_Cursor_Name cursor for     
                    select Feild1 from Your_table where 1=1
         end

    Open your_Cursor_Name 

        Fetch next from your_Cursor_Name into @xyz
        While @@FETCH_STATUS=0
        Begin
             `enter code here
        end
Close your_Cursor_Name 

关于sql - 如何在 sql server 2008 中声明时在游标中使用 if 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459501/

相关文章:

sql - 如何使用模式绑定(bind)创建存储过程?

sql-server - MS SQL 自动递增非标识列

超过 100 万行的 SQL Server BULK INSERT - 需要性能改进

sql-server - 为什么 SSIS 忽略我从 Excel 源导入的最后一列?

mysql - 创建触发器时出现语法错误

java - Hibernate 自定义查找器 SQL 查询不返回 OneToMany 关系

sql - 没有证书的 MS SQL 列加密

php - 使用 PHP 的 odbc 抓取一个 varchar(max) 列

sql - PLSQL - 字符串中所有字符的计数

sql - 如何从按一列分组的 MySQL 表中选择行,并在另一列中选择所需值