SQL Server??????????????????????????????SQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server??????????????????????????????SQL相关的知识,希望对你有一定的参考价值。

???????????????   ????????????   nvarchar   ??????   name   pre   ??????   tle   ??????   

????????????(stored procedure)????????????????????????????????????SQL????????????????????????????????????????????????????????????????????????????????????????????????SQL??????????????????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????????????????????????????????????????????????????????????? execute ????????????????????????

?????????

1??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????SQL?????????????????????????????????????????????????????????????????????

????????????????????????????????????????????????????????????????????????????????????????????????

2???????????????SQL??????????????????????????????????????????????????????????????????????????????SQL??????????????????????????????????????????????????????????????????????????????SQL??????????????????????????????

3??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????SQL????????????????????????????????????

????????????????????????????????????????????????????????????????????????

 

?????????

??????????????????
 1 CREATE PROC [ EDURE ] procedure_name [ ; number ]
 2     [ { @parameter data_type }
 3         [ VARYING ] [ = default ] [ OUTPUT ]
 4     ] [ ,...n ]
 5 [ WITH
 6     { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
 7 [ FOR REPLICATION ]
 8 AS 
 9 [ begin ]
10     T-SQL ??????
11 [ end ]
??????????????????

????????????????????????

??????????????????
 1 --???????????? GetStuCou ????????????????????????
 2 create procedure GetStuCou
 3 as
 4 begin
 5     select * 
 6     from Student s
 7     left join Course c on s.C_S_Id=c.C_Id
 8 end
 9 
10 --???????????? GetStuCou ????????????????????????
11 execute GetStuCou
??????????????????

??????????????????????????????

??????????????????
 1 --???????????? GetStuCou_Re ??????????????????????????????
 2 create procedure GetStuCou_Re
 3 as
 4 begin
 5     insert into Course(C_Name) values(???html5???)
 6     return SCOPE_IDENTITY();        -- ?????????????????????????????????????????????????????????
 7 end
 8 
 9 --???????????? GetStuCou ??????????????????????????????
10 execute GetStuCou_Re
??????????????????

?????????????????????????????????

??????????????????
 1 --???????????? GetStuCou_In ?????????????????????????????????
 2 create procedure GetStuCou_In
 3 @StuNo    nvarchar(64)=???001???        --???????????????
 4 as
 5 begin
 6     select * from Student where [email protected]
 7 end
 8 
 9 --???????????? GetStuCou_In ????????????????????????????????????????????????????????????????????????
10 execute GetStuCou_In
11 
12 --???????????? GetStuCou_In ???????????????????????????????????????????????????
13 execute GetStuCou_In ???005???
??????????????????

??????????????????????????????????????????

??????????????????
 1 --???????????? GetStuCou_Out ????????????????????????????????????????????????
 2 create procedure GetStuCou_Out
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32) output
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> ??????)
 8     begin
 9         select @Height=S_Height 
10         from Student 
11         where [email protected]
12     end
13     else
14     begin
15         set @Height=???185???
16     end
17 end
18 
19 --???????????? GetStuCou_Out ????????????????????????????????????????????????
20 execute GetStuCou_Out ???005???,null
??????????????????

??????????????????????????????????????????????????????

??????????????????
 1 --???????????? GetStuCou_DS ????????????????????????????????????????????????????????????
 2 create procedure GetStuCou_DS
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32) output
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> ??????)
 8     begin
 9         select @Height=S_Height 
10         from Student 
11         where [email protected]
12     end
13     else
14     begin
15         set @Height=???185???
16     end
17 
18     select s.S_Id,s.S_StuNo,s.S_Name,s.S_Sex,s.S_Height,s.S_BirthDate,c.C_Id,c.C_Name
19     from Student s
20     left join Course c on s.C_S_Id=c.C_Id
21     where [email protected]
22 end
23 
24 --???????????? GetStuCou_DS ????????????????????????????????????????????????????????????
25 execute GetStuCou_DS ???005???,null
??????????????????

???????????????????????????????????????

??????????????????
 1 --???????????? GetStuCou_DSS ???????????????????????????????????????
 2 create procedure GetStuCou_DSS
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32)
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> ??????)
 8     begin
 9         select *
10         from Student 
11         where [email protected]
12     end
13 
14     if(@Height is not null and @Height <> ??????)
15     begin
16         select * 
17         from Student
18         where [email protected]
19     end
20 end
21 
22 --???????????? GetStuCou_DSS ???????????????????????????????????????
23 execute GetStuCou_DSS ???005???,???185???
??????????????????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????? T-SQL ???????????????????????????

 

???????????????????????????????????????????????????????????????????????????

??????????????????
 1 --???????????? GetStuCou_Ext ???????????????????????????????????????
 2 create procedure GetStuCou_Ext
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32)
 5 as
 6 begin
 7     declare @Var nvarchar(10)    --????????????
 8 
 9     set @Var=???123???        --????????????
10 
11     --???????????????
12     declare @StuTab table 
13     (
14         ID     int not null primary key,
15         StuNo    nvarchar(50) unique,
16         Name varchar(50),
17         Sex varchar(10),
18         Height varchar(10)
19     )
20     --?????????????????????????????????????????????
21 
22     --???????????????
23     create table #Tab
24     (
25         ID     int not null primary key,
26         StuNo    nvarchar(50),
27         Name varchar(50),
28         Sex varchar(10),
29         Height varchar(10)
30     )
31 
32     alter table #Tab add constraint S_UNIQUE unique(StuNo)
33 
34     --????????????????????????????????????
35     
36     if(@StuNo is not null and @StuNo <> ??????)
37     begin
38         insert into @StuTab(ID,StuNo,Name,Sex,Height)    --????????????????????????
39         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
40         from Student 
41         where [email protected]
42 
43         insert into #Tab(ID,StuNo,Name,Sex,Height)    --????????????????????????
44         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
45         from Student 
46         where [email protected]
47     end
48 
49     if(@Height is not null and @Height <> ??????)
50     begin
51         insert into @StuTab(ID,StuNo,Name,Sex,Height)    --????????????????????????
52         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
53         from Student
54         where [email protected]
55 
56         insert into #Tab(ID,StuNo,Name,Sex,Height)    --????????????????????????
57         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
58         from Student
59         where [email protected]
60     end
61 
62     SELECT * FROM @StuTab
63     select * from #Tab
64 end
65 
66 --???????????? GetStuCou_DSS ???????????????????????????????????????
67 execute GetStuCou_Ext ???005???,???185???
??????????????????

???????????????????????? SQL ?????????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???005,006,007??? ????????????????????????????????????????????? sql ?????????

??????????????????
 1 create proc GetStus
 2 @StuNo nvarchar(500)
 3 as
 4 begin
 5     declare @Sql nvarchar(3000)
 6 
 7     if(@StuNo is not null and @StuNo <> ??????)
 8     begin
 9         set @Sql=??? select * from Student where S_StuNo in (???[email protected]+???) ???
10     end
11 
12     exec (@Sql)    --???????????? sql 
13 end
14 
15 exec GetStus ???003,005,009???        --?????????????????? GetStus
??????????????????

???????????????????????????????????????????????? sql ??????????????????????????????????????????????????????????????? sql ????????????????????? ID ,?????????????????? ID ???????????????

???????????????????????? sp_executesql ?????? sql ??????????????????

???????????????????????????

??????????????????
 1 ALTER proc [dbo].[GetStus]
 2 @StuNo nvarchar(500)
 3 as
 4 begin
 5     declare @Sql nvarchar(3000)
 6     declare @C_Id int
 7     declare @Cou int
 8 
 9     if(@StuNo is not null and @StuNo <> ??????)
10     begin
11         set @Sql=??? select @CId=C_S_Id,@cou=count(1) from Student where S_StuNo in (???[email protected]+???) group by C_S_Id ???
12     end
13 
14     exec sp_executesql @Sql,N???@CId int output,@cou int output???,@CId = @C_Id output,@cou = @Cou output;
15 
16     select @C_Id,@Cou    --????????????????????????
17 
18     select * from Course where [email protected]_Id 
19 end
20 
21 exec GetStus ???005???        --?????????????????? GetStus
??????????????????

??????????????????

PS???sp_executesql ???????????? Sql ????????? Sql ????????????????????? NVARCHAR ?????????

??????Sql??????????????????????????????????????????????????????????????????????????????N???@CId int output,@cou int output???,@CId = @C_Id output,@cou = @Cou output;???@CId ?????? @C_Id???@cou ?????? @Cou???

??????SQl????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????N???@CId int output,@cou int output???,@CId = @C_Id output,@cou = @Cou output;?????? @CId = @C_Id ??? @cou = @Cou ???

以上是关于SQL Server??????????????????????????????SQL的主要内容,如果未能解决你的问题,请参考以下文章