SQL server從入門精通---- T-sql基本語句+函數與預存程序

來源:互聯網
上載者:User

標籤:

-----------------T_SQL--------------------------------1.全域變數-------------------------------------print @@identity --最後一次插入的標識值print @@language --當前使用語言print @@version --版本號碼print @@servername--服務名--2.自訂變數    ----1.聲明用declare        declare @i int,@j int        declare @sum int    ----2.賦值set,select        set @i=10        select @j=20        --把i與j的值加起來,並列印        set @sum=@i+@j        print @sum        --把最低分尋找出來,列印 ------while 迴圈語句declare @stat int,@end int,@count intset @end=10set @count=0set @stat=0while(@stat<=@end)begin     --裡面可以執行任何語句    set @count=@stat+@count    set @stat=@stat+1    insert into t_buyer(baid) values(@count)endprint @count------------定時命令------------------waitfor delay ‘00:00:3‘--延時3秒執行print ‘3..2..1 go!‘Select CONVERT(varchar(100), GETDATE(), 24)waitfor time ‘11:40:56‘      --10:57:47    --到時執行print ‘hell0!‘-------case語句--用法1select case when len(bquertion)>0 then ‘有值‘                 else ‘沒值‘ end,* from t_buyerselect * ,等級=case        when score>80 then ‘優秀‘        when score>=60 then ‘良好‘        else ‘不及格‘         endfrom Grade--用法二select *,稱呼=case sex when ‘男‘ then ‘大帥哥‘                       else ‘小美女‘ end---函數create function fn_Sum(---參數列表    @aa int ,    @bb int)returns int --指定傳回型別as begin--方法體    declare @s int    set @s=@aa+@bb    return @senddeclare @x int set @x =dbo.fn_Sum(10,20)print @x-------------------動態執行-------------------declare @sql varchar(400)set @sql=‘select * from student‘-----將字串當做sql server 中的語句來執行exec(@sql)-------只要@sql中的語句符合sql的文法就可以--預存程序--無參預存程序create proc up_printasprint 1go--帶參函數create proc up_print1  --(在sql中預存程序不能同名)@str nvarchar(20)as print @strgoexec up_printexec up_print1 ‘你好啊‘--帶輸出參數的預存程序create procedure up_print2@xing nvarchar(1),@ming nvarchar(5),@rtn nvarchar(6) outputasset @rtn=@xing+@minggodeclare @rtn nvarchar(6)exec up_print2 ‘李‘,‘濤‘,@rtn outputprint @rtn----寫一個新增學生的存數過程up_insertStu   if (OBJECT_ID(‘up_insertStu‘,‘p‘))is not nulldrop proc up_insertStugocreate proc up_insertStu    @stuname varchar(20),    @sex varchar(2),    @stuno varchar(40),    @birthday datetime,    @remark text    asbegin try    declare @id int    --1.插入資料    insert into student values(@stuname,@sex,@stuno,@birthday,@remark)    --2.為剛才這個的、學生添加一個英語隨機分數    set @id =@@IDENTITY       insert into grade values(‘英語‘,RAND()*100,@id)end trybegin catch    print ‘執行出錯,違反約束,錯誤號碼:‘+convert(varchar,@@error)end catchgo---------------------------------調用----------------------------------exec up_insertStu ‘濤濤‘,‘男‘,‘2014140208‘,‘1991-1-1‘,‘無‘

 

SQL server從入門精通---- T-sql基本語句+函數與預存程序

聯繫我們

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