|
Posted by Hugo Kornelis on 01/21/06 23:47
On 20 Jan 2006 11:49:20 -0800, cavassinif@gmail.com wrote:
>
>I need to dynamic select a column in which insert a vale based on a
>parameter value, I have this code, but it throws an incorrect syntax
>error.
>
>How do I dinamically select a column to insert based on a parameter?
>
>Create PROCEDURE dbo.UpdateDetalleOT (
>@eotId int,
>)
>
>insert into OT (
> select Case
> when @eotId = 1 THEN OTFechaBorrador
> when @eotId = 2 THEN OTFechaAAsignar
> end
>) values ....
>
>
>Best Regards
>Fabio Cavassini
>http://www.pldsa.com
Hi Fabio,
You can't insert into just one column - you insert a complete row, and
you'll have to give values (either real values or NULL) for all columns.
Sure, the language permits you to leave out some columns, but that's
just a shorthand way for specifying that you want to insert the defined
DEFAULT value (if any) or NULL in all the other columns.
To do what you appear to want (and I *really* hope that this is an
extremely simplified illustration of the real problem, because if this
is your real procedure, you have much, much bigger problems), you can
either use the code posted by MGFoster, or use
INSERT INTO OT (OTFechaBorrador, OTFechaAAsignar)
SELECT CASE WHEN @eotId = 1 THEN .... ELSE NULL END),
CASE WHEN @eotId = 2 THEN .... ELSE NULL END)
--
Hugo Kornelis, SQL Server MVP
Navigation:
[Reply to this message]
|