SQL 中 with as 为什么不能这样子用?

zhengyang153 2010-03-16 04:53:13

create proc testProc
(
@placeNumber varchar(32), --场所编号
@placeName nvarchar(50), --场所名称
@PageIndex int = 0, --页码
@PageSize int = 10, --每页记录数
@RecordCount int output --记录总数
)
as
declare @sqlStr nvarchar(2000),@sqlStrEnd nvarchar(10)
begin
select * from EnvirInfo order by placeNumber
select @RecordCount = count(distinct placeNumber)
from EnvirInfo

set @sqlStr = 'select row_number() over(order by placeNumber ) as rownum, * from (select distinct * from EnvirInfo where 1=1 '
if @placeNumber = ''
begin
if @placeName <>''
begin
set @sqlStr = @sqlStr + 'and placeName = ' + @placeName;
end
end
else
begin
if @placeName <>''
begin
set @sqlStr = @sqlStr + 'and placeName = ' + @placeName + ' and placeNumber = ' + @placeNumber;
end
else
begin
set @sqlStr = @sqlStr + 'and placeNumber = ' + @placeNumber;
end
end
set @sqlStrEnd = ') '
set @sqlStr = @sqlStr + @sqlStrEnd

--重置当前页号
if (@RecordCount / @PageSize < @PageIndex+1)
set @PageIndex = @RecordCount / @PageSize
;
set @sqlStr = 'with temp as ( ' + @sqlStr;
set @sqlStr = @sqlStr + ') '
exec @sqlStr
--分页处理
select * from temp where a.rownum between (@PageIndex * @PageSize + 1) and (@PageIndex + 1) * @PageSize
end


上面的存储过程我创建成功。然后在执行的时候提示这个错误
消息 203,级别 16,状态 2,过程 UP_UA_GetEnvirInfoList,第 50 行
名称 'with temp as ( select row_number() over(order by placeNumber ) as rownum, * from (select distinct * from EnvirInfo where 1=1 and placeNumber = test) ) ' 不是有效的标识符。
...全文
579 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengyang153 2010-03-17
  • 打赏
  • 举报
回复
- -认真看了下。后面那个
@sqlStr = @sqlStr + ' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))+' + 1) * '+cast(@PageSize as varchar(50))+''

忘了加set。。。

现在可以了。谢谢
zhengyang153 2010-03-17
  • 打赏
  • 举报
回复
还是报错哦。
    @sqlStr = @sqlStr + ' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))+' + 1) * '+cast(@PageSize as varchar(50))+''


消息 102,级别 15,状态 1,过程 UP_UA_GetEnvirInfoList,第 52 行
'@sqlStr' 附近有语法错误。
Wudaode 2010-03-16
  • 打赏
  • 举报
回复

public
csdnfan 2010-03-16
  • 打赏
  • 举报
回复
如下会报错吗?我先回家了,回家后再看了

create proc testProc
(
@placeNumber varchar(32), --场所编号
@placeName nvarchar(50), --场所名称
@PageIndex int = 0, --页码
@PageSize int = 10, --每页记录数
@RecordCount int output --记录总数
)
as
declare @sqlStr nvarchar(2000),@sqlStrEnd nvarchar(10)
begin
select * from EnvirInfo order by placeNumber
select @RecordCount = count(distinct placeNumber)
from EnvirInfo

set @sqlStr = 'select row_number() over(order by placeNumber ) as rownum, * from (select distinct * from EnvirInfo where 1=1 '
if @placeNumber = ''
begin
if @placeName <>''
begin
set @sqlStr = @sqlStr + 'and placeName = ' + @placeName;
end
end
else
begin
if @placeName <>''
begin
set @sqlStr = @sqlStr + 'and placeName = ' + @placeName + ' and placeNumber = ' + @placeNumber;
end
else
begin
set @sqlStr = @sqlStr + 'and placeNumber = ' + @placeNumber;
end
end
set @sqlStrEnd = ') '
set @sqlStr = @sqlStr + @sqlStrEnd

--重置当前页号
if (@RecordCount / @PageSize < @PageIndex+1)
set @PageIndex = @RecordCount / @PageSize
;
set @sqlStr = 'with temp as ( ' + @sqlStr;
set @sqlStr = @sqlStr + ') '
--exec @sqlStr
--分页处理
@sqlStr = @sqlStr + ' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))+' + 1) * '+cast(@PageSize as varchar(50))+''
exec @sqlStr
--select * from temp where a.rownum between (@PageIndex * @PageSize + 1) and (@PageIndex + 1) * @PageSize
end
jack15850798154 2010-03-16
  • 打赏
  • 举报
回复
一.WITH AS的含义
WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。
特别对于UNION ALL比较有用。因为UNION ALL的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用WITH AS短语,则只要执行一遍即可。如果WITH AS短语所定义的表名被调用两次以上,则优化器会自动将WITH AS短语所获取的数据放入一个TEMP表里,如果只是被调用一次,则不会。而提示materialize则是强制将WITH AS短语里的数据放入一个全局临时表里。很多查询通过这种方法都可以提高速度。
二.使用方法
先看下面一个嵌套的查询语句:

