declare @countrq int
set @countrq = 1
--select @countrq
if @countrq = 3 print '此值为3'
if @countrq = 0 print '此值为0'
if @countrq = 1 print '此值为1'
if @countrq = 2 print '此值为2'delete from ky_yhnycx
--select @countrqif @countrq = 0
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 1
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 2
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 3
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
end上面可以出现‘此值为1’,但是后面的insert into 语句却提示错误,也就是说后面的if语句没有起到作用。请问这是为什么呢?
set @countrq = 1
--select @countrq
if @countrq = 3 print '此值为3'
if @countrq = 0 print '此值为0'
if @countrq = 1 print '此值为1'
if @countrq = 2 print '此值为2'delete from ky_yhnycx
--select @countrqif @countrq = 0
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 1
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 2
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 3
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
end上面可以出现‘此值为1’,但是后面的insert into 语句却提示错误,也就是说后面的if语句没有起到作用。请问这是为什么呢?
列名 'd29' 无效。
也就是说后面的insert 语句都执行了,这是为什么呢?
drop table [dbo].[Ky_Yhnycx]
GOCREATE TABLE [dbo].[Ky_Yhnycx] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[rqny] [varchar] (6) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[xl] [varchar] (1) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[yyhm] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[jsy] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[d26] [numeric](6, 2) NOT NULL ,
[d27] [numeric](6, 2) NOT NULL ,
[d28] [numeric](6, 2) NOT NULL ,
[d29] [numeric](6, 2) NOT NULL ,
[d30] [numeric](6, 2) NOT NULL ,
[d31] [numeric](6, 2) NOT NULL ,
[d1] [numeric](6, 2) NOT NULL ,
[d2] [numeric](6, 2) NOT NULL ,
[d3] [numeric](6, 2) NOT NULL ,
[d4] [numeric](6, 2) NOT NULL ,
[d5] [numeric](6, 2) NOT NULL ,
[d6] [numeric](6, 2) NOT NULL ,
[d7] [numeric](6, 2) NOT NULL ,
[d8] [numeric](6, 2) NOT NULL ,
[d9] [numeric](6, 2) NOT NULL ,
[d10] [numeric](6, 2) NOT NULL ,
[d11] [numeric](6, 2) NOT NULL ,
[d12] [numeric](6, 2) NOT NULL ,
[d13] [numeric](6, 2) NOT NULL ,
[d14] [numeric](6, 2) NOT NULL ,
[d15] [numeric](6, 2) NOT NULL ,
[d16] [numeric](6, 2) NOT NULL ,
[d17] [numeric](6, 2) NOT NULL ,
[d18] [numeric](6, 2) NOT NULL ,
[d19] [numeric](6, 2) NOT NULL ,
[d20] [numeric](6, 2) NOT NULL ,
[d21] [numeric](6, 2) NOT NULL ,
[d22] [numeric](6, 2) NOT NULL ,
[d23] [numeric](6, 2) NOT NULL ,
[d24] [numeric](6, 2) NOT NULL ,
[d25] [numeric](6, 2) NOT NULL
) ON [PRIMARY]
GOALTER TABLE [dbo].[Ky_Yhnycx] WITH NOCHECK ADD
CONSTRAINT [PK_Ky_Yhnycx] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
GO
贴出来看看
if @countrq = 0
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 2
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 3
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
end
拿掉可以?
declare @countrq int
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
--select @countrq
if @countrq = 3 print '此值为3'
if @countrq = 0 print '此值为0'
if @countrq = 1 print '此值为1'
if @countrq = 2 print '此值为2'delete from ky_yhnycx
--select @countrqif @countrq = 0
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 1
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 2
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 3
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
end
看你的代码,你好像只进行一次计算啊,为什么会是变化的,还有没有上下文?##temp也是动态生成的吗?
declare @xl varchar(1)
declare @rq_s varchar(10)
declare @rq_e varchar(10)
declare @countrq intset @xl='4'
set @rq_s='2006/02/26'
set @rq_e='2006/03/25'set @sql = 'select xl,jsy1,yyhm'
select @sql = @sql + ' , sum(case convert(varchar(10),rq,120) when '''+convert(varchar(10),rq,120)+''' then yhck else 0 end) [d'+convert(varchar(2),day(rq))+']'
from (select distinct rq from yy_ryye where rq>=@rq_s and rq<=@rq_e) as a order by rq
set @sql = @sql + 'into ##temp from yy_ryye where rq>='''+@rq_s+'''and rq<='''+@rq_e+ '''group by xl,jsy1,yyhm order by xl,jsy1,yyhm'
--print @sql
exec(@sql) select A.name as name_temp,B.name as Type into #temp from tempdb..syscolumns A
join tempdb..systypes B on A.xtype=B.xusertype
where id = object_id('tempdb..##temp')--declare @countrq int
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
--select @countrq
if @countrq = 3 print '此值为3'
if @countrq = 0 print '此值为0'
if @countrq = 1 print '此值为1'
if @countrq = 2 print '此值为2'delete from ky_yhnycx
--select @countrqif @countrq = 0
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 1
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 2
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endif @countrq = 3
begin
insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d30,d31,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm
endselect * from ky_yhnycxdrop table ##temp
drop table #temp
谢谢,这是我所有的SQL语句
##temp是在exec里跑出来的,所以完了之后就没了....
这句话是
我删除数据,然后重新插入,这样会有问题吗?
如果我手动来用insert语句,那么就不会有错误,但是加了条件以后就不能用了,不知道为什么,我只是感觉,if语句没有起作用,是不是我写的if语句不对呢
if @countrq = 0 print '此值为0'
if @countrq = 1 print '此值为1'
if @countrq = 2 print '此值为2'
把这个拿掉试试?你这个表太多列了,呵呵,还不好测试
这样debug看看,把4个if,分别注释掉3个,这样测4次看看...
但看语句好象没什么问题啊
truncate table tt或者
delete from tt
dbcc checkident(tt,reseed,0)
先别删。不应该有问题才对 还没找到原因呢
http://community.csdn.net/Expert/topic/5041/5041234.xml?temp=.3221399
其实我的想法是这样的,如果是大月1,3,5,7,8,10,12,那么就有31号也就有d31字段的值,如果是小月4,6,9,11那么就没有31号的值,如果是润年2月就有29号,没有30,31号的值,如果是平年就没有29,30,31号的值,我没有更好的办法了,只好用这个笨的方法
服务器: 消息 207,级别 16,状态 3,行 43
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 50
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 50
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 57
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 57
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 57
列名 'd31' 无效。
就是if语句没有起到作用,奇怪啊
是啊,可是有什么办法呢?时间不饶人啊
select A.name as name_temp,B.name as Type into #temp from tempdb..syscolumns A
join tempdb..systypes B on A.xtype=B.xusertype
where id = object_id('tempdb..##temp')
然后用这个
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
判断是否是d29,d30,d31这三个字段,因为29,30,31不可能从中间分开,一定是29,30,31这样的顺序啊
判断是否是d29,d30,d31这三个字段??
这不明白
-------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
xl varchar
jsy1 varchar
yyhm varchar
d26 numeric
d27 numeric
d28 numeric
d1 numeric
d2 numeric
d3 numeric
d4 numeric
d5 numeric
d6 numeric
d7 numeric
d8 numeric
d9 numeric
d10 numeric
d11 numeric
d12 numeric
d13 numeric
d14 numeric
d15 numeric
d16 numeric
d17 numeric
d18 numeric
d19 numeric
d20 numeric
d21 numeric
d22 numeric
d23 numeric
d24 numeric
d25 numeric
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
判断是否是d29,d30,d31这三个字段??
这不明白
如果count(*)=0 说明 没有d29,d30,d31字段
如果count(*)=1 说明 有d29字段
如果count(*)=2 说明 有d29,d30字段
如果count(*)=3 说明 有d29,d30,d31字段
我这样想对不?因为前面的##temp表中的字段是顺序的,不会出现29,31,或30,31的情况吧
我以前就加了,还是一样的错误(所影响的行数为 207 行)
(所影响的行数为 31 行)服务器: 消息 207,级别 16,状态 3,行 45
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 53
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 53
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 61
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 61
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 61
列名 'd31' 无效。
再加个Order by看看
把
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
改为:
set @countrq = 0
set @countrq= @countrq + (select count(*) from #temp where name_temp='d29')
set @countrq= @countrq + (select count(*) from #temp where name_temp='d30')
set @countrq= @countrq + (select count(*) from #temp where name_temp='d31')
当你的##temp形成之后,也就是他的列已经定下来了(可能有d29,d30,d31,也可能没有)
这样,假如##temp出来没有d29,d30,d31
虽然程式是根据比如@count=0,走的是第一步,也就可以执行,
但是SQL在编译的时候,还是会编译(@count=1,2,3的情况,虽然程式不会执行这部分,但编译是要的),这样SQL就会发现,缺少d29,d30,d31,所以提示错误.
(所影响的行数为 0 行)0
把
set @countrq=(select count(*) from #temp where name_temp='d29' or name_temp='d30' or name_temp='d31')
改为:
set @countrq = 0
set @countrq= @countrq + (select count(*) from #temp where name_temp='d29')
set @countrq= @countrq + (select count(*) from #temp where name_temp='d30')
set @countrq= @countrq + (select count(*) from #temp where name_temp='d31')
虽然问题还没解决,但是心情不一样了,非常感谢,我看看有没有别的办法
declare @str varchar(8000)
set @str=''
if @countrq = 0
set @str='insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm'if @countrq = 1
set @str='insert into ky_yhnycx (xl,yyhm,jsy,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25)
select xl,yyhm,jsy1,d26,d27,d28,d29,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23,d24,d25 from ##temp
order by xl,jsy1,yyhm'if @countrq = 2
...
if @countrq = 3
...
exec (@str)
那说明编译的时候就出问题了语句并没有执行
这样的话playwarcraft(时间就像乳沟,挤挤还是有的) ( )
说的是有道理的
试了,结果还是一样的
服务器: 消息 207,级别 16,状态 3,行 34
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 42
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 42
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 50
列名 'd29' 无效。
服务器: 消息 207,级别 16,状态 1,行 50
列名 'd30' 无效。
服务器: 消息 207,级别 16,状态 1,行 50
列名 'd31' 无效。
if @countrq = 0
exec('insert into ... select ...')
if @countrq = 1
exec('insert into ... select ...')
if @countrq = 2
exec('insert into ... select ...')
if @countrq = 3
exec('insert into ... select ...')
HOHO
周末啊~~~
哈哈,然后ZZZZzzzz
HOHO
human或者ud