DECLARE @customerIDTable TABLE (customerID INT) INSERT @customerIDTable VALUES (1) INSERT @customerIDTable VALUES (2) INSERT @customerIDTable VALUES (3) SELECT @customerIDTable.customerID, a.* FROM @customerIDTable INNER JOIN Address a ON a.customerID = @customerIDTable.customerIDwhich doesn't work, but
SELECT c.customerID, a.* FROM Address a INNER JOIN @customerIDTable c ON a.icustid = c.customerIDworks. A bug in the MS SQL parser?
1. EXECUTE ('SELECT street, city, state >> FROM Address >> WHERE customerID IN (' + @pkString + ')') >> >> EXECUTE ('SELECT orderID, amount >> FROM Orders >> WHERE customerID IN (' + @pkString + ')') >> >> -- Additional SELECTs... >> >>2. CREATE TABLE #customerIDTable (customerID INT) >> >> DECLARE @StartIndex INT >> SET @StartIndex = 0 >> DECLARE @CustomerID VARCHAR(10) >> >> SET @CommaIndex = CHARINDEX(',', @pkString, @StartIndex) >> >> WHILE @CommaIndex > 0 >> BEGIN >> SET @CustomerID = >> LTRIM(SUBSTRING(@pkString, @StartIndex, @CommaIndex - @StartIndex)) >> >> INSERT #customerIDTable VALUES (@CustomerID) >> >> SET @StartIndex = @CommaIndex + 1 >> SET @CommaIndex = CHARINDEX(',', @pkString, @StartIndex) >> END >> >> -- This either gets the last pk in @pkString or the single >> -- customerID in @pkString >> SET @WarehouseID = >> LTRIM(SUBSTRING(@Warehouses, @StartIndex, @WarehousesLength - @StartIndex + 1)) >> INSERT #WarehouseIDs VALUES (@WarehouseID) >> >> SELECT add.street, add.city, add.state >> FROM Address add >> INNER JOIN #customerIDTable cidt >> ON add.customerID = cidt.customerID >> >> SELECT ord.orderID, ord.amount >> FROM Orders ord >> INNER JOIN #customerIDTable cidt >> ON ord.customerID = cidt.customerID >> >> -- Additional SELECTs... >> >> DROP TABLE #customerIDTable>>Is there a more efficient way? I doubt that the second option would pay off unless I was doing quite a few SELECTs. All the tables that I am JOINing them with are pretty big.