我想在Select Query的连接语句中调用存储过程。
例如,
Select *
from test
left join test1 on GetID(test.id)=test1.id
这个想法是匹配一对多的关系。
表的结构是
表:测试
ID Name
1 abc
2 te
表:Test1
Id TestID Name
1 1 xxx
2 1 yyy
3 1 zzz
4 2 aaa
存储过程:
Create procedure GETID
@id int
as
begin
select top 1 id
from test1
where testid = @id
end
答案 0 :(得分:2)
您可以将存储过程转换为内联表值函数,也可以将查询放在var setting1:String {
get {
if let setting1 = userDefaults.valueForKey("setting1") as? String {
return setting1
} else {
//set the value for the default settings 1
let setting1 = "Value"
userDefaults.setObject(setting1, forKey: "setting1")
userDefaults.synchronize()
return setting1
}
}
set {
userDefaults.setObject(newValue, forKey: "setting1")
userDefaults.synchronize()
}
}
中:
OUTER APPLY
答案 1 :(得分:1)
改为使用标量函数。
CREATE FUNCTION GETID
(
@id int
)
RETURNS int
AS
BEGIN
return (select top 1 id from test1 where testid=@id)
END
或者,审核帖子中的方法:Get top 1 row of each group
使用交叉应用(或外部应用),在查询的右侧执行一次。 或者,在分区上使用row_number()来对组行进行排名,并根据排名进行选择。
declare @test table (id int, name varchar(100))
insert into @test (id, name) values (1, 'abc')
insert into @test (id, name) values (1, 'te')
declare @test1 table (id int, testid int, name varchar(100))
insert into @test1 (id, testid, name) values (1, 1, 'xxx')
insert into @test1 (id, testid, name) values (2, 1, 'yyy')
insert into @test1 (id, testid, name) values (3, 1, 'zzz')
insert into @test1 (id, testid, name) values (4, 2, 'aaa')
Select * from @test t
cross apply (select top 1 * from @test1
where testid = t.id
order by id) -- change group order as needed
as t1