2014-12-02 82 views
0

我想使用我的表變量「@Customers」的變量,如「@Customers.Name」,我試過它像:Customers.Name或@ Customers.Name,但都顯示錯誤,請建議如何使用變量?如何使用傳遞給另一個存儲過程的表的變量?

CREATE TYPE [dbo].[TableType] AS TABLE(
[CustomerID] [int] NOT NULL, 
[Name] [varchar](255) NULL, 
[City] [varchar](50) NULL, 
[State] [char](2) NULL, 
[Zipcode] [varchar](20) NULL 
) 
GO 

ALTER PROCEDURE sp_GetCustomers 
AS 
BEGIN 
DECLARE @Customers AS TableType 
INSERT INTO @Customers(CustomerID,Name,City,State,ZipCode) 
SELECT C.CustomerID,C.FirstName + ' ' + C.LastName,A.City,A.State,A.ZipCode 
FROM Customers C INNER JOIN CustomerAddress A ON C.CustomerID=A.CustomerID 
EXEC sp_GetCustomersWithAddresses @Customers 

END 
go 

CREATE PROCEDURE sp_GetCustomersWithAddresses 
@Customers TableType READONLY 
AS 
BEGIN 
    SELECT * FROM @Customers where @Customers.Name="XYZ"; 
END 

回答

2

您應該在查詢中使用別名。您的存儲過程應該如下所示:

CREATE PROCEDURE sp_GetCustomersWithAddresses 
@Customers TableType READONLY 
AS 
BEGIN 
    SELECT * FROM @Customers a where a.Name='XYZ'; 
END 
0

在T-SQL中,您不像使用類的C#語言那樣訪問表列名稱。

但在你的榜樣,我認爲這是簡單:

Select * From @Customers Where [Name] = 'XYZ' 

我在括號中有「姓名」,因爲它是在T-SQL的保留字。而T-SQL應該知道[Name]是一個列,因爲它知道@Customers是TableType類型。所以這應該工作。

相關問題