文件名称:原创 T_SQL 动态SQL 结合 游标 多表查询
文件大小:7KB
文件格式:SQL
更新时间:2013-02-09 12:04:51
动态SQL 结合 游标 多表查询
OPEN H_BM_Cursor FETCH NEXT FROM H_BM_Cursor into @bmid,@bmmc -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN declare @strtemp varchar(50) -- Concatenate and display the current values in the variables. --PRINT ' when ' + cast(@bmid as varchar(20)) + ' then ' + @bmmc select @strtemp=' when ' + cast(@bmid as varchar(20)) + ' then ''' + @bmmc+'''' --select @strtemp select @strSql=@strSql+@strtemp -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM H_BM_Cursor into @bmid,@bmmc END CLOSE H_BM_Cursor select @strSql= @strSql+' end as ''原来部门'',case xbmid ' --select @strSql as 'goSql' DEALLOCATE H_BM_Cursor