如何建立動態資料列–【葉子】

來源:互聯網
上載者:User

首先我先寫一個小例子

 

 declare @table table(a int,b int)

 insert into @table

 select 2,3

 

 select a,b,a+b as col_add,a*b as col_ji from @table

 

 /*結果

 a           b           col_add     col_ji

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

2           3           5           6

 */

 

 我們可以看到後兩列是前面資料運算後的結果

 

 我們可以取出這樣的結果,但是結果是在取資料的時候,進行運算的。

 當資料量大的時候這個運算時需要很長時候的。

 

 那換個思路考慮一下,我們是否可以在建立資料的時候就進行運算。

 

 我們寫個簡單的例子:

 

 --建立表

 create table [dbo].[table_name]

 (  

  [a] [int] null,  

  [b] [int] null,  

  [col_add] as ([a]+[b]),

  [col_ji] as ([a]*[b])

 )

 on [primary]

  

--插入資料

insert into table_name(a,b)

select 2,3

 

--查詢出結果

select * from  table_name

 

--刪掉表

drop table table_name

 

/*

a           b           col_add     col_ji

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

2           3           5           6

*/

 

這樣當每插入一條記錄的時候,就進行一次計算。

 

@【葉子】http://blog.csdn.net/maco_wang 原創作品,轉貼請註明作者和出處,留此資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.