标题:语``问题
只看楼主
copy462829
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2007-1-31
 问题点数:0 回复次数:7 
语``问题
set @strJoin=' Re_Name like ''%'+rtrim(@caption)+'%''' --

select @RecordCount=count(*) from Regimental where @strJoin --

运行的时候 上句错误 怎么样的写法 才能得倒我要想要的出 ,请大家多多指点
2007-02-08 14:11
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
得分:0 
declare @count int
declare @str varchar(200)
declare @m varchar(8000)
declare @n varchar(2000)
set @n='jin%'
set @str='name like '''+@n+''''
exec('select'+'''+@count+'''+'=count(1) from student where '+@str)
print @count
上次不是给你写过吗,照着改吧.

26403021 sql群 博客 blog./user15/81152/index.shtml
2007-02-08 14:56
copy462829
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2007-1-31
得分:0 

没有读值,是空的

2007-02-08 20:38
copy462829
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2007-1-31
得分:0 

create proc RegimentalSearch
@caption nvarchar(100),--标题
@PageSize int,--页大小
@PageIndex int, --多少页
@WordLength int
as
begin
declare @RecordCount int --行
declare @PageCount int --页

declare @str nvarchar(1000) --运行参数
declare @strJoin nvarchar(500) --存放条件
if(@caption != '')
begin
set @strJoin=' Re_Name like ''%'+rtrim(@caption)+'%'''
end
else
begin
set @strJoin='Re_ID=0' --显示空' '
end
declare @aa nvarchar(100)
exec('select '+'''@RecordCount'''+' = count(*) from Regimental where'+ @strJoin) --反回行

print(@RecordCount)

set @PageCount=ceiling(@RecordCount*1.0/@PageSize)

--RecordCount和PageCount打印出来是空的
如果不要exec,写成 select @RecordCount=count(*) from Regimental where @strJoin 请问这句怎么写才对

2007-02-08 20:42
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
得分:0 
select @RecordCount=count(*) from Regimental where @strJoin
这句是错误的啊,@strJoin是字符串,没有这种写法的,你只有把整个语句都变成字符串才行,就像exec中括号里面那样

26403021 sql群 博客 blog./user15/81152/index.shtml
2007-02-08 20:46
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
得分:0 
declare @count int
declare @str varchar(200)
declare @m varchar(8000)
declare @n varchar(2000)
set @n='jin%'
set @str='name like '''+@n+''''
exec('select'+'''+@count+'''+'=count(1) from student where '+@str)
print @count
这语句我执行的时候输出是正确的啊

26403021 sql群 博客 blog./user15/81152/index.shtml
2007-02-08 20:47
copy462829
Rank: 1
等 级:新手上路
帖 子:112
专家分:0
注 册:2007-1-31
得分:0 

create proc RegimentalSearch
@caption nvarchar(100),--标题
@PageSize int,--页大小
@PageIndex int, --多少页
@WordLength int
as
begin
declare @RecordCount int --行
declare @PageCount int --页

declare @str nvarchar(1000) --运行参数
declare @strJoin nvarchar(500) --存放条件
if(@caption != '')
begin
set @strJoin=' Re_Name like ''%'+rtrim(@caption)+'%'''
end
else
begin
set @strJoin='Re_ID=0'
end
declare @aa nvarchar(100)
exec('select '+'''@RecordCount'''+' = count(*) from Regimental where'+ @strJoin) --反回行

print @RecordCount

set @PageCount=ceiling(@RecordCount*1.0/@PageSize)


print @PageCount


if @PageIndex=0 or @PageCount<=1
begin
set @str='select top'+str(@PageSize)+' a.Re_ID,
case when len(a.Re_Name)>'+str(@WordLength)+' then substring (a.Re_Name,0,'+str(@WordLength)+')+''...''
else a.Re_Name end as name
,a.Re_Time,(select Co_ClassName from CoAskClass where Co_ID =a.Re_Type) as leixing ,a.Re_Count from Regimental as a where '+@strJoin+'order by a.Re_Count desc'

end
else if @PageIndex=@PageCount-1
begin

set @str='select * from (select top '+str(@RecordCount-@PageSize*@PageIndex)+' a.Re_ID,
case when len(a.Re_Name)>'+str(@WordLength)+' then substring (a.Re_Name,0,'+str(@WordLength)+')+''...''
else a.Re_Name end as name
,a.Re_Time,(select Co_ClassName from CoAskClass where Co_ID =a.Re_Type) as leixing ,a.Re_Count from Regimental as a where '+@strJoin+'order by a.Re_Count asc) temptable order by Re_Count desc '
end
else
begin

set @str='select top'+str(@PageSize)+' * from(select top'+str( @RecordCount - @PageSize * @PageIndex)+' a.Re_ID,
case when len(a.Re_Name)>'+str(@WordLength)+' then substring (a.Re_Name,0,'+str(@WordLength)+')+''...''
else a.Re_Name end as name
,a.Re_Time,(select Co_ClassName from CoAskClass where Co_ID =a.Re_Type) as leixing ,a.Re_Count from Regimental as a where '+@strJoin+'order by a.Re_Count asc) temptable order by Re_Count desc '
end
exec(@str)
end

-- @RecordCount @PageCount没有打印值,
-- exec exec('select '+'''@RecordCount'''+' = count(*) from Regimental where'+ @strJoin) 返回 @RecordCount=2 ,问题我没给这列起别名,
-- 打印无值 --主要的问题是他不变量赋值,@RecordCount=null

2007-02-10 17:41
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
得分:0 
declare @f int
exec sp_executesql N'select @f=count(1) from student',N'@f int output',@f output
select @f as m
用这种方法试试

26403021 sql群 博客 blog./user15/81152/index.shtml
2007-02-10 18:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-120210-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.140909 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved