儲存過程建立案例

2022-04-05 05:32:33 字數 4228 閱讀 4690

set ansi_nulls on

set quoted_identifier on

goalter procedure [dbo].[prc_create_table](

@banid varchar(50),

@name varchar(50),

@keyname varchar(50),

@banzhu bigint,

@ftstatus int,

@htstatus int,

@status int,

@remark text)as

declare @tiezhitabl varchar(100)--帖子表

declare @huitietabl varchar(100)--回帖表

declare @xgtiezitabl varchar(100)--帖子修改記錄表

declare @jcbiaotabl varchar(100)--獎懲記錄表

declare @sql1 varchar(500)

declare @sql2 varchar(500)

declare @sql3 varchar(500)

declare @sql4 varchar(500)

--帖子表cast(b as int)

set @tiezhitabl='gb_bbs_tiezhi_'+@banid

--回帖表

set @huitietabl='gb_bbs_huitie_'+@banid

--帖子修改記錄表

set @xgtiezitabl='gb_bbs_xgtiezi_'+@banid

--獎懲記錄表

set @jcbiaotabl='gb_bbs_jcbiao_'+@banid

--判斷這張表是否存在

if exists(select name from sys.objects where name=''+@tiezhitabl+'')

begin

select 0 as msg--0表示表已存在,建立表失敗

endelse

begin

set @sql1='create table '+@tiezhitabl+'(id bigint primary key identity,

title varchar(200) not null,

content text not null,

status int,

ifjj int,

ifdz int,

fttime datetime,

userid bigint foreign key (userid) references gb_bbs_user(id)

)'if exists(select name from sys.objects where name=''+@huitietabl+'')

begin

select 0 as msg--0:表已存在,建立表失敗

endelse

begin

set @sql2='create table '+@huitietabl+'(id bigint primary key identity,

tieziid bigint foreign key (tieziid) references '+@tiezhitabl+'(id),

content text,

status int,

fttime datetime,

userid bigint foreign key (userid) references gb_bbs_user(id)

)'if exists(select name from sys.objects where name=''+@xgtiezitabl+'')

begin

select 0 as msg--0:表已存在,建立表失敗

endelse

begin

set @sql3='create table '+@xgtiezitabl+'(id bigint primary key identity,

tieziid bigint foreign key (tieziid) references '+@tiezhitabl+'(id),

userid bigint foreign key (userid) references gb_bbs_user(id) ,

updatetime datetime,

content text

)'if exists(select name from sys.objects where name=''+@jcbiaotabl+'')

begin

select 0 as msg --0:表示表已存在,建立表失敗

endelse

begin

set @sql4='create table '+@jcbiaotabl+'(id bigint primary key identity,

tieziid bigint foreign key (tieziid) references '+@tiezhitabl+'(id),

time datetime,

content text

)'begin tran --事務開始

--新增板塊

insert into dbo.gb_bbs_bankuai ( name, keyname, banzhu, ftstatus, htstatus, status, remark) values (@name,@keyname,@banzhu,@ftstatus,@htstatus,@status,@remark)

exec(@sql1)

exec(@sql2)

exec(@sql3)

exec(@sql4)

if(@@error<>0)

begin

rollback tran

select 1 as msg--建立表遇到未知錯誤

end-- else if(@@trancount>0)

-- begin

-- rollback tran

-- end

else

begin

commit tran

select 2 as msg

end

endend

endend

execute prc_create_table '21','abc','abc',2,0,0,0,'abc'

呼叫protected void page_load(object sender, eventargs e)

;//設定引數

parameters[0].value = "123456";

parameters[1].value = "123456fda";

parameters[2].value = "123abc";

parameters[3].value = 2;

parameters[4].value = 1;

parameters[5].value = 1;

parameters[6].value = 1;

parameters[7].value = "此處是備註";

//新增引數

cmd.parameters.add(parameters[0]);

cmd.parameters.add(parameters[1]);

cmd.parameters.add(parameters[2]);

cmd.parameters.add(parameters[3]);

cmd.parameters.add(parameters[4]);

cmd.parameters.add(parameters[5]);

cmd.parameters.add(parameters[6]);

cmd.parameters.add(parameters[7]);

conn.open();

label1.text = cmd.executenonquery().tostring();

dr = cmd.executereader();

datatable dt = new datatable();

dt.load(dr);

this.gridview1.datasource = dt;

this.gridview1.databind();

}catch (exception ex)

finally

Mysql儲存過程案例

刪除drop procedure if exists p house set premit 建立獲取房間最低 和統計房間數儲存過程 delimiter create definer dev stand admin procedure p house set premit in in house in...

案例講解 儲存過程

一 建立儲存過程實現傳入使用者名稱和密碼,插入到admin表中 create procedure test pro1 in username varchar 20 in loginpwd varchar 20 begin insert into admin admin.username,passwo...

建立儲存過程

execute pro book 當你執行該儲存過程時,所有包括在其中的sql語句都會執行,在上面的例子中,會返回所有在forum表中的記錄。當在批處理中的第乙個語句是呼叫儲存過程時,你並不需要使用execute語句。你可以簡單地提供儲存過程的名稱來執行儲存過程。比如在isql w中,可以象下面所示...