Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can use COALESCE in Insert?
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Versions des environnements
SQL Server:
SQL Server 2014
Application:
Web
Divers
Thread ID:
01617904
Message ID:
01617906
Vues:
19
>Hi,
>
>Simplified case:
>In a stored procedure I declare a variable then the variable either gets a value or not (the value is actually retrieved from pass XML file).
>Then I need to Insert a record and set the value to this column if the value exists, or do not include this column in the Insert (if the value is NULL).
>Example that generates error:
>
>DECLARE @Field1Value AS CHAR(10)
>INSERT INTO MyTable( Field1) values (COALESCE(@Field1Value, Field1))
>
>
>So far, the only solution I see is to Insert the record field, get the PK (right after insert using
>select @PkValue = SCOPE_IDENTITY() ). And then UPDATE the record where COALESCE() can be used.
>
>Can anybody see/suggest an approach of doing it all in one Insert (instead of Insert and Update)?
>
>TIA

During the INSERT the Field1 is NULL because you insert new record.
You can insert empty value:
INSERT INTO MyTable( Field1) values (COALESCE(@Field1Value, ''))
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform