注册 登录
编程论坛 SQL Server论坛

语``问题

copy462829 发布于 2007-02-08 14:11, 423 次点击
set @strJoin=' Re_Name like ''%'+rtrim(@caption)+'%''' --

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

运行的时候 上句错误 怎么样的写法 才能得倒我要想要的出 ,请大家多多指点
7 回复
#2
棉花糖ONE2007-02-08 14:56
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
上次不是给你写过吗,照着改吧.
#3
copy4628292007-02-08 20:38

没有读值,是空的

#4
copy4628292007-02-08 20:42

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 请问这句怎么写才对

#5
棉花糖ONE2007-02-08 20:46
select @RecordCount=count(*) from Regimental where @strJoin
这句是错误的啊,@strJoin是字符串,没有这种写法的,你只有把整个语句都变成字符串才行,就像exec中括号里面那样
#6
棉花糖ONE2007-02-08 20:47
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
这语句我执行的时候输出是正确的啊
#7
copy4628292007-02-10 17:41

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

#8
棉花糖ONE2007-02-10 18:29
declare @f int
exec sp_executesql N'select @f=count(1) from student',N'@f int output',@f output
select @f as m
用这种方法试试
1