使用 SQL CASE 语句动态更改要选择的表

发布于 2024-07-13 02:35:29 字数 472 浏览 3 评论 0原文

我正在尝试编写一个存储过程,并且根据某个列值,我希望能够更改我从中选择的表。 我将尝试举一个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

内心荒芜 2024-07-20 02:35:29

FROM 子句中不能使用 CASE 语句,但可以使用以下语句:

SELECT itemnumber, itemtype, description
  FROM tablea
 WHERE itemnumber = @itemnumber AND itemtype = 'A'
UNION ALL
SELECT itemnumber, itemtype, description
  FROM tableb
 WHERE partnumber = @itemnumber AND itemtype <> 'A'

You can not use CASE statement in FROM clause, but you can use the following instead:

SELECT itemnumber, itemtype, description
  FROM tablea
 WHERE itemnumber = @itemnumber AND itemtype = 'A'
UNION ALL
SELECT itemnumber, itemtype, description
  FROM tableb
 WHERE partnumber = @itemnumber AND itemtype <> 'A'
孤独岁月 2024-07-20 02:35:29

您可以尝试将动态 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.

冬天旳寂寞 2024-07-20 02:35:29

我不知道为什么你想在一个 SQL 语句中做一些事情..我不是一个 SQL Server 人员,但是在 Oracle 存储过程中你可以写这样的东西像

If itemtype = 'A' 
Then 
 <statement for table A>
Else
 <statement for Table B>
End if

这样的东西也应该在 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

If itemtype = 'A' 
Then 
 <statement for table A>
Else
 <statement for Table B>
End if

Something like this should work in SQL Server, too .. maybe someone could expand on this?

浮世清欢 2024-07-20 02:35:29

您确实没有解释 ItemType 来自哪里。 如果您只是组合两个表,建议 UNION 可能适用。

这是可能与您的问题相关的另一种可能性:

SELECT ItemNumber,
       ItemType, 
       COALESCE(TableA.Description, TableB.Description) AS Description
FROM Items
LEFT JOIN TableA
    ON Items.ItemType = 'A'
    AND TableA.ItemNumber = Items.ItemNumber
LEFT JOIN TableB
    ON Items.ItemType <> 'A'
    AND TableB.ItemNumber = Items.ItemNumber

You really aren't explaining where ItemType is coming from. As suggested UNION might be applicable if you are simply combining two tables.

Here's another possibility which may relate to your problem:

SELECT ItemNumber,
       ItemType, 
       COALESCE(TableA.Description, TableB.Description) AS Description
FROM Items
LEFT JOIN TableA
    ON Items.ItemType = 'A'
    AND TableA.ItemNumber = Items.ItemNumber
LEFT JOIN TableB
    ON Items.ItemType <> 'A'
    AND TableB.ItemNumber = Items.ItemNumber
枯叶蝶 2024-07-20 02:35:29

最好先使用 UNION 查询来连接表,然后再进行 SELECT。

另外,您可以考虑为其中一个表创建一个视图,这样它在重命名它们时仅提取您需要的列,然后使用 UNION,然后从 UNION 中进行选择。

或者使用临时表来存储每个查询的结果。 将临时表的创建放在CASE中(伪代码,未测试):

CASE @itemType
   WHEN 'A'
      SELECT ACol1 AS Col1, ACol2 AS Col2
      FROM TABLE_A
      INTO #tempTable
      WHERE ItemNumber = @itemNumber
   ELSE
      SELECT BCol1 AS Col1, BCol2 AS Col2
      FROM TABLE_B
      INTO #tempTable
      WHERE PartNumber = @itemNumber
END

SELECT * FROM #tempTable

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):

CASE @itemType
   WHEN 'A'
      SELECT ACol1 AS Col1, ACol2 AS Col2
      FROM TABLE_A
      INTO #tempTable
      WHERE ItemNumber = @itemNumber
   ELSE
      SELECT BCol1 AS Col1, BCol2 AS Col2
      FROM TABLE_B
      INTO #tempTable
      WHERE PartNumber = @itemNumber
END

SELECT * FROM #tempTable
夏了南城 2024-07-20 02:35:29

它可以是动态查询,或者您可以继续使用以下方法:

SELECT 
  CASE ItemType
    WHEN 'A' THEN (Select ItemNumber from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select ItemNumber from TableB Where ItemNumber = @itemNumber)
  End as ItemNumber,
  CASE ItemType
    WHEN 'A' THEN (Select ItemType from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select ItemType from TableB Where ItemNumber = @itemNumber)
  End as ItemType,
  CASE ItemType
    WHEN 'A' THEN (Select Description from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select Description from TableB Where ItemNumber = @itemNumber)
  End as Description 

It could be a dynamic query or you can proceed with the below approach:

SELECT 
  CASE ItemType
    WHEN 'A' THEN (Select ItemNumber from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select ItemNumber from TableB Where ItemNumber = @itemNumber)
  End as ItemNumber,
  CASE ItemType
    WHEN 'A' THEN (Select ItemType from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select ItemType from TableB Where ItemNumber = @itemNumber)
  End as ItemType,
  CASE ItemType
    WHEN 'A' THEN (Select Description from TableA Where ItemNumber = @itemNumber)
    When 'B' THEN (Select Description from TableB Where ItemNumber = @itemNumber)
  End as Description 
蓝咒 2024-07-20 02:35:29

你可以在 SQL Server 中这样做

DECLARE @t AS VARCHAR(1) = 'B';

BEGIN

IF @t = 'A' 
SELECT * FROM TableA
    
IF @t = 'B' 
SELECT * FROM TableB

END

You can do it like this in SQL Server

DECLARE @t AS VARCHAR(1) = 'B';

BEGIN

IF @t = 'A' 
SELECT * FROM TableA
    
IF @t = 'B' 
SELECT * FROM TableB

END
留蓝 2024-07-20 02:35:29

您可以使用变量来做到这一点。 例如;

DECLARE @YEAR AS INT
SET @YEAR = 2021

DECLARE @TABLENAME AS NVARCHAR(100)

IF @YEAR < 2021
SET @TABLENAME = 'TABLE A'
ELSE 
SET @TABLENAME = 'TABLE B'

EXEC('SELECT * FROM '+@TABLENAME+'')

You can do it with a variable. For example;

DECLARE @YEAR AS INT
SET @YEAR = 2021

DECLARE @TABLENAME AS NVARCHAR(100)

IF @YEAR < 2021
SET @TABLENAME = 'TABLE A'
ELSE 
SET @TABLENAME = 'TABLE B'

EXEC('SELECT * FROM '+@TABLENAME+'')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文