Sql型別轉換、select、set賦值區別
阿新 • • 發佈:2019-01-28
declare @Name varchar(12),@Id int
select @Name = 'smf',@Id = 1
set @Name = 'sun' set @Id = 2
print @Name
print @Id
----------------------------
declare @Name varchar(20)
set @Name = 'smf'
set @Name = (select NAME from T_Test where ID = -1)
select @Name --null set在返回值不存在時將賦值為null
----------------------------
declare @Name varchar(20)
set @Name = 'smf'
select @Name = Name from T_Test where ID = -1
select @Name --這裡結果為'smf',select在返回值不存在時將保持原值不變
----------------------------
全域性變數以@@開頭
select @@IDENTITY --為null
insert into T_Test values('ssss')
select @@IDENTITY --1183596
----------------------------
型別轉換
declare @version int
set @version = 2008
select 'SqlServer' + convert(varchar(10),@version )
select 'SqlServer' + CAST(@version as varchar(10))
declare @dateTime DateTime
select @dateTime = GETDATE()
select 'SqlServer' + CONVERT(varchar(40),@dateTime,102) --convert在轉換日期時將更靈活
select @Name = 'smf',@Id = 1
set @Name = 'sun' set @Id = 2
print @Name
print @Id
----------------------------
declare @Name varchar(20)
set @Name = 'smf'
set @Name = (select NAME from T_Test where ID = -1)
select @Name --null set在返回值不存在時將賦值為null
----------------------------
declare @Name varchar(20)
set @Name = 'smf'
select @Name = Name from T_Test where ID = -1
select @Name --這裡結果為'smf',select在返回值不存在時將保持原值不變
----------------------------
全域性變數以@@開頭
select @@IDENTITY --為null
insert into T_Test values('ssss')
select @@IDENTITY --1183596
----------------------------
型別轉換
declare @version int
set @version = 2008
select 'SqlServer' + convert(varchar(10),@version )
select 'SqlServer' + CAST(@version as varchar(10))
declare @dateTime DateTime
select @dateTime = GETDATE()
select 'SqlServer' + CONVERT(varchar(40),@dateTime,102) --convert在轉換日期時將更靈活