sql2005的t-sql的一些基本的语法
2012-08-05 16:35
429 查看
use
--创建一个新用户,第一个参数是用户名,第二个参数是密码
exec sp_addlogin 'caohuan','wenbin';
exec sp_grantdbaccess
use caowen;
exec sp_revokedbaccess 'caohuan';
--将当前数据库的访问权限赋予给'caohuan'这个新用户,并且取一个别名,这个别名必须要存在,否则下面的赋予权限就会存在问题
exec sp_grantdbaccess 'caohuan', 'ccc';
--这是赋予dep表的select权限给ccc这个别名的用户
grant select on dep to ccc;
grant insert, delete on dep to ccc;
grant all on dep to ccc;
--这是移除ccc用户访问dep表的select权限
revoke select on dep from ccc;
select * from caowen.sa.dep;
select * from sys.databases;
use caowen;
select * from sysobjects where name ='dep';
--如果表存在,就删除表
if exists(select * from sysobjects where name = 'dep')
drop table dep;
exec sp_password wenbin,caohuan, caohuan;
---t-sql的讲解
/*变量分为:
局部变量:
局部变量必须以标记@作为前缀 ,如@age
局部变量的使用也是先声明,再赋值
全局变量:
全局变量必须以标记@ @作为前缀,如@@version
全局变量由系统定义和维护,我们只能读取,不能修改全局变量的值 */
use caowen;
select * from dep;
insert into dep values('男生部');
--定义局部变量
declare @id int;
declare @name varchar(10);
declare @@x varchar(10);--全局变量
declare @y int;
--这是赋值
set @id = 12;
select @name = depname from dep where depid = 1;--这里耍选出来的记录必须只有一条
set @@x = 'caohuan';
select @@x='caojuan', @y=123;--set,多个select都可以赋值,但是select可以赋值多个,但是set只可以一个
--这是输出值
print @y;
print @@x;
print @id;--print是文本输出
select @name;--select是表格输出
print @@x;
--系统的一些全局变量
select @@error;--
select @@identity;--最后一次插入的标识值
print @@language;--当前使用的语言的名称
print @@max_connections;--可以创建的同时连接的最大数目
select @@rowcount;--受上一个SQL语句影响的行数
select @@servername;--本地服务器的名称
select @@VERSION;--SQL Server的版本信息
--while的用法
--第一种写法
declare @sum int, @i int;
select @sum = 0, @i = 1;
while(@i<=100)
begin
set @sum = @sum + @i;
set @i = @i + 1;
end;
print @sum;
--第二种写法
declare @sum int, @i int;
select @sum = 0,@i = 1;
while(1=1)
begin
set @sum = @sum + @i;
set @i = @i + 1;
if(@i > 100)
begin
break;
end;
end;
print @sum;
--这是if-else语句
declare @x int, @y int;
select @x = 1, @y = 3;
if(@x = @y)--记住判断是否等于的时候是用'='而不是用'=='
begin
print('x == y');
end
else
begin
print('x != y');
end
--多条件的case-end
declare @y int, @x varchar(10);
select @y = 24;
select @x =
case @y/10--相当于case后面的语句会返回一个值,需要一个参数接受,这里的@x就是接收的
when 1 then '是1'
when 2 then '是2'
when 3 then '是3'
when 4 then '是4'
else '都不是'
end;
print(@x);
--第二种写法
declare @x int;
set @x = 24;
select case
when @x > 30 then '优秀'
when @x > 20 then '良好'
when @x > 10 then '一般'
else '不好'
end as '成绩';
--创建一个新用户,第一个参数是用户名,第二个参数是密码
exec sp_addlogin 'caohuan','wenbin';
exec sp_grantdbaccess
use caowen;
exec sp_revokedbaccess 'caohuan';
--将当前数据库的访问权限赋予给'caohuan'这个新用户,并且取一个别名,这个别名必须要存在,否则下面的赋予权限就会存在问题
exec sp_grantdbaccess 'caohuan', 'ccc';
--这是赋予dep表的select权限给ccc这个别名的用户
grant select on dep to ccc;
grant insert, delete on dep to ccc;
grant all on dep to ccc;
--这是移除ccc用户访问dep表的select权限
revoke select on dep from ccc;
select * from caowen.sa.dep;
select * from sys.databases;
use caowen;
select * from sysobjects where name ='dep';
--如果表存在,就删除表
if exists(select * from sysobjects where name = 'dep')
drop table dep;
exec sp_password wenbin,caohuan, caohuan;
---t-sql的讲解
/*变量分为:
局部变量:
局部变量必须以标记@作为前缀 ,如@age
局部变量的使用也是先声明,再赋值
全局变量:
全局变量必须以标记@ @作为前缀,如@@version
全局变量由系统定义和维护,我们只能读取,不能修改全局变量的值 */
use caowen;
select * from dep;
insert into dep values('男生部');
--定义局部变量
declare @id int;
declare @name varchar(10);
declare @@x varchar(10);--全局变量
declare @y int;
--这是赋值
set @id = 12;
select @name = depname from dep where depid = 1;--这里耍选出来的记录必须只有一条
set @@x = 'caohuan';
select @@x='caojuan', @y=123;--set,多个select都可以赋值,但是select可以赋值多个,但是set只可以一个
--这是输出值
print @y;
print @@x;
print @id;--print是文本输出
select @name;--select是表格输出
print @@x;
--系统的一些全局变量
select @@error;--
select @@identity;--最后一次插入的标识值
print @@language;--当前使用的语言的名称
print @@max_connections;--可以创建的同时连接的最大数目
select @@rowcount;--受上一个SQL语句影响的行数
select @@servername;--本地服务器的名称
select @@VERSION;--SQL Server的版本信息
--while的用法
--第一种写法
declare @sum int, @i int;
select @sum = 0, @i = 1;
while(@i<=100)
begin
set @sum = @sum + @i;
set @i = @i + 1;
end;
print @sum;
--第二种写法
declare @sum int, @i int;
select @sum = 0,@i = 1;
while(1=1)
begin
set @sum = @sum + @i;
set @i = @i + 1;
if(@i > 100)
begin
break;
end;
end;
print @sum;
--这是if-else语句
declare @x int, @y int;
select @x = 1, @y = 3;
if(@x = @y)--记住判断是否等于的时候是用'='而不是用'=='
begin
print('x == y');
end
else
begin
print('x != y');
end
--多条件的case-end
declare @y int, @x varchar(10);
select @y = 24;
select @x =
case @y/10--相当于case后面的语句会返回一个值,需要一个参数接受,这里的@x就是接收的
when 1 then '是1'
when 2 then '是2'
when 3 then '是3'
when 4 then '是4'
else '都不是'
end;
print(@x);
--第二种写法
declare @x int;
set @x = 24;
select case
when @x > 30 then '优秀'
when @x > 20 then '良好'
when @x > 10 then '一般'
else '不好'
end as '成绩';
相关文章推荐
- 【数据库】 sql的一些基本语法(一)
- 【数据库】 sql的一些基本语法(三)
- SQL语句的基本常用的一些语法
- 【数据库】 sql的一些基本语法(二)
- 【数据库】 sql的一些基本语法(四)
- MySQL 和 Oracle SQL的一些基本语法和函数
- SQL语法的一些基本应用
- sql一些基本的语法
- SQL的一些基本操作语法
- PL/SQL程序设计_基本语法
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
- T-SQL的基本语法
- 动态sql语句基本语法
- 动态sql语句基本语法
- java中数组中的一些基本语法
- .Net_02_增删改查的基本语法 (Sql 语句)
- 基本SQL语法
- 动态sql语句基本语法
- 数据库之SQL基本语法
- SQL 基本语法参考(初学者留意)