select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')

上面的查询语句使用了一个子查询。虽然这条SQL语句并不复杂,但如果嵌套的层次过多,会使SQL语句非常难以阅读和维护。因此,也可以使用表变量的方式来解决这个问题,SQL语句如下:

declare @t table(CountryRegionCode nvarchar(3))
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')

select * from person.StateProvince where CountryRegionCode
in (select * from @t)


虽然上面的SQL语句要比第一种方式更复杂,但却将子查询放在了表变量@t中,这样做将使SQL语句更容易维护,但又会带来另一个问题,就是性能的损失。由于表变量实际上使用了临时表,从而增加了额外的I/O开销,因此,表变量的方式并不太适合数据量大且频繁查询的情况。为此,在SQL Server 2005中提供了另外一种解决方案,这就是公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性,同时,CTE要比表变量的效率高得多。

下面是CTE的语法:

[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,n ] ) ]
AS
( CTE_query_definition )

现在使用CTE来解决上面的问题,SQL语句如下:



with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)

select * from person.StateProvince where CountryRegionCode in (select * from cr)

其中cr是一个公用表表达式,该表达式在使用上与表变量类似,只是SQL Server 2005在处理公用表表达式的方式上有所不同。

在使用CTE时应注意如下几点:
1. CTE后面必须直接跟使用CTE的SQL语句(如select、insert、update等),否则,CTE将失效。如下面的SQL语句将无法正常使用CTE:


with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.CountryRegion -- 应将这条SQL语句去掉
-- 使用CTE的SQL语句应紧跟在相关的CTE后面 --
select * from person.StateProvince where CountryRegionCode in (select * from cr)


2. CTE后面也可以跟其他的CTE,但只能使用一个with,多个CTE中间用逗号(,)分隔,如下面的SQL语句所示:



with
cte1 as
(
select * from table1 where name like 'abc%'
),
cte2 as
(
select * from table2 where id > 20
),
cte3 as
(
select * from table3 where price < 100
)
select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id

3. 如果CTE的表达式名称与某个数据表或视图重名,则紧跟在该CTE后面的SQL语句使用的仍然是CTE,当然,后面的SQL语句使用的就是数据表或视图了,如下面的SQL语句所示:


-- table1是一个实际存在的表

with
table1 as
(
select * from persons where age < 30
)
select * from table1 -- 使用了名为table1的公共表表达式
select * from table1 -- 使用了名为table1的数据表

4. CTE 可以引用自身,也可以引用在同一 WITH 子句中预先定义的 CTE。不允许前向引用。

5. 不能在 CTE_query_definition 中使用以下子句:

(1)COMPUTE 或 COMPUTE BY

(2)ORDER BY(除非指定了 TOP 子句)

(3)INTO

(4)带有查询提示的 OPTION 子句

(5)FOR XML

(6)FOR BROWSE

6. 如果将 CTE 用在属于批处理的一部分的语句中,那么在它之前的语句必须以分号结尾,如下面的SQL所示:

declare @s nvarchar(3)
set @s = 'C%'
; -- 必须加分号
with
t_tree as
(
select CountryRegionCode from person.CountryRegion where Name like @s
)
select * from person.StateProvince where CountryRegionCode in (select * from t_tree)

CTE除了可以简化嵌套SQL语句外,还可以进行递归调用,
zhengyang153 2010-03-16
  • 打赏
  • 举报
回复
关键问题是我把那个加号拿出去后,依然报错
错误是同一个
csdnfan 2010-03-16
  • 打赏
  • 举报
回复
哦,我的一个+号写到引号里面了,你应该可以看出来的。。。
@sqlStr = @sqlStr + ' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))+' + 1) * '+cast(@PageSize as varchar(50))+''
zhengyang153 2010-03-16
  • 打赏
  • 举报
回复
whit as 后面好像是必须跟sql语句。
首先谢谢你的回复。可是我按你说的把分页处理后面的替换成你给我的代码。
分析代码的时候提示我这一段有语法错误

@sqlStr = @sqlStr + ' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))'+ + 1) * '+cast(@PageSize as varchar(50))+''
csdnfan 2010-03-16
  • 打赏
  • 举报
回复
你这两句就不是一个级别的,你先exec @sqlStr 先后再select * from temp where a.rownum between (@PageIndex * @PageSize + 1) and (@PageIndex + 1) * @PageSize
看一下那个with 的语法吧。
执行到exec @sqlStr会报错的,你的语法就不对了。
你在----分页处理后替换为
@sqlStr = @sqlStr +' select * from temp where a.rownum between ('+cast(@PageIndex as varchar(50))+' * '+cast(@PageSize as varchar(50))+' + 1) and ('+cast(@PageIndex as varchar(50))'+ + 1) * '+cast(@PageSize as varchar(50))+''
exec @sqlStr

把上面那个 exec @sqlStr 去掉

zhengyang153 2010-03-16
  • 打赏
  • 举报
回复
为什没没有人来帮我看- -自己顶一下

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