我有一个像这样的表结构:
FIELD | VALUE
--------------+----------
COLUMN_NAME_A | VALUE_1
COLUMN_NAME_B | VALUE_2
FIELD
列的值是另一个表中列的名称。我需要以某种方式从该表中选择存在于另一个表中列出的列中的值,并且我不能只是对其进行硬编码,因为它可能随时更改而不另行通知。
有没有办法根据单独表格中列的值选择列?
所以我基本上需要像
这样的东西SELECT *
FROM other_table
WHERE COLUMN_NAME_A = VALUE_1
答案 0 :(得分:0)
create procedure Proc_name as
DECLARE @myCursor cursor;
DECLARE @sqlSelect char(400);
DECLARE @value char(100);
BEGIN
SET @myCursor = CURSOR FOR
SELECT field from source_table
OPEN @myCursor
FETCH NEXT FROM @myCursor
INTO @value
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlSelect = ' select * from Other_table where
COLUMN_NAME_A =' + @value
EXEC (@sqlSelect)
FETCH NEXT FROM @myCursor INTO @value
END
CLOSE @myCursor
DEALLOCATE @myCursor
END