Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

microsoft, sql server, databases, case in trigger

Posted on 2008-06-11
3
Medium Priority
?
190 Views
Last Modified: 2008-06-11
folks

i have a trigger that pics up the value of the id field in one table and then has to create records into another table based on this,
note the value of the id field in table a determines different inserts into table b i.e.
if the value starts with w-1 then it needs to insert 2 records into table b

i.e  
insert into b (id,number,type)(select id,@number ,'hours' from inserted)
insert into b (id,number,type)(select id,@number,'meter' from inserted)

if the value starts with b-1 then it needs to insert 2 records into table b

insert into b (id,number,type)(select id,@number ,'moves' from inserted)
insert into b (id,number,type)(select id,@number,'miles' from inserted)

else if the id doesnt start with these values then it isnt required to perform any inserts into table b

note the @number is based on the following:

DECLARE @number int
SELECT @number =MAX(number)+1 FROM table b

how do i build a trigger to achieve this?
0
Comment
Question by:rutgermons
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Joel Coehoorn earned 1000 total points
ID: 21759927
select id,@number ,CASE WHEN id LIKE 'w-1%' THEN 'hours' ELSE WHEN id LIKE 'b-1%' THEN 'moves' END from inserted
0
 

Author Comment

by:rutgermons
ID: 21759967
jcoe

thats wrong ,i need multiple inserts ie if my id like w-1 then 2 inserts need to be made

insert into b (id,number,type)(select id,@number ,'hours' from inserted)
insert into b (id,number,type)(select id,@number,'meter' from inserted)

if the value starts with b-1 then it needs to insert 2 records into table b

insert into b (id,number,type)(select id,@number ,'moves' from inserted)
insert into b (id,number,type)(select id,@number,'miles' from inserted)

all help will do
0
 
LVL 18

Expert Comment

by:Joel Coehoorn
ID: 21760191
That was just an example.  I expected you would use it as a template to build another set for next query.  Of course, it does assume that you've presented the entire problem, or it not at least that there are the same number of inserts for each id case.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question