第十二单元 `T-SQL` 编程 第十二单元 SQL` SQL 编程 T-
create database step2_unit12; go use step2_unit12; go -- 部门表 CREATE TABLE [dbo].[Department]( [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NULL ); -- 职位表 CREATE TABLE [dbo].[Job]( [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NULL ); -- 人员表 CREATE TABLE [dbo].[person]( [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [Sex] [varchar](10) NULL, [Age] [int] NULL ); -- 工资表 CREATE TABLE [dbo].[Salary]( [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, [Sal] [money] NULL, [Comm] [money] NULL, [StaffId] [int] NULL ); -- 员工表 CREATE TABLE [dbo].[Staff]( [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL, [Name] [varchar](20) NOT NULL, [Hiredate] [datetime] NOT NULL, [DepartmentId] [int] NULL, [JobId] [int] NULL ); SET IDENTITY_INSERT [dbo].[Department] ON INSERT [dbo].[Department] ([Id], [Name]) VALUES (1, N'技术部') INSERT [dbo].[Department] ([Id], [Name]) VALUES (2, N'人资部') INSERT [dbo].[Department] ([Id], [Name]) VALUES (3, N'市场部') SET IDENTITY_INSERT [dbo].[Department] OFF SET IDENTITY_INSERT [dbo].[Job] ON INSERT [dbo].[Job] ([Id], [Name]) VALUES (1, N'经理') INSERT [dbo].[Job] ([Id], [Name]) VALUES (2, N'组长') INSERT [dbo].[Job] ([Id], [Name]) VALUES (3, N'员工') SET IDENTITY_INSERT [dbo].[Job] OFF SET IDENTITY_INSERT [dbo].[person] ON INSERT [dbo].[person] ([Id], [Name], [Sex], [Age]) VALUES (1, N'高圆圆', N'女', 28) INSERT [dbo].[person] ([Id], [Name], [Sex], [Age]) VALUES (2, N'王佳佳', N'女', 32) INSERT [dbo].[person] ([Id], [Name], [Sex], [Age]) VALUES (3, N'杨硕', N'男', 25) SET IDENTITY_INSERT [dbo].[person] OFF SET IDENTITY_INSERT [dbo].[Salary] ON INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (1, 15000.0000, 10000.0000, 1) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (2, 15000.0000, 9000.0000, 2) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (3, 12000.0000, 8000.0000, 3) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (4, 8000.0000, 6000.0000, 4) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (5, 4000.0000, 3000.0000, 5) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (6, 9000.0000, 8000.0000, 6) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (7, 15000.0000, 12000.0000, 7) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (8, 12000.0000, 9000.0000, 7) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (9, 7000.0000, 2000.0000, 9) INSERT [dbo].[Salary] ([Id], [Sal], [Comm], [StaffId]) VALUES (10, 9000.0000, 8000.0000, 10) SET IDENTITY_INSERT [dbo].[Salary] OFF SET IDENTITY_INSERT [dbo].[Staff] ON INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (1, N'王鑫', CAST(N'2014-02-01 00:00:00.000' AS DateTime), 1, 1) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (2, N'张磊', CAST(N'2015-09-03 00:00:00.000' AS DateTime), 2, 1) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (3, N'李鹏', CAST(N'2016-08-01 00:00:00.000' AS DateTime), 3, 2) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (4, N'王洋', CAST(N'2019-02-03 00:00:00.000' AS DateTime), 1, 3) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (5, N'吴潘', CAST(N'2018-10-01 00:00:00.000' AS DateTime), 2, 3) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (6, N'贾丹', CAST(N'2018-07-01 00:00:00.000' AS DateTime), 2, 2) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (7, N'李露', CAST(N'2016-09-01 00:00:00.000' AS DateTime), 3, 1) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (8, N'杨腾', CAST(N'2018-11-08 00:00:00.000' AS DateTime), 1, 2) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (9, N'孙艺', CAST(N'2018-12-26 00:00:00.000' AS DateTime), 3, 3) INSERT [dbo].[Staff] ([Id], [Name], [Hiredate], [DepartmentId], [JobId]) VALUES (10, N'赵帅', CAST(N'2019-01-03 00:00:00.000' AS DateTime), 1, 3) SET IDENTITY_INSERT [dbo].[Staff] OFF
1. T-SQL 命名规则
1,开头不能是数字,空格或特殊字符
2,首字符后可以包含 字母 数字 汉字 _ @ #
3,不能是系统保留字(关键字)
2. 变量
全局变量
整个系统都可以使用
@@ERROR 返回执行的上一个T-SQL语句的错误号,0 代表没有错误,非0 代表有错误 begin tran declare @a int = 1; select @a/0 select @@ERROR commit @@IDENTITY 返回插入到表的IDENTITY列的最后一个值 @@LANGUAGE返回当前所用语言的名称 @@MAX_CONNECTIONS返回Sql Server实例允许同时进行的最大用户连接数 @@ROWCOUNT返回上一次语句影响的数据行的行数 @@SERVERNAME返回运行Sql Server的本地服务器的名称 @@SERVICENAME返回Sql Server正在其下运行的注册表项的名称 @@TIMETICKS返回每个时钟周期的微秒数 @@TRANCOUNT返回当前连接的活动事务数 @@VERSION 返回当前安装的日期、版本和处理器类型
局部变量
注意:定义局部变量必须以@开头。只能在当前连接(会话)下使用
定义变量
--declare @变量名 变量类型= 变量值; --如 declare @name varchar(20)= '任我行';
局部变量赋值
-
select 赋值 declare @age int= 0; -- 定义变量 -- 直接赋值 select @age = 20; -- 也可通过查询语句进行赋值 select @age=Age from person where Name='高圆圆'; set 赋值 set @age = 20; -- 不可通过查询语句进行赋值 declare @username nvarchar(50)='任我行'; --定义局部变量,并赋初始值 set @username='张三'; --通过set赋值 select @username='aaa'; --通过select 赋值 select @username as 用户名; --通过select 输出并取别名 print @username; --print 打印输出
案例
-- 查询高圆的年龄,如果年龄是1-10岁,则输出儿童,10-25 输出少年,25-40输出青年,40-60输出中年,其他老年 declare @age int=0; select @age=Age from person where Name='高圆圆'; set @age=30; --给局部变量赋值 if @age<10 begin print '儿童'; end; else if @age<25 begin print '少年' end; else if @age<40 begin print '青年' end; else if @age<60 begin print '中年' end; else begin print '老年' end;
3. 运算符
1. 算术运算符
运算符 | 名称 | ||||
---|---|---|---|---|---|
运算符 | 名称 | ||
---|---|---|---|
运算符 | 含义 |
---|---|
级别 | 运算符 |
---|---|