示例
1、建立一個表,然後通過存儲過程往該表中插入100條記錄
create table teachers(id int primary key not null identity(10000,100),tea_name varchar(50),tea_age int default 20)
create procedure insert1000teachers
as
declare @tea_name varchar(50)
declare @tea_age int
declare @index int
declare @count int
set @index=1
set @count=100
while @index<@count
begin
set @tea_age=30*rand()
set @tea_name=''teacher''
set @tea_name=@tea_name+convert(varchar,@index)
insert into teachers(tea_name,tea_age)
values(@tea_name,@tea_age)
set @index=@index+1
end
2、創建數據庫
create database test1
on
primary
(
name=t1_dat,
filename=''d:\test_database\1\t1_dat.mdf'',
size=1,
filegrowth=15%,
maxsize=50
),
(
name=t2_dat,
filename=''d:\test_database\1\t2_dat.ndf'',
size=1,
maxsize=UNLIMITED,
filegrowth=1
)
log on
(
name=t_log,
filename=''d:\test_database\1\t_log.ldf'',
size=1,
maxsize=UNLIMITED,
filegrowth=1
)
3、增加一個數據文件到指定的數據庫
alter database test1
add
file
(
name=t3_dat,
filename=''d:\test_database\1\t3_dat.ndf'',
size=1,
maxsize=UNLIMITED,
filegrowth=1
)
4、增加組
alter database test1
add
filegroup filegroup1
5、增加一個數據文件到指定的組
alter database test1
add file
(
name=t4_dat,
filename=''d:\test_database\1\t4_dat.ndf''
)
to filegroup filegroup1
6、對象重命名
更改字段名:sp_rename ''teachers.tea_name'',''tea_name1'',''column''
更改表名:sp_rename ''dbo.teachers'',''teachers1''
7、查看存儲過程的內容:
sp_helptext insert1000teachers
8、采用存儲過程實現:根據存儲過程中的內容,查找存儲過程的名字
create procedure getProcedureByBodyWords
@patpro varchar(100),--存儲過程中的任意內容
@proname varchar(50) --存儲過程的部分或者全名,或者為null
as
begin
select name from sysobjects where