在sql语法里面定义变量

               DECLARE @EmployeeName varchar(50)
                set @EmployeeName = '{0}'
                DECLARE @departmentGuid varchar(50)
                declare @Leader int
                select @departmentGuid = g_id_bm,@Leader = bLeader  from sz_bm_ywy where s_mch = @EmployeeName 
                if (@@rowcount = 0) return

                if ( @Leader = 1 )
                    begin
                  declare @Depth int
                  select @Depth =  i_depth  from  sz_tree_bm where g_id = @departmentGuid
                  if (@@rowcount = 0) return
                  if(@Depth = 1)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent1 = @departmentGuid)
                  if(@Depth = 2)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent2 = @departmentGuid)
                  if(@Depth = 3)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent3 = @departmentGuid)
                  if(@Depth = 4)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent4 = @departmentGuid)
                  if(@Depth = 5)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent5 = @departmentGuid)
                  if(@Depth = 6)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent6 = @departmentGuid)
                  if(@Depth = 7)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent7 = @departmentGuid)
                  if(@Depth = 8)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent8 = @departmentGuid)
                  if(@Depth = 9)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent9 = @departmentGuid)
                  if(@Depth = 10)
                  select g_id from  sz_bm_ywy where g_id_bm in ( select g_id_expand from sz_tree_extend where g_id_parent10 = @departmentGuid)
                    end
                if ( @Leader = 0 )

                select g_id from  sz_bm_ywy where  s_mch = @EmployeeName

注解:

1.

begin

end

相当于C#里面的

{ }

2. (@@rowcount 是全局变量 系统自带的 返回执行的行数

3. 判断是否为空 使用下面的例子

if (@ReceiveMoney is null) set @ReceiveMoney = 0;

4. IF 分支可以这样使用

 if @InvoiceDate_old is null

     begin

       //do something

    end

else

 begin

       //do something

    end

 

 

posted @ 2011-02-24 20:36  郑文亮  阅读(1041)  评论(0编辑  收藏  举报