T-SQL,定义变量以及select,print if的使用

if exists(select * from sys.databases where name ='stuDB1')
	drop database stuDB1
go
create database stuDB1
go
use stuDB1
go
create table stuInfo
(
	stuNo int identity,
	stuName nvarchar(16),
	sttuAge int
)
go
insert into stuInfo values('夏冬',55)
insert into stuInfo values('马超',464)
insert into stuInfo values('张辽',554)
insert into stuInfo values('马岱',54)
insert into stuInfo values('徐晃',88)
--定义变量,局部变量以@开头,系统变量以@@开头,不区分大小写,默认赋值null
declare @stuAge int
declare @stuName nvarchar(16),@stuSex int
--变量赋值
--必须和declare 一起执行
set @stuAge=18 
select @stuName='张的'
--可以查询时赋值
declare @stuName1 nvarchar(16)
select @stuName1=stuName from stuInfo where stuNo=1
select @stuName1
--了解各种系统变量
--输出语句
--区别在与,print 输出都只是文本字符串,而select s是以表格形式
declare @stuName2 nvarchar(8)
select @stuName2=stuName from stuInfo where stuNo=2
select @stuName2 as '你的名字', convert(nvarchar,getdate()) 打印时间
print '我是谁?你是'+@stuName2+'!    打印时间:'+convert(nvarchar,getdate())

--if 语句,只包含一句程序语句可以省略 begin end(代表大括号)
--if其他用法与C++等语言相似
declare @stuAge1 int
select  @stuAge1=sttuAge from stuInfo where stuNo=2
if(@stuAge1>88)
	print '姜还是老的辣'
else
	print '你好嫩了点'
if(@stuAge1>88)
	begin
		print '你老了'
		print '但是很辣'
	end
else
	begin
		print '你非常有活力'
		print '但是嫩了点'
	end

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值