我的存储过程接受一个参数并返回单个值。我的要求是对带有公司 id 的表的每一行调用存储过程。我试过了,但它不起作用
SELECT CompanyID,CompanyName,EmailID,'Exec Usp_GetCount'
+CONVERT(varchar(256),companyID) 'Employees' from Tbl_Company
谢谢
我的存储过程接受一个参数并返回单个值。我的要求是对带有公司 id 的表的每一行调用存储过程。我试过了,但它不起作用
SELECT CompanyID,CompanyName,EmailID,'Exec Usp_GetCount'
+CONVERT(varchar(256),companyID) 'Employees' from Tbl_Company
谢谢
在您的选择中使用函数而不是存储过程 - 然后您可以执行以下操作:
SELECT CompanyId, CompanyName, dbo.GetCount(CompanyId) 'Employees'
FROM Tbl_Company
WHERE...
试试这个——
如果 SP:
DECLARE @temp TABLE
(
CompanyID INT
, Cnt INT
)
DECLARE @CompanyID INT
DECLARE cur CURSOR FAST_FORWARD READ_ONLY FOR
SELECT DISTINCT CompanyID
FROM dbo.Tbl_Company
OPEN cur
FETCH NEXT FROM cur INTO @CompanyID
WHILE @@FETCH_STATUS = 0 BEGIN
DECLARE @Result INT
EXEC @Result = dbo.Usp_GetCount CONVERT(VARCHAR(256), @CompanyID)
INSERT INTO @temp (CompanyID, Cnt)
SELECT @CompanyID, @Result
FETCH NEXT FROM cur INTO @CompanyID
END
CLOSE cur
DEALLOCATE cur
SELECT
c.CompanyID
, c.CompanyName
, c.EmailID
, Employees = t.Cnt
FROM dbo.Tbl_Company c
JOIN @temp t ON t.CompanyID = c.CompanyID
如果是标量函数:
SELECT
c.CompanyID
, c.CompanyName
, c.EmailID
, Employees = dbo.GetCount(c.CompanyID)
FROM dbo.Tbl_Company c