SQL纯手写创建数据库到表内内容

时间:2023-03-10 00:47:20
SQL纯手写创建数据库到表内内容

建表啥的只点点鼠标,太外行了,不如来看看我的纯手写,让表从无到有一系列;还有存储过程临时表,不间断的重排序;

一:建数据库

create Database Show

on

  primary
(
name= Show_data ,
filename= 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Show.mdf' ,
size=10MB,
maxsize=UNLIMITED,
filegrowth=% )
log on
(
name=Show_log,
filename='C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Show_log .ldf' ,
size=10MB,
maxsize=UNLIMITED,
filegrowth=%
)

二:建表

建表

三:建外键

建外键

四:添加表数据

插入表数据

五:删除数据库、表、外键、存储过程

 drop database Show;      --删除数据库

 drop table T_user;
drop table T_proMain;
drop table T_proType;
drop table T_proImg; --删除表 alter table T_proMain drop constraint fk_typeID
alter table T_proMain drop constraint fk_UID
alter table T_proImg drop constraint fk_proID --删除外键约束 drop proc proc_getPic --删除存储过程

删除数据库、表、外键、存储过程

六:建存储过程

  create proc proc_getproM  (
@Index int,
@Size int
)
as
begin
declare @ta table
(
[proID] [int]
)
insert into @ta(proID) select proID from (SELECT ROW_NUMBER() over ( order by CTR desc) as id ,* from T_proMain )a where id between @Index and @Size
declare @a int , @b varchar()
declare @tc table
(
[proID] int,
[proExp] varchar(),
[UName] varchar(),
[UrlName] varchar(max)
) while exists(select [proID] from @ta)
begin
-- select * from @ta;
select top @a=[proID] from @ta;
declare @c int
select @c =proID from T_proMain where proID=@a ; --proID declare @e varchar()
select @e=proExp from T_proMain where proID=@a ; --proExp 第一张的 项目名 declare @d varchar(),@l int;
select @l=UID from T_proMain where proID=@a ;
select @d=UName from T_user where UID=@l; --UName 也就是作者名
declare @tb table
(
[imgURL] varchar()
) insert into @tb (imgURL) select imgURL from T_proImg where proID=@a;
-- select * from @tb
declare @g varchar(max);
set @g='';
while exists(select [imgURL] from @tb)
begin
select top @b=[imgURL] from @tb;
declare @f varchar() select @f=imgURL from T_proImg where imgURL=@b ; --imgURL 第一张的图片地址
set @g+=@f;
set @g+='#';
print @f;
declare @h varchar()
select @h=imgName from T_proImg where imgURL=@b ; --imgName 第一张的图片地址
print @h;
declare @o bit
select @o=ISDefault from T_proImg where imgURL=@b ;
set @g+=@h;
set @g+='#';
declare @n bit,@p varchar()
set @n=;
if @o=@n
begin
set @p=''
end
else
begin
set @p=''
end
set @g+=@p;
set @g+='*';
print @g;
delete from @tb where [imgURL] = @b;
end insert into @tc values(@c,@e,@d,@g);
delete from @tb;
delete from @ta where [proID]=@a;
end
select * from @tc;
end

建存储过程:动态取出另一张ID连续等于第几条到第几条,在这张表的数据

 create proc proc_getPic  (
@Index int,
@Size int
)
as
begin
declare @ta table
(
[ID] [int]
)
insert into @ta(ID) select ID from (SELECT ROW_NUMBER() over ( order by ID desc) as id from T_Pic )aa where ID between @Index and @Size
declare @a int , @b varchar()
declare @tc table
(
[ID] int,
[Pic] varchar(),
[PicName] varchar(),
[PicAuthor] varchar()
) while exists(select [ID] from @ta)
begin
-- select * from @ta;
select top @a=[ID] from @ta;
declare @c int
select @c =ID from T_Pic where ID=@a ; --proID declare @e varchar()
select @e=Pic from T_Pic where ID=@a ; --proExp 第一张的 项目名 declare @d varchar(),@l varchar();
select @l=PicName from T_Pic where ID=@a ;
select @d=PicAuthor from T_Pic where ID=@a; --UName 也就是作者名 insert into @tc values(@c,@e,@l,@d); delete from @ta where [ID]=@a;
end
select * from @tc;
end
exec proc_getPic ,

动态取出表的连续的第几条到第几条数据

希望能帮到有需要的人;

      --一个快乐的码农!