使用局部变量来查询标量值函数

时间:2015-11-12 15:15:03

标签: sql sql-server

我有一个返回cat代码列表的查询:

SELECT  Catcode From tbluserslogin
join tblUserRoleMapping
    on tblUserRoleMapping.UserID = tblUsersLogin.LoginUserID
join tblCats
    on UserName = CatCode
where RoleID = 87

返回:

enter image description here

SELECT dbo.GetCatManager('', 1) <--this is my function 

根据输入的catcode

返回管理员代码

如何从cat代码列表中创建一个可以在我的函数中使用的变量?

我正在查看局部变量但是语法有问题。

像工作一样:

@CatCode = CatCode

所以我的功能看起来像这样:

SELECT dbo.GetAgentManager('@CatCode', 1) 

这可能,我该怎么做?

2 个答案:

答案 0 :(得分:1)

认为你之后的目标

SELECT  dbo.GetAgentManager(Catcode,1) AS ManagerCode 
From tbluserslogin
join tblUserRoleMapping
    on tblUserRoleMapping.UserID = tblUsersLogin.LoginUserID
join tblCats
    on UserName = CatCode
where RoleID = 87

答案 1 :(得分:1)

您可以直接使用您的功能:

SELECT  Catcode, dbo.GetAgentManager(Catcode, 1) as Manager From tbluserslogin
join tblUserRoleMapping
    on tblUserRoleMapping.UserID = tblUsersLogin.LoginUserID
join tblCats
    on UserName = CatCode
where RoleID = 87