Solved

microsoft, sql server, databases, case in trigger

Posted on 2008-06-11
3
185 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 40
Binding error when running a view SQL Server 3 27
SQL SERVER - Index skipped a number 2 26
SQL R 21 26
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

856 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