变量

定义变量:declare  @hello  varchar(20)

赋值:set  @hello = ‘你好’

select(结果框显示)/print(消息框显示) @hello

 

*三行必须同时运行

declare @hello varchar(20)

set @hello = '销售部'

select *from bumen where name = @hello

 

当放到select 和from中间时,可以当做赋值语句,不执行查询功能(赋值为查询到的最后一行数据)

declare @hello varchar(20)

set @hello = '销售部'

select @hello = name from bumen

select @hello

 

 

全局变量,系统变量

print @@connections--只能取值,不能赋值(返回SqlServer自上次启动以来的连接数,不管成功还是失败)

print @@error--返回执行上一个sql语句时的错误,返回0代表没错

print @@language--返回当前使用的语言

print @@rowcount--返回受上次操作影响的行数

print @@version--获取数据库的版本信息

 

 

ex.

update bumen set phone='123'where name = '销售部'

select '此查询语句一共影响了'+cast (@@ROWCOUNT as varchar(10)) +'行'

 

declare @text varchar(20)set @text = 'ab''js'(单引号当转义字符用)

print @text