declare @month int declare @year int declare @sql varchar (1000) set @month=9 set @year=2008 select case when @month in (1,2,4,6,8,9,11) then 'a' when @month in (5,7,10,12) then '1' when @month = 3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then '2' end /*
---- a(所影响的行数为 1 行) */ then后面不能这样写 变量 = 值.
Then 后面的类型要保持一致.
select case when @month in (1,2,4,6,8,9,11) then 'a' when @month in (5,7,10,12) then '1' when @month = 3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then '2' end
declare @month int declare @year int declare @sql varchar (1000) set @month=9 set @year=2008select case when @month in (1,2,4,6,8,9,11) then @sql='@chuqin TABLE([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL' when @month in (5,7,10,12) then @sql='a' when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then @sql='b' end执行的结果是: Server: Msg 170, Level 15, State 1, Line 8 Line 8: Incorrect syntax near '='.
你看看: declare @month int declare @year int declare @sql varchar (1000) set @month=9 set @year=2008 select @sql=(case when @month in (1,2,4,6,8,9,11) then '@chuqin TABLE([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL' when @month in (5,7,10,12) then 'a' when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then 'b' end ) print @sql
不大明白你的意思. declare @month int declare @year int declare @sql varchar (1000) set @month=9 set @year=2008 select @sql=(case when @month in (1,2,4,6,8,9,11) then 'TABLE chuqin ([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL' when @month in (5,7,10,12) then 'a' when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then 'b' end ) --print @sql set @sql='create '+@sql+')' --print @sql exec(@sql)你的建表语句有问题.把@chuqin改为chuqin.你现在看看行不行.
declare @year int
declare @sql varchar (1000)
set @month=9
set @year=2008 select case
when @month in (1,2,4,6,8,9,11) then 'a'
when @month in (5,7,10,12) then '1'
when @month = 3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then '2'
end /*
----
a(所影响的行数为 1 行)
*/
then后面不能这样写 变量 = 值.
when @month in (1,2,4,6,8,9,11) then 'a'
when @month in (5,7,10,12) then '1'
when @month = 3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then '2'
end
declare @year int
declare @sql varchar (1000)
set @month=9
set @year=2008select case
when @month in (1,2,4,6,8,9,11) then @sql='@chuqin TABLE([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL'
when @month in (5,7,10,12) then @sql='a'
when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then @sql='b'
end执行的结果是:
Server: Msg 170, Level 15, State 1, Line 8
Line 8: Incorrect syntax near '='.
declare @month int
declare @year int
declare @sql varchar (1000)
set @month=9
set @year=2008 select @sql=(case
when @month in (1,2,4,6,8,9,11) then '@chuqin TABLE([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL'
when @month in (5,7,10,12) then 'a'
when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then 'b'
end )
print @sql
Line 1: Incorrect syntax near '@chuqin'.
如何修改阿?
declare @month int
declare @year int
declare @sql varchar (1000)
set @month=9
set @year=2008
select @sql=(case
when @month in (1,2,4,6,8,9,11) then 'TABLE chuqin ([name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[no] [varchar] (10) COLLATE Chinese_PRC_CI_AS NULL'
when @month in (5,7,10,12) then 'a'
when @month=3 and ((@year % 400 = 0)or(@year % 4 = 0) and (@year % 100 <> 0)) then 'b'
end )
--print @sql
set @sql='create '+@sql+')'
--print @sql
exec(@sql)你的建表语句有问题.把@chuqin改为chuqin.你现在看看行不行.
case后面应该不能跟赋值语句!