我并不总是喜欢鼓励使用动态 SQL,但以下内容将完成您想要完成的工作。
DECLARE @ColumnName varchar(10) = 'ItemName', @SearchTerm varchar(5) = 'pat', @sql varchar(1000) = ''
SELECT @sql = 'SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved],
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]
FROM OITW
INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]
WHERE OITM.' + @ColumnName + ' LIKE ''%' + @SearchTerm + '%'''
EXEC (@sql)
以下是我将如何真正做到这一点,但你可能有不想这样做的理由。由你决定。
SELECT
OITM.[ItemCode] AS [Item Code],
OITM.[ItemName] AS [Item Name],
OITM.[FrgnName] AS [Merchandise Type],
OITM.[OnHand] AS [In Stock],
OITM.[IsCommited] AS [Customer Reserved],
OITM.[Onorder] AS [On Order],
OITW.[AvgPrice] AS [Cost]
FROM OITW
INNER JOIN OITM ON OITM.[ItemCode] = OITW.[ItemCode]
WHERE (OITM.ItemName LIKE '%' + @SearchTerm + '%'
OR OITM.FrgnName LIKE '%' + @SearchTerm + '%')...
祝你好运!