表 infoid type
0 1
1 1
2 2
. .
我想写一个查询,找到一种类型的所有信息的详细内容detail,为此我已经写了一个函数f(@id),即输入id就可以通过其他表联接找到detail
现在我写的查询想法是
select
id artical_id,
f(id) artical_detail -- 这一条我不知道怎么表达,就是想逐条对对应的id查询到对应的detail. 可是这个参数怎么传递??
from info
where type=**高手指教! 十分感谢!
0 1
1 1
2 2
. .
我想写一个查询,找到一种类型的所有信息的详细内容detail,为此我已经写了一个函数f(@id),即输入id就可以通过其他表联接找到detail
现在我写的查询想法是
select
id artical_id,
f(id) artical_detail -- 这一条我不知道怎么表达,就是想逐条对对应的id查询到对应的detail. 可是这个参数怎么传递??
from info
where type=**高手指教! 十分感谢!
a b
1 1
1 2
1 3
2 1
2 2
3 1
--如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1 create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
goif object_id('pubs..f_hb') is not null
drop function f_hb
go--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tbdrop table tb--结果
a b
----------- ------
1 1,2,3
2 1,2
3 1(所影响的行数为 3 行)
多个前列的合并
数据的原始状态如下:
ID PR CON OP SC
001 p c 差 6
001 p c 好 2
001 p c 一般 4
002 w e 差 8
002 w e 好 7
002 w e 一般 1
===========================
用SQL语句实现,变成如下的数据
ID PR CON OPS
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)if object_id('pubs..tb') is not null
drop table tb
gocreate table tb
(
id varchar(10),
pr varchar(10),
con varchar(10),
op varchar(10),
sc int
)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '差', 6)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '好', 2)
insert into tb(ID,PR,CON,OP,SC) values('001', 'p', 'c', '一般', 4)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '差', 8)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '好', 7)
insert into tb(ID,PR,CON,OP,SC) values('002', 'w', 'e', '一般', 1)
goif object_id('pubs..test') is not null
drop table test
go
select ID,PR,CON , OPS = op + '(' + cast(sc as varchar(10)) + ')' into test from tb--创建一个合并的函数
if object_id('pubs..f_hb') is not null
drop function f_hb
go
create function f_hb(@id varchar(10),@pr varchar(10),@con varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(OPS as varchar) from test where id = @id and @pr = pr and @con = con
set @str = right(@str , len(@str) - 1)
return(@str)
End
go--调用自定义函数得到结果:
select distinct id ,pr , con , dbo.f_hb(id,pr,con) as OPS from testdrop table tb
drop table test--结果
id pr con OPS
---------- ---------- ---------- -------------------
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)(所影响的行数为 2 行)create table b
(col varchar(20))insert b values ('a')
insert b values ('b')
insert b values ('c')
insert b values ('d')
insert b values ('e')
declare @sql varchar(1024)
set @sql=''
select @sql=@sql+b.col+',' from (select col from b) as b
set @sql='select '''+@sql+''''
exec(@sql)
from info
where type = **
怎么说,就是说通过条件a查询所有符合的id.然后根据查询出来的id,逐条运用函数f获取其他信息. 函数我已经写好了,但是不知道这个函数的参数怎么传递.因为是动态的.
id artical_id,
f(id) artical_detail -- 这一条我不知道怎么表达,就是想逐条对对应的id查询到对应的detail. 可是这个参数怎么传递??
from info
where type=**-------------------------是这个意思吗?select id, artical_id = dbo.f(id) from info wher type = **
select id as artical_id,
f(id) as artical_detail
from info
where type=**也就是说查询出来的表是两列, artical_id 和artical_detail
但是现在提示我函数的参数id列名无效
这个id就是前面查询出来的id,是动态的.
想问下参数该怎么传递?
--f(id) as artical_detail
dbo.f(id) as artical_detail
from info
where type=**-----------------自定义函数要加DBOwner(dbo)引用。这就没错了,就这么传递参数的,把列名id作为参数。
illegal_id id,
illegal_content_id content_id,
dbo.f_q_illegalAuthorCityTopic(illegal_id) city --这里参数用id我也试过,一样的
from illegal_info
where illegal_editor_id = 0调试的时候是加了dbo.的,但是这样一直没办法运行成功啊.
OR
没返回结果集
OR
返回结果集不对
对象名 'illegal_id' 无效。换成id做参数,错误会变成消息 207,级别 16,状态 1,第 5 行
列名 'id' 无效。
---------------
illegal_id
illegal_content_id
illegal_type
illegal_editor_id函数
ALTER FUNCTION [dbo].[f_q_illegalAuthorCityTopic]
(
@illegal_id int
)
RETURNS varchar(5000)函数具体实现我就不贴了,比较长,也跟这个没太大关系谢谢!
select
illegal_id id,
illegal_content_id content_id
from illegal_info
where illegal_editor_id = 0
我现在不知道是函数调用的问题还是定义的问题??
select dbo.f_q_illegalAuthorCityTopic(7)
报错消息 4121,级别 16,状态 1,第 3 行
找不到列 "dbo" 或用户定义的函数或聚合 "dbo.f_q_illegalAuthorCityTopic",或者名称不明确。试了其他几个以前写过的正确函数也是这样,这是什么原因哪...昏了我
select 数据库名.dbo.f_q_illegalAuthorCityTopic(7)
把表結構貼出來,-->希望得到什麼結果.
或者把function貼出,大家可以幫你找錯誤