大力帮忙,存储过程问题,有TEXT字段类型的记录不能UNION

huayuxing 2003-11-18 11:30:08
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE dh_GetQueryResult (@TableName varchar(100),@QueryString varchar(100),@StrCondition varchar(100))

AS declare @Ssql varchar(8000) DECLARE Query_cursor CURSOR FOR

Select S.Name from SysColumns S inner join SysObjects O on S.ID=O.ID Where O.Name=@TableName

declare @FName varchar(100)

open Query_cursor

fetch Query_cursor into @FName

Set @Ssql='(Select * from '+@TableName +' Where Cast ('+@FName +' as varchar) like ''%' +@QueryString +'%'''+@Strcondition+')'

fetch next from Query_cursor into @FName

while @@fetch_status=0

begin

set @Ssql=@Ssql+( ' union (Select * from '+@TableName +' Where Cast ('+@FName +' as varchar) like ''%' +@QueryString+'%'''+@Strcondition+')')

fetch next from Query_cursor into @FName

end

close Query_cursor

deallocate Query_cursor

Exec(@Ssql)

return
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


其中有几个字段是TEXT类型的,任一查询都没问题,UNION了以后就说DISTINCT不能用于TEXT,IMAGE字段,
应该是对UNION的记录集自动DISTINCT了,急,谢谢
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
linxu 2003-11-18
  • 打赏
  • 举报
回复

CREATE PROCEDURE dh_GetQueryResult (@TableName varchar(100),@QueryString varchar(100),@StrCondition varchar(100))

AS declare @Ssql varchar(8000) DECLARE Query_cursor CURSOR FOR

Select S.Name from SysColumns S inner join SysObjects O on S.ID=O.ID Where O.Name=@TableName

declare @FName varchar(100)

open Query_cursor

fetch Query_cursor into @FName

Set @Ssql='Select * from '+@TableName +' Where (Cast ('+@FName +' as varchar) like ''%' +@QueryString +'%'''

fetch next from Query_cursor into @FName

while @@fetch_status=0

begin

set @Ssql=@Ssql+ ' or Cast ('+@FName +' as varchar) like ''%' +@QueryString +'%'''

fetch next from Query_cursor into @FName

end
set @Ssql=@Ssql+')'+@Strcondition

close Query_cursor

deallocate Query_cursor

Exec(@Ssql)

return
huayuxing 2003-11-18
  • 打赏
  • 举报
回复
union all 会所有的记录都重复显示出来的,
zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
因为union会去掉重复记录(相当于distinct),而text字段是不支持的,所以你改用:
union all 就行了.
zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE dh_GetQueryResult (@TableName varchar(100),@QueryString varchar(100),@StrCondition varchar(100))

AS declare @Ssql varchar(8000) DECLARE Query_cursor CURSOR FOR

Select S.Name from SysColumns S inner join SysObjects O on S.ID=O.ID Where O.Name=@TableName

declare @FName varchar(100)

open Query_cursor

fetch Query_cursor into @FName

Set @Ssql='(Select * from '+@TableName +' Where Cast ('+@FName +' as varchar) like ''%' +@QueryString +'%'''+@Strcondition+')'

fetch next from Query_cursor into @FName

while @@fetch_status=0

begin

set @Ssql=@Ssql+( ' union all (Select * from '+@TableName +' Where Cast ('+@FName +' as varchar) like ''%' +@QueryString+'%'''+@Strcondition+')')

fetch next from Query_cursor into @FName

end

close Query_cursor

deallocate Query_cursor

Exec(@Ssql)

return
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

34,668

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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