0

I am trying to insert into a temporary table from a select with a case statement defining which table to insert from but I cannot get the syntax right.

I would like to do something like the following:-

DECLARE @TempTable TABLE(ID int IDENTITY, Charges varchar(270));
INSERT INTO @TempTable SELECT (
  CASE @inItemType
    WHEN 1 THEN SELECT ID, Charges FROM TicketType WHERE Charges <> ''
    WHEN 10 THEN SELECT ID, Charges FROM SeatType WHERE Charges <> ''
  END
);

Does anyone even know if this is possible or can suggest a better way?

1 Answer 1

1

I would just do:

DECLARE @TempTable TABLE(ID int IDENTITY, Charges varchar(270));
INSERT INTO @TempTable
SELECT ID, Charges FROM TicketType WHERE Charges <> '' and @inItemType=1
UNION ALL
SELECT ID, Charges FROM SeatType WHERE Charges <> '' and @inItemType=10;

A CASE expression, in common with most expressions in most mainstream languages, computes a single (scalar) value. It cannot return tuples or (as in this case) entire rowsets.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.