返回函数中的表行

时间:2011-06-20 09:09:03

标签: sql-server tsql

CREATE FUNCTION dbo.GetCustomers
(
  @ZIPCode VARCHAR(255)
)
RETURNS TABLE

AS BEGIN
SELECT * dbo.GetCustomers
WHERE dbo.GetCustomers.ZIPCODE = ZIPCode 
RETURN
END

我不熟悉语法。请帮忙。

3 个答案:

答案 0 :(得分:7)

值得猜测参数@ZIPCode将作为@ZIPCode进入查询

CREATE FUNCTION dbo.GetCustomers
(
  @ZIPCode VARCHAR(255)
)
RETURNS TABLE

AS
RETURN (
    SELECT * FROM dbo.Customers
    WHERE dbo.Customers.ZIPCODE = @ZIPCode
    )
END

您的第一次尝试WHERE dbo.Customers.ZIPCODE = ZIPCodeWHERE dbo.Customers.ZIPCODE = dbo.CustomersZIPCode相同。也就是说,返回所有行

答案 1 :(得分:3)

试试这个

CREATE FUNCTION dbo.GetCustomers ( 
@ZIPCode VARCHAR(255) 
) 
RETURNS TABLE  
AS return (SELECT * FROM dbo.Customers WHERE dbo.Customers.ZIPCODE = @ZIPCode) 

答案 2 :(得分:1)

当正常的存储过程可以解决问题时,似乎没有任何理由创建一个函数来执行此操作。

CREATE PROCEDURE GetCustomers
@ZIPCode VARCHAR(255)

AS

SELECT * FROM dbo.Customers
WHERE Customers.ZIPCODE = @ZIPCode