sql-定义变量

1
2
3
declare @subject nvarchar(50)
 
set @subject=(select Subject from dbo.Scores where ID=1)

  

--select @subject=Subject from dbo.Scores where ID=1
--标量 标量就是单值 ,set后如果是一个结果集,则set的最终值为结果集中最后一个值
select @subject
 
设置自增
 
1
2
insert into dbo.Student values ('王五','男',40);select @@IDENTITY;
   insert into dbo.Student output inserted.ID values ('李六','男',41)

  

 
执行一些特定的脚本,需要考虑判断循环等行为,因此有if while等结构
if(){}------------>sqlserver中就是 if() begin end
 
1
2
3
4
5
6
7
8
9
10
  declare @name nvarchar(50)='王五'
if(@name='王五')
begin
  select 'true'
end
else
begin
select 'false'
end

  

 
循环
  --计算1-100的和
1
2
3
4
5
6
7
8
declare @int int=1;
declare @total int=0;
while(@int<=100)
begin
set @total=@total+@int;
set @int=@int +1;
end
select @total

  

 
 
 
 
posted @   馒头的梦想  阅读(461)  评论(0编辑  收藏  举报
编辑推荐:
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
阅读排行:
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率
· AI Agent爆火后,MCP协议为什么如此重要!
点击右上角即可分享
微信分享提示