Solved

microsoft, sql server, databases, case in trigger

Posted on 2008-06-11
3
182 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:
jcoehoorn earned 500 total points
Comment Utility
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
Comment Utility
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:jcoehoorn
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now