Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can use COALESCE in Insert?
Message
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Titre:
Can use COALESCE in Insert?
Versions des environnements
SQL Server:
SQL Server 2014
Application:
Web
Divers
Thread ID:
01617904
Message ID:
01617904
Vues:
27
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
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform