DECLARE @Temp nvarchar(4000)<br>DECLARE @Sql nvarchar(4000)<br>set @Temp=N''<br>DECLARE Employee_Cursor CURSOR FOR<br>--SELECT item_name FROM t_item<br>select item_name from t_Item where item_name in(<br>select name from syscolumns where id=object_id('t_City'))<br>OPEN Employee_Cursor<br>FETCH NEXT FROM Employee_Cursor INTO @AA<br>WHILE @@FETCH_STATUS = 0<br>BEGIN<br> IF @Temp = N'' <br>BEGIN<br> Set @Temp = N'SELECT '+ rtrim(@AA)<br> END<br> ELSE<br> BEGIN<br> Set @Temp = @Temp +N','+ rtrim(@AA)<br> END<br> FETCH NEXT FROM Employee_Cursor INTO @AA<br>END<br>Set @Temp = rtrim(@Temp) + N' FROM t_city'<br><br>Print @Temp<br><br>EXEC sp_executesql @Temp <br><br><br>CLOSE Employee_Cursor<br>DEALLOCATE Employee_Cursor<br><br><br><br><br><br><br><br><br><br>=============================<br>--行列转换 行转列<br>DROP TABLE t_change_lc;<br>CREATE TABLE t_change_lc (card_code VARCHAR2(3), q NUMBER, bal NUMBER);<br><br>INSERT INTO t_change_lc <br>SELECT 001 card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal FROM dual CONNECT BY <br><br>ROWNUM <= 4<br>UNION <br>SELECT 002 card_code, ROWNUM q, trunc(dbms_random.VALUE * 100) bal FROM dual CONNECT BY <br><br>ROWNUM <= 4;<br><br>SELECT * FROM t_change_lc;<br><br>SELECT a.card_code,<br>SUM(decode(a.q, 1, a.bal, 0)) q1,<br>SUM(decode(a.q, 2, a.bal, 0)) q2,<br>SUM(decode(a.q, 3, a.bal, 0)) q3,<br>SUM(decode(a.q, 4, a.bal, 0)) q4<br>FROM t_change_lc a<br>GROUP BY a.card_code<br>ORDER BY 1;<br><br>--行列转换 列转行<br>DROP TABLE t_change_cl;<br>CREATE TABLE t_change_cl AS <br>SELECT a.card_code,<br>SUM(decode(a.q, 1, a.bal, 0)) q1,<br>SUM(decode(a.q, 2, a.bal, 0)) q2,<br>SUM(decode(a.q, 3, a.bal, 0)) q3,<br>SUM(decode(a.q, 4, a.bal, 0)) q4<br>FROM t_change_lc a<br>GROUP BY a.card_code<br>ORDER BY 1;<br><br>SELECT * FROM t_change_cl;<br><br>SELECT t.card_code,<br>t.rn q,<br>decode(t.rn, 1, t.q1, 2, t.q2, 3, t.q3, 4, t.q4) bal<br>FROM (SELECT a.*, b.rn<br>FROM t_change_cl a,<br>(SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM <= 4) b) t<br>ORDER BY 1, 2;<br><br>--行列转换 行转列 合并<br>DROP TABLE t_change_lc_comma;<br>CREATE TABLE t_change_lc_comma AS SELECT card_code,quarter_||q AS q FROM t_change_lc; <br><br>SELECT * FROM t_change_lc_comma;<br><br>SELECT t1.card_code, substr(MAX(sys_connect_by_path(t1.q,
), 2) q<br>FROM (SELECT a.card_code,<br>a.q,<br>row_number() over(PARTITION BY a.card_code ORDER BY a.q) rn<br>FROM t_change_lc_comma a) t1<br>START WITH t1.rn = 1<br>CONNECT BY t1.card_code = PRIOR t1.card_code<br>AND t1.rn - 1 = PRIOR t1.rn<br>GROUP BY t1.card_code;<br><br>--行列转换 列转行 分割<br>DROP TABLE t_change_cl_comma;<br>CREATE TABLE t_change_cl_comma AS<br>SELECT t1.card_code, substr(MAX(sys_connect_by_path(t1.q,
), 2) q<br>FROM (SELECT a.card_code,<br>a.q,<br>row_number() over(PARTITION BY a.card_code ORDER BY a.q) rn<br>FROM t_change_lc_comma a) t1<br>START WITH t1.rn = 1<br>CONNECT BY t1.card_code = PRIOR t1.card_code<br>AND t1.rn - 1 = PRIOR t1.rn<br>GROUP BY t1.card_code;<br><br>SELECT * FROM t_change_cl_comma;<br><br>SELECT t.card_code,<br>substr(t.q,<br>instr(; || t.q, ;, 1, rn),<br>instr(t.q || ;, ;, 1, rn) - instr(; || t.q, ;, 1, rn)) q<br>FROM (SELECT a.card_code, a.q, b.rn<br>FROM t_change_cl_comma a,<br>(SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM <= 100) b<br>WHERE instr(; || a.q, ;, 1, rn) > 0) t<br>ORDER BY 1, 2;