0

是否可以通过将参数保留在 where 子句中来获取值,如下所示:

DECLARE  @COLUMNNAME VARCHAR(50)
-- setting required column name 
SET @COLUMNNAME = (select name from sys.columns where OBJECT_ID = 
    (SELECT OBJECT_ID FROM SYS.objects WHERE name = 'BORVALIDATION') AND column_id = 2) 
--PRINT @COLUMNNAME (we do get the column name)
SELECT * FROM BORVALIDATION WHERE @COLUMNNAME = 'BBNAME'

非常感谢您的知识和帮助。

4

1 回答 1

3

您可以使用动态 SQL:

Declare @SQL VARCHAR(4000)
SET @SQL = 'SELECT * FROM BORVALIDATION WHERE ' + @COLUMNNAME + '= ''BBNAME''';
EXEC(@SQL);
于 2013-09-26T13:22:21.080 回答