儲存過程
create procedure createdjh
@tablename nvarchar(40), --指定你的表名
@columname nvarchar(50),--指定乙個那一列是id
@idformate nvarchar(30),--單號的格式,如配件採購單為'cgd'
@latestid char(16) output --返回的值 ,也就是最後生成的id
asdeclare @tempid as nvarchar(16)
declare @sqlstr as nvarchar(254)
declare @datestr as char(8)
declare @tempdate as char(2)
--get the date str
set @tempdate=cast(datepart(dd,getdate() ) as char(2))
if len(@tempdate)=1
begin
set @tempdate='0'+@tempdate
endset @datestr=cast(datepart(yy, getdate()) as char(4) )
+substring( datename(mm, getdate()), 1, 3) +@tempdate
--get the last no
set @sqlstr=n'select top 1 @id = '+@columname+' from ' +@tablename+' where '+@columname+' like ltrim(rtrim(@likestr)) order by '+@columname+' desc'
declare @templikestr as char(14)
set @templikestr='%'+@idformate+ @datestr+'%'
execute sp_executesql @sqlstr,n' @likestr char(14) ,@id char(16) output', @templikestr,@tempid output --得到當前最後的乙個id號
--create the no
if @tempid is null
begin --第一次插入
set @tempid=@idformate+ @datestr+'0001'
endelse
begin
declare @templastforword as char(4)
declare @tempcount as int
declare @templenth as int
set @templastforword=right(@tempid,4)
set @tempcount=cast(@templastforword as int)
set @tempcount=@tempcount+1
set @templastforword=cast(@tempcount as char(4))
set @templenth=len(@templastforword)
if @templenth=1
begin
set @templastforword='000'+@templastforword
endelse if @templenth=2
begin
set @templastforword='00'+@templastforword
endelse if @templenth=3
begin
set @templastforword='0'+@templastforword
endset @tempid=@idformate+ @datestr+@templastforword
endselect @latestid=@tempid
return
gopublic static string getid(string tbname,string colname,string idname,sqlconnection conn)
儲存過程的呼叫
oracle儲存過程 以oracle自帶例子資料庫的表舉例 1 create or replace procedure p is cursor c is select from emp2 for update begin for v emp in c loop if v emp.sal 2000 t...
Oracle儲存過程呼叫儲存過程
oracle儲存過程呼叫有返回結果集的儲存過程一般用光標的方式,宣告乙個游標,把結果集放到游標裡面,然後迴圈游標 declare newcs sys refcursor cs1 number cs2 number cstype table rowtype table列的個數和newcs返回的個數一樣...
呼叫儲存過程
用乙個命令物件呼叫儲存過程,就是定義儲存過程的名稱,給過程的每個引數新增引數定義,然後執行命令。1.呼叫沒有返回值的儲存過程 呼叫儲存過程的最簡單示例是不給呼叫者返回任何值。下面定義了兩個這樣的儲存過程,乙個用於更新現有的region記錄,另乙個用於刪除指定的region記錄。1 記錄的更新 cre...