SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using Case within an Insert Statement


Using Case within an Insert Statement

Author
Message
kevin_fralick
kevin_fralick
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 15
I have an Insert statement. One of the columns to be added will be either 'Y' or 'N' depending on the value of another queried column.

Pseudocode would be something like this, where I need to insert into field 'valid' based on the value of description.

Insert into table
values (@ssn,@empid,@race,
CASE @description
WHEN 'deputy' THEN valid = 'Y'
WHEN 'civilian' THEN valid ='N'
end
)

Thanks.
dkschill
dkschill
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1057 Visits: 430
You should be able to write it with the case statement as a field. So it would look like this:

insert into table
values (@ssn,@empid,@race,
CASE @description
WHEN 'deputy' THEN 'Y'
WHEN 'civilian' THEN 'N'
end
)

The case itself is the field; so you do not have to set a value to return.

I hope this answers your question.
kevin_fralick
kevin_fralick
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 15
The name of the field to be updated is different than the field which contains the determining value.

I need to update field 'valid' based on the value in field 'description'.
dkschill
dkschill
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1057 Visits: 430
If you need to include description and the valid column you would just need to add it into values list:

insert into table (ssn,empid,race,description,valid)
values (
@ssn,
@empid,
@race,
@description,
CASE @description
WHEN 'deputy' THEN 'Y'
WHEN 'civilian' THEN 'N'
end
)

It shouldn't hurt anything to do that. Valid is a column in the same table right?

Hope to help,
Dane
kevin_fralick
kevin_fralick
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 15
If I have values after the case statement what would it look like?

I'm not sure of the syntax. Also I don't have the columns hardcoded on the insert line because I'm putting data into all of them.

insert into table
values (
@ssn,
@empid,
@race,
@description,
CASE @description
WHEN 'deputy' THEN 'Y'
WHEN 'civilian' THEN 'N'
end,
column1,
column2,

)
dkschill
dkschill
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1057 Visits: 430
How you typed it seems correct. I am assuming that the trailing comma is a typo. There are hidden dangers with not specifying the columns you are inserting. If the table ever changes your insert will definitely fail unless it is also touched.

Hope this helps,
Dane
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search