動態關聯表

2021-04-17 03:22:22 字數 960 閱讀 8488

--示例資料

createtable[table](asysname,bvarchar(10))

insert[table]select'table_1','a'

union all    select'table_2','b'

createtabletable_1(aint)

inserttable_1select1

union all    select2

createtabletable_2(aint)

inserttable_2select3

union all    select4

go/*--問題說明:

table中,a欄位儲存著其他表的名稱

a欄位中,記錄和各個表的a欄位的最大值

即得到如下結果:

table.b  c

-------- -----------

a        1

b        2

--*/

--處理方法

declare@svarchar(8000)

set@s=''

select@s=@s+'when'''+a+'''then(selectmax(a)from['+a+'])'

from[table]groupbya

exec('selectb,c=casea'+@s+'endfrom[table]')

go--刪除測試

droptable[table],table_1,table_2

/*--測試結果

b         c          

---------------------

a         2

b         4

--*/

動態關聯表

示例資料 create table table a sysname,b varchar 10 insert table select table 1 a union all select table 2 b create table table 1 a int insert table 1 sele...

動態關聯表

最後再說一句。我在老闆那裡看見這句話,忍不住與大家分享 我喜歡在這個地方工作 microsoft 過去有過許多競爭者。還好,我們有很多博物館來收藏它們。microsoft 董事長 bill gates,forbes.com,2004 年 10 月 4 日。shawn msdn online 總編輯 ...

動態關聯表

示例資料 create table table a sysname,b varchar 10 insert table select table 1 a union all select table 2 b create table table 1 a int insert table 1 sele...