Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

microsoft, sql server, databases, case in trigger

Posted on 2008-06-11
3
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn 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:jcoehoorn
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

721 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