我想使用我的表变量" @ 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
答案 0 :(得分:2)
您应该在查询中使用别名。您的存储过程应如下所示:
CREATE PROCEDURE sp_GetCustomersWithAddresses
@Customers TableType READONLY
AS
BEGIN
SELECT * FROM @Customers a where a.Name='XYZ';
END
答案 1 :(得分:0)
在T-SQL中,您不会以这种方式访问表列名称,就像使用C#语言一样。
但在你的例子中,我认为它很简单:
Select * From @Customers Where [Name] = 'XYZ'
我在括号中有'Name',因为它是T-SQL中的保留字。并且T-SQL应该知道[Name]是一个列,因为它知道@Customers的类型是TableType。所以这应该有用。