有關儲存過程的幾個知識點總結

2021-05-25 15:38:12 字數 1911 閱讀 9510

這兩天就寫儲存過程遇到些問題,現在總結如下:希望給我遇到同樣問題的朋友一點幫助,如有錯誤的地方,希望得到大家指正共同學習。

1.有關型別轉換求和

select sum(cast('123' as float))----123

select sum(cast('' as float))----0

select sum(cast(null as float))----null

2.有關臨時表的問題

大夥都知道臨時表示用乙個#,全域性錶用兩個##

select * into #tempx    from dbo.collection 

declare @str nvarchar(max)

set @str='select convert(char(10),wb.createdtm ,120) as data,count(1) as numcount into ##tempx    from dbo.collection ct  left join waybillinfor2 wb     on ct.waybillid=wb.waybillid  left join waybillassign wa on wa.waybillid=wb.waybillid where  1=1  ' + @sql + '    group by convert(char(10),wb.createdtm ,120) '

exec (@str)

當我們需要借助臨時表來暫時儲存資料是,但是如上的第二段查詢語句中有變數的時候,就不行了,此事需要借助exec,而是此時也需要改為改為全域性表。

3.sqlserver : exec和sp_executesql的區別

exec除了不支援動態批處理中的輸入引數外,他也不支援輸出引數。預設情況下,exec把查詢的輸出返回給呼叫者。當我們需要輸出一定值的時候這是後就不行了,此是我們需要使用sp_executesql:比如下面的語句

declare @sqlwhere char(10), @sql varchar(1000), @feight int

set @sql = n' and wb.destinations=''11217461 '' and (ct.createdtm between ''2011-05-23 00:00:00'' and ''2011-05-30 23:59:59'')'

set @sqlwhere ='2011-05-09'

set @sql = 'select @a = sum(cast(wb.freight as float)) from waybillinfor2 wb inner join collection ct on wb.waybillid= ct.waybillid where convert(char(10),wb.createdtm,120)='''+@sqlwhere+''' and wb.paymode=''20101218190'''+@sql

exec sp_executesql @sql,n'@a int output',@feight output 

(此問題的解決多虧了jxqn_liu、maco_wang兩位牛人的幫助,問題緣由可以查閱

建議在執行字串時,使用sp_executesql儲存過程而不要使用 execute 語句。由於此儲存過程支援引數替換,因此sp_executesql比 execute 的功能更多;由於 sql server 更可能重用sp_executesql生成的執行計畫,因此sp_executesql比 execute 更有效。

有關使用sp_executesql請參閱:

exec和sp_executesql的區別請參閱:

有關 儲存 的知識點

1.cookie 在瀏覽器 與伺服器 之間 來回 傳遞 settionstorage 和localstoreage 不會 把資料 發給 伺服器 僅在本地儲存。2.資料有效期 不同 cookie 只在 裝置 cookie 過期 時間 之前 一直 有效 即使 視窗或 瀏覽器關閉。sessionstora...

儲存過程知識點!

2011年9月 14日 星期二 一 建立儲存過程 1 基本應用 語法格式 create database create table 儲存函式 每條語句的結束 宣告新的結束符 任選,避免在 mysql 環境下有特殊含義 delimiter 分隔符 delimiter delimiter create ...

幾個知識點

1 記憶體對齊 2 mvc與ssh框架對應關係 3 面試相關 4 非c 內建型別a和b,在哪幾種情況下b能隱式轉化為a?case 4 賦值操作,雖不是正宗的隱式型別轉換,但也可以勉強算乙個 5 extern c 的慣用法 1 在c 中引用c語言中的函式和變數,在包含c語言標頭檔案 假設為cexamp...