從儲存過程例子的總結

2021-08-27 00:17:31 字數 3765 閱讀 2000

1.

create procedure sp_split

asdeclare @mg varchar(200)

declare @i int

set @mg='aa,bb,ccc'

set @i=charindex(',',@mg)

while @i>=1

begin

set @mg=substring(@mg,@i+1,len(@mg)-@i)

print(@mg)

set @i=charindex(',',@mg)

endgo

2.create procedure sp_test --查詢提取

asdeclare @i int

declare @strlen int

declare @str varchar(512)

declare @str1 varchar(512)

declare @str2 varchar(512)

declare @position smallint

set @str='癌、惡性、肉瘤、淋巴瘤,排除:未見癌、不見癌'

set @strlen=len(@str)

set @i=1

set @position=charindex(',',@str)

print @position

set @str1=substring(@str,1,@position-1)

set @str=right(@str,(len(@str)-@position))

set @position=charindex(';',@str)

set @str2=substring(@str,1,@position-1)

set @str=right(@str,(len(@str)-@position))

print @str1

print @str2

print @str

while @i<@strlen+1

begin

print(substring(@str,@i,1))

set @i=@i+1

endgo

3./*

declare @msg as varchar(512)

exec sp_expexcle 15,'c:\users\administrator\desktop\dkj\匯出資料\12.xls',@msg output

select @msg

drop procedure sp_expexcle

go*/

create procedure sp_expexcle --匯出excel檔案

@itemid int=0,

@path varchar(512)='',

@msg varchar(512)='' output

asdeclare @str as varchar(8000)

set @str=''

set @str = 'bcp "select ''科室編號'' as deskno,''性別'' as ***,''年齡'' as age ,''檢查部位'' as check_part ,''影像描述'' as image_del ,''影像診斷'' as image_diagnose ,''登記日期'' as register_date ,''報告日期'' as repoat_date ,''審核日期'' as chack_date union all select deskno,***,age,check_part,image_del,image_diagnose,register_date,repoat_date,chack_date from hospital.dbo.hos_pickup where item='+ str(@itemid)+'" queryout "' +@path+ '" -c -s127.0.0.1 -usa -p111111'

print @str

exec master..xp_cmdshell @str

--exec master..xp_cmdshell 'bcp "select ''編號'' as id,''名稱'' as name,''備註'' as remark union all select cast(id as varchar(8) ),name,remark from hospital.dbo.hos_item" queryout "c:\aa.xls " -c -s127.0.0.1 -usa -p111111 '

declare @ncount as int

set @ncount=0

select @ncount=count(*) from hospital.dbo.hos_pickup where item=@itemid

set @msg = '成功匯出'+cast(@ncount as varchar(30) )+'條資料。' --匯出後,顯示在匯出excel按鈕的旁邊

print @msg

go總結:

1)將int 轉換為 varchar :cast(@ncount as varchar(30) )

2)定義varchar: declare @str as varchar(8000)

int :declare @count as int

3)賦值:set @mg='aa,bb,ccc'

4)條件下:while @i>=1

begin

i++end

5)列印輸出:print @str1 .這個顯示在訊息裡面,如果select @str1顯示在結果裡面

6)擷取字串:set @mg=substring(@mg,@i+1,len(@mg)-@i)

7)以特定字元分割得到特定字串的位置 set @i=charindex(',',@mg)

8)得到字串長度:set @strlen=len(@str)

9)定義儲存過程引數:

create procedure sp_expexcle

@itemid int=0,

@path varchar(512)='',

@msg varchar(512)='' output

(兩個為輸入引數,乙個為輸出引數)

定義兩個輸入引數,沒有輸出引數則:

create procedure sp_expexcle

@itemid int=0,

@path varchar(512)=''

定義輸出引數為

create procedure sp_expexcle

@msg varchar(512)='' output

10)呼叫儲存過程

exec sp_expexcle 15,'c:\users\administrator\desktop\dkj\匯出資料\12.xls',''

exec sp_expexcle 15,'c:\users\administrator\desktop\dkj\匯出資料\12.xls'

declare @msg

exec sp_expexcle @msg output

select @msg

11)刪除儲存過程

drop procedure sp_expexcle

go12)執行sql

declare @sql as varchar(100)

set @sql='select * from table'

exec @sql

13)sql傳引數

set @ncount=0

select @ncount=count(*) from hospital.dbo.hos_pickup where item=@itemid

儲存過程的例子

帶引數儲存過程讀資料 public static void procinputreader sdr.close conn.close 帶 引數 輸出 儲存過程讀資料 output public static void procinputreader1 帶 引數 輸出 儲存過程讀資料 return 用...

儲存過程例子

alter proc futurema updageorderdatasdistinguishresult asdeclare connectionstring nvarchar 256 declare server nvarchar 256 declare uid nvarchar 256 dec...

儲存過程例子

create table testtb id varchar2 30 name varchar2 30 insert into testtb values 1 21 insert into testtb values 2 22 insert into testtb values 3 23 inser...