SQL Server 2008使用sproc中的函数

时间:2016-02-17 20:46:19

标签: sql sql-server sql-server-2008

我正在使用此功能:

ALTER FUNCTION [dbo].[fncGetCustInfo] (@IShopID int)
RETURNS

@CustomerInfo table (ShopID int, CustID int)

AS
BEGIN

INSERT INTO @CustomerInfo
SELECT  ShopID,
CustomerID
FROM dbo.ShopTbl
WHERE ShopID= @IShopID  
RETURN
END

这是我的存储过程:

ALTER PROC [dbo].[GetCustomerInfo]

@IShopID int 
as
BEGIN

declare @CustomerName varchar(50), @ShopD int, @CustomerID int, @Date date

--Return the customer ids using the function I have created
SELECT @CustomerID = fncGetCustInfo.CustomerID FROM dbo.fncGetCustInfo(@IShopID)
--Set the customer names column using the customer ids I retrived using my function
SELECT @CustomerName = CustomerName
from dbo.CustomerTbl
where CustomerID = @CustomerID

Select @Date = GETDATE()

END

我的问题是关于sproc中的这一行:

SELECT @CustomerID = fncGetCustInfo.CustomerID FROM dbo.fncGetCustInfo(@IShopID)

这会通过调用我之前创建的函数从我在名为CustomerInfo的函数中创建的表中返回CustomerID吗?

1 个答案:

答案 0 :(得分:0)

我看到的方式我的回答是肯定的,但是我没有创建一个函数来返回结果,而是将这个函数嵌入proc中:

ALTER PROC [dbo].[GetCustomerInfo]
(

@IShopID int 
)
as
BEGIN

declare @CustomerName varchar(50),  @CustomerID int, @Date date   

SET @CustomerID = (SELECT TOP 1 ShopID,CustomerID FROM dbo.ShopTbl WHERE ShopID= @IShopID)


--Set the customer names column using the customer ids I retrived using my     
SELECT @CustomerName = CustomerName
from dbo.CustomerTbl
where CustomerID = @CustomerID

Select @Date = GETDATE()

END

除非您打算在其他地方使用该功能,但请记住该功能可能会产生性能问题。