Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Inserting using Case Statement

Author  Topic 

Ask SQLTeam Question

0 Posts

Posted - 2002-04-16 : 10:04:58
Manish Garg writes "i want to insert a record to a different table depending on the variable passed as argument using case statement."

Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-04-16 : 11:17:42
Not possible (sort-of).

An INSERT statement can only insert rows into one table or view. A case statement does evaluate a list of conditions and return one of multiple possible results expressions, however, it is not allowed as part of the table_name arguement.

You need to either use IF...ELSE to test your variable for a certain condition and then use the flow control to execute the proper insert statement ... or ...

(Here is the sort-of part). If you are on SQL 2k, you can create an an instead of trigger on a view or table to route the insert to the correct base tables. In SQL 7, and updatable view can modify more than one table involved in the view, as long as SQL Server can translate the update request unambiguously to update in the base tables referenced in the view definition. I've never done that in SQL 7 for an insert and I'm not sure its possible...


Edited by - Page47 on 04/16/2002 11:20:17
Go to Top of Page

- Advertisement -