0

如果我的方案有捷径,有人可以帮助我。

SELECT ProductName FROM Product where ProductId = 1012

此语句的结果是“DoubleDutch”

SELECT ColumnName FROM ProductMapping

此语句的结果是“ProductName”

SELECT (SELECT PM.ColumName FROM Product where ProductId = 1012) FROM ProductMapping PM

现在我只是期待结果是“DoubleDutch”,但结果仍然是“ProductName”

4

2 回答 2

0

尝试这个:

DECLARE @Col VARCHAR(1000), @sql NVARCHAR(max)
SELECT @Col = ValueFromTableB from TableA
SELECT @sql = 'SELECT ' + @Col + ' FROM TableB
EXEC sp_executesql @sql
于 2013-09-16T09:16:58.877 回答
0

我认为这应该对你有用

DECLARE @Col VARCHAR(1000), @sql NVARCHAR(max)
Set @Col  = ''
SELECT @Col = @Col + ValueFromTableB + ' , ' from TableB
Set @Col = Left(@Col , (LEN(@Col) - 1))
SELECT @sql = 'SELECT TableAId , ' + @Col + ' FROM TableA'
EXEC sp_executesql @sql
于 2013-09-16T09:52:44.520 回答