Sql_server基本操作

來源:互聯網
上載者:User

標籤:const   group   類型   and   nbsp   ret   select   weight   class   

使用Sql_server建立表,視圖,觸發器,預存程序,函數等基本操作。

 

create table test1(                  /* 建立一個表 */    num int)   alter table test1                    /* 修改表 */alter column num int not null        /* 修改某一列 */alter table test1                    /* 修改表 */add constraint pk_num                /* 添加約束 */primary key(num);                    /* 主鍵約束 */create trigger insert_test1          /* 建立觸發器 */on test1 for insert                  /* 當test1有添加資料時觸發 */as                                   /* as 以後時sql語句 */begin    print ‘success!‘endselect * into test1Bak               /* 建立備份表 */from test1where 1 = 2                          /* 備份為空白表 */create view s1                      /* 建立視圖 s1 */        as                           /* 注意,這個as不可省略 */            select *from jkcreate proc procSumByPurchase                    @Gname nvarchar(50),                           /* 指定多個參數 */    @name nvarchar(50),    @num int output                                /* 輸出參數 */asbegin    select @num = (        select sum(s.Sell_num)        from Sell s inner join Goods g        on g.Goo_no = s.Goo_no        group by g.Goo_name, s.Sell_date, g.Pro_name        having g.Goo_name = @name        and g.Pro_name = @Gname        and    year(s.Sell_date) = 2018            /* date篩選年份 */        and month(s.Sell_date) = 1                 /* date篩選月份 */    )enddeclare @num1 int                        exec procSumByPurchase ‘聯想公司‘, ‘拯救者15.6英寸輕薄遊戲本‘, @num1 outputselect ‘SumNum‘ = str(@num1)                    /* 將返回的 int 型轉變成 字串 */create function Purchase_Total(@start datetime,        /* 自訂函數 */    @last datetime)                                    /* 可多個參數 */    returns table                                      /* 傳回值類型,這裡為表格 */as                                                     /* as以後為 sql 語句 */    return(                                            /* 最後為傳回型別 */        select *        from Purchase p        where p.Pur_date >= @start        and p.Pur_date <= @last    )

 

Sql_server基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.