是一个文章表article
articleid(int) classid(int) ....
一个文章分类表aclass
classid(int) parentid(int) articlenum(int)...
现在要统计用一条语句更新所有aclass表中记录的articlenum这个字段。aritclenum = 所有这个分类下的文章数,包括这个分类的子分类的文章。
articleid(int) classid(int) ....
一个文章分类表aclass
classid(int) parentid(int) articlenum(int)...
现在要统计用一条语句更新所有aclass表中记录的articlenum这个字段。aritclenum = 所有这个分类下的文章数,包括这个分类的子分类的文章。
第一种,用一个函数,这个函数返回一个逗号分隔的子分类id的列表,类似 '25,37' update aclass set articlecount = (select sum(articlenum) from aclass where classid in (select dbo.f_news_getchildclassstr(classid)))
但是错误如下:
服务器: 消息 245,级别 16,状态 1,行 1
将 varchar 值 '25,37' 转换为数据类型为 int 的列时发生语法错误。
由于我的函数要用到的参数不是常量,好像又不能用语句拼接的方式。
第二种,函数的返回值是一个表,记录是所有子分类的id,有多行记录
update aclass set articlecount = (select sum(articlenum) from aclass where classid in (select classid from dbo.f_news_getchildclass(classid)))
错误如下:我这里函数的参数的写法也是有问题的,通不过
服务器: 消息 155,级别 15,状态 1,行 1
'classid' 不是可以识别的 OPTIMIZER LOCK HINTS 选项。
望高手指点指点。
aclass就是分类表现在要统计每个分类的文章数(包括子分类的),更新到aclass表的articlecount字段中。
create table aclass(classid int, parentid int, articlenum int)insert article select 1
union all select 1
union all select 2
union all select 2
union all select 3insert aclass select 1, 0, 0
union all select 2, 1, 0
union all select 3, 0, 0
GO--自定义函数的作用是:输入1,返回,1,2,
create function dbo.fn_GetAllChildren(@f int)
returns varchar(1000)
as
begin
declare @str varchar(300)
set @str=cast(@f as varchar(10))if exists(select 1 from aclass where parentid =@f)
select @str=@str+','+ dbo.fn_GetAllChildren(classid) from aclass where parentid =@freturn @str + ','
end
go
--select dbo.fn_GetAllChildren(classid) from aclass
--如果是1就返回 ,1,2,
应该会用到charindex
sum(case when charindex( ',' + convert(varchar,a.classid) + ',', ',1,2,') > 0 then 1 else 0 end) [count]drop function fn_GetAllChildren
drop table article
drop table aclass,************没有解决结束***************,只是给一个思路。
就是用到最终的update语句的时候就不对了。我问的是最后这两种update的写法怎么改一下就能通过了。
update a
set a.articlecount = (select sum(articlenum) from aclass
where (select ','+dbo.f_news_getchildclassstr(a.classid) + ',' like ','+classid + ',' )
)
from aclass a
第二种,试试:
update a
set articlecount = (select sum(articlenum) from aclass
where classid in (select classid from dbo.f_news_getchildclass(a.classid)))
from aclass a
第一种
服务器: 消息 156,级别 15,状态 1,行 3
在关键字 'like' 附近有语法错误。第二种
服务器: 消息 170,级别 15,状态 1,行 3
第 3 行: '.' 附近有语法错误。
是一个文章表article
articleid(int) classid(int) ....
一个文章分类表aclass
classid(int) parentid(int) articlenum(int) articlecount(int)...
现在要统计用一条语句更新所有aclass表中记录的articlecount这个字段。
articlenum这列已经有值了,是每个分类中的文章数,不包含子分类aritclecount = 所有这个分类下的文章数,包括这个分类的子分类的文章。
25 13 25,37 0 0
26 13 26,40 0 0
28 13 28,33,34,35,46 0 0
33 28 33 0 0
34 28 34 1 0
35 28 35 0 0
38 13 38 0 0
40 26 40 0 0
45 47 45 0 0
46 28 46 0 0
51 13 51 0 0
52 12 52 0 0
68 66 68 0 0
69 66 69 0 0
70 47 70 0 0
77 76 77 26 0
78 76 78 9 0
79 76 79 4 0
80 76 80 8 0
81 76 81 4 0
82 76 82 11 0
83 76 83 13 0
84 76 84 7 0
86 85 86 7 0
87 85 87 4 0
93 76 93 34 0
94 76 94 10 0
96 13 96 0 0
119 13 119 0 0
120 0 120 6645 0
122 0 122 8271 0
123 0 123 331 0
124 0 124 5015 0
27 13 27,39 0 0
49 0 49 150 0
71 66 71 6 0
126 0 126 159 0
127 0 127 103 0
128 0 128 147 0
129 0 129 82 0
130 0 130 11 0
50 12 50 9 0
142 131 142 6 0
98 31 98 0 0
30 13 30 0 0
44 13 44 0 0
29 13 29 0 0
37 25 37 0 0
39 27 39 0 0
36 13 36 0 0
31 13 98,31 0 0
47 13 45,70,47 0 0
131 0 142,131,132,133,134,135,136,137,138,140,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163 1 0
132 131 132 4 0
133 131 133 0 0
134 131 134,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163 20 0
135 131 135 0 0
136 131 136 0 0
137 131 137 1 0
138 131 138 0 0
140 131 140 0 0
144 134 144,149,150,151 0 0
145 134 145,152,153,154,155,156 0 0
146 134 146,157,158,159,160,161,162 0 0
147 134 147,163 0 0
148 134 148 0 0
149 144 149 10 0
150 144 150 10 0
151 144 151 10 0
152 145 152 10 0
153 145 153 10 0
154 145 154 11 0
155 145 155 9 0
156 145 156 0 0
157 146 157 0 0
158 146 158 0 0
159 146 159 0 0
160 146 160 0 0
161 146 161 0 0
162 146 162 0 0
163 147 163 0 0
164 0 164 0 0
165 0 165,167 3 0
167 165 167 11 0
139 0 139 23 0
166 0 166 29 0
168 0 168 19 0
169 0 169,170 931 0
170 169 170 18 0
171 0 171,172,177,180,183,184,185,187,188,189,190,173,174,175,181,195,186,196,197,198,201,202,204,176,178,179,191,192,193,194,203 0 0
172 171 172 213 0
177 173 177 8 0
180 178 180 147 0
183 178 183 359 0
184 171 184,185,186 0 0
185 184 185 5 0
187 171 187,195,196,197,198 1 0
188 171 188,201,202,204,203 0 0
189 171 189,191,192,193,194 1 0
190 171 190 3 0
173 171 177,173,174,175,176 0 0
174 173 174 12 0
175 173 175 30 0
181 178 181 121 0
195 187 195 341 0
186 184 186 5 0
196 187 196 164 0
197 187 197 11 0
198 187 198 131 0
205 0 205 1256 0
206 0 206 4 0
201 188 201 27 0
202 188 202 0 0
204 188 204 0 0
1 0 1 12 0
176 173 176 19 0
178 171 180,183,181,178,179 0 0
179 178 179 25 0
191 189 191 96 0
192 189 192 37 0
193 189 193 1 0
194 189 194 10 0
203 188 203 0 0
207 0 207 4 0
208 0 208,209,210,211,212,215,216,213,214,218,219,217,220 0 0
209 208 209 4710 0
210 208 210 2011 0
211 208 211 1411 0
212 208 212 382 0
215 208 215 2308 0
216 208 216 912 0
213 208 213 143 0
214 208 214 404 0
218 208 218 6300 0
219 208 219 251 0
217 208 217 838 0
220 208 220 1584 0