declare @sUserType varchar(10) declare @s varchar(10) set @sUserType = '' set @s = ''select @s = Case @sUserType When 'gagant' then 'a' Else 'b' Endprint @s
Case @sUserType When 'gagant' then print 'a' Else print 'b' End 这不止二个条件啊,我要用Case, 要怎么做? 上面的代码出现 在关键字 'Case' 附近有语法错误。
declare @sUserType varchar(800) print( Case @sUserType When 'gagant' then 'a' Else 'b' End)
Create Procedure UserAdd @sUserType nvarchar(20), @sUserName nvarchar(30), @sPwd nvarchar(32), @bSex bit, @sRealName nvarchar(30), @sIdentity nvarchar(20), @sTel nvarchar(20), @sEmail nvarchar(50), @sBirthday char(10), @sProvince nvarchar(10), @sCity nvarchar(10), @sBankAddress nvarchar(50), @sBankType nvarchar(20), @sBankCode nvarchar(25), @sAddress nvarchar(50), @iGAgantID int, @iAgantID int, @iID int AS Case @sUserType When 'gagant' then print 'a' Else print 'b' End--下面是UserInfo表 Insert Into cp_userinfo(u_username_s,u_sex_b,u_personID_s,u_realName_s,u_tel_s,u_email_s,u_birthday_s,u_city_s,u_province_s,u_bank_s,u_bankType_n,u_bankID_s,u_address_s) Values(@sUserName,@bSex,@sIdentity,@sRealName,@sTel,@sEmail,@sBirthday,@sCity,@sProvince,@sBankAddress,@sBankType,@sBankCode,@sAddress) GO原文是这个的,为什么会CASE出现啊?
declare @sUserType as varchar(10) set @sUserType = 'gagant' select Case When @sUserType = 'gagant' then 'a' Else 'b' End as result result ------ a(所影响的行数为 1 行) ------------------------------------------------------------------------- declare @sUserType as varchar(10) set @sUserType = 'gagant1' select Case When @sUserType = 'gagant' then 'a' Else 'b' End as resultresult ------ b(所影响的行数为 1 行)
Case @sUserType When 'gagant' then print 'a' Else print 'b' End--改为: print( Case @sUserType When 'gagant' then 'a' Else 'b' End)---这个没什么用,好像是测试用的,看入参@sUserType是不是'gagant'
这些都不行啊,我要的是这样的 Case @sUserType when 'gagant' then insert into gagant... when 'agant' then insert into agant... else insert into user... End这样的语句要怎么写啊?
Declare @sql varchar(8000) Select @sql=Case @sUserType when 'gagant' then 'insert into gagant...' when 'agant' then 'insert into agant...' else 'insert into user...' endexec(@sql)
晕, insert into agant (col) select (case @sUserType when 'gagant' then ... case @sUserType when 'agant' then ... else ... end) from tb where ....
print 'a'
Else print 'b'
declare @sUserType varchar(10)
declare @s varchar(10)
set @sUserType = ''
set @s = ''select @s =
Case @sUserType
When 'gagant' then 'a'
Else 'b'
Endprint @s
When 'gagant' then print 'a'
Else print 'b'
End
这不止二个条件啊,我要用Case,
要怎么做?
上面的代码出现
在关键字 'Case' 附近有语法错误。
print( Case @sUserType
When 'gagant' then 'a'
Else 'b'
End)
@sUserType nvarchar(20),
@sUserName nvarchar(30),
@sPwd nvarchar(32),
@bSex bit,
@sRealName nvarchar(30),
@sIdentity nvarchar(20),
@sTel nvarchar(20),
@sEmail nvarchar(50),
@sBirthday char(10),
@sProvince nvarchar(10),
@sCity nvarchar(10),
@sBankAddress nvarchar(50),
@sBankType nvarchar(20),
@sBankCode nvarchar(25),
@sAddress nvarchar(50),
@iGAgantID int,
@iAgantID int,
@iID int
AS
Case @sUserType
When 'gagant' then print 'a'
Else print 'b'
End--下面是UserInfo表
Insert Into cp_userinfo(u_username_s,u_sex_b,u_personID_s,u_realName_s,u_tel_s,u_email_s,u_birthday_s,u_city_s,u_province_s,u_bank_s,u_bankType_n,u_bankID_s,u_address_s)
Values(@sUserName,@bSex,@sIdentity,@sRealName,@sTel,@sEmail,@sBirthday,@sCity,@sProvince,@sBankAddress,@sBankType,@sBankCode,@sAddress)
GO原文是这个的,为什么会CASE出现啊?
set @sUserType = 'gagant'
select Case When @sUserType = 'gagant' then 'a' Else 'b' End as result
result
------
a(所影响的行数为 1 行)
-------------------------------------------------------------------------
declare @sUserType as varchar(10)
set @sUserType = 'gagant1'
select Case When @sUserType = 'gagant' then 'a' Else 'b' End as resultresult
------
b(所影响的行数为 1 行)
When 'gagant' then print 'a'
Else print 'b'
End--改为:
print( Case @sUserType
When 'gagant' then 'a'
Else 'b'
End)---这个没什么用,好像是测试用的,看入参@sUserType是不是'gagant'
Case @sUserType
when 'gagant' then insert into gagant...
when 'agant' then insert into agant...
else insert into user...
End这样的语句要怎么写啊?
Select @sql=Case @sUserType
when 'gagant' then 'insert into gagant...'
when 'agant' then 'insert into agant...'
else 'insert into user...' endexec(@sql)
insert into agant (col)
select
(case @sUserType when 'gagant' then ...
case @sUserType when 'agant' then ...
else ...
end) from tb where ....