使用 SQL CASE 语句动态更改要选择的表
我正在尝试编写一个存储过程,并且根据某个列值,我希望能够更改我从中选择的表。 我将尝试举一个例子:
SELECT ItemNumber,
ItemType,
Description
FROM
CASE ItemType
WHEN 'A' THEN TableA
ELSE TableB
END
WHERE
CASE ItemType
WHEN 'A' THEN ItemNumber = @itemNumber
ELSE PartNumber = @itemNumber
END
正如您所看到的,我不仅动态更改我选择的表,而且由于这两个表是由两个不同的人在两个不同的时间创建的,因此列名也不同。
所以,我的问题是:完成此操作的最佳方法是什么,因为 SQL Server 似乎不喜欢我构建的查询。
如果任何看到我正在尝试做的事情的人可以提出更好的方法来做到这一点,我会洗耳恭听:-)
I'm trying to write a stored procedure and depending on a certain column value, I want to be able to change what table I select from. I'll try to give an example:
SELECT ItemNumber,
ItemType,
Description
FROM
CASE ItemType
WHEN 'A' THEN TableA
ELSE TableB
END
WHERE
CASE ItemType
WHEN 'A' THEN ItemNumber = @itemNumber
ELSE PartNumber = @itemNumber
END
As you can see, not only am I dynamically changing the table I select from, but since these two tables were made at two different times by two different people, the column names are different as well.
So, my question is: What is the best way to accomplish this, since SQL Server doesn't seem to like my query I have constructed.
If anyone who sees what I'm trying to do can suggest a better way to do this, I'd be all ears :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
FROM 子句中不能使用 CASE 语句,但可以使用以下语句:
You can not use CASE statement in FROM clause, but you can use the following instead:
您可以尝试将动态 SQL 语句构建为字符串,然后调用 sp_executesql 存储过程来执行该字符串。
有关详细信息和示例,请参阅此处。
You could try building the dynamic SQL statement as a string, and then calling the sp_executesql stored procedure to execute the string.
See here for more information and examples.
我不知道为什么你想在一个 SQL 语句中做一些事情..我不是一个 SQL Server 人员,但是在 Oracle 存储过程中你可以写这样的东西像
这样的东西也应该在 SQL Server 中工作..也许有人可以对此进行扩展?
I'm not sure why you want to do things in one SQL Statement .. I'm not a SQL Server person, but in an Oracle stored procedure you could write something like this
Something like this should work in SQL Server, too .. maybe someone could expand on this?
您确实没有解释
ItemType
来自哪里。 如果您只是组合两个表,建议UNION
可能适用。这是可能与您的问题相关的另一种可能性:
You really aren't explaining where
ItemType
is coming from. As suggestedUNION
might be applicable if you are simply combining two tables.Here's another possibility which may relate to your problem:
最好先使用 UNION 查询来连接表,然后再进行 SELECT。
另外,您可以考虑为其中一个表创建一个视图,这样它在重命名它们时仅提取您需要的列,然后使用 UNION,然后从 UNION 中进行选择。
或者使用临时表来存储每个查询的结果。 将临时表的创建放在CASE中(伪代码,未测试):
You are better of using UNION query to join the tables first, and then SELECT.
Also, you may consider creating a view for one of the tables, so it pulls only the columns you need while renaming them, then UNION, and then select from the UNION.
Or use a temp table to store the result from each query. Put the creation of the temp table in a CASE (pseudocode, not tested):
它可以是动态查询,或者您可以继续使用以下方法:
It could be a dynamic query or you can proceed with the below approach:
你可以在 SQL Server 中这样做
You can do it like this in SQL Server
您可以使用变量来做到这一点。 例如;
You can do it with a variable. For example;