Click here to Skip to main content
15,912,457 members
Home / Discussions / Database
   

Database

 
GeneralRe: Deploying SQL Application with C#.net 2003 Pin
Paddy Boyd2-Jun-06 6:08
Paddy Boyd2-Jun-06 6:08 
GeneralRe: Deploying SQL Application with C#.net 2003 Pin
Felipe Dalorzo2-Jun-06 6:46
Felipe Dalorzo2-Jun-06 6:46 
QuestionSQL version [modified] Pin
ass3aad1-Jun-06 17:41
ass3aad1-Jun-06 17:41 
AnswerRe: SQL version [modified] Pin
Dinuj Nath1-Jun-06 22:17
Dinuj Nath1-Jun-06 22:17 
QuestionEnterprise Library and Dataset Update Pin
ShineLee1-Jun-06 16:33
ShineLee1-Jun-06 16:33 
AnswerRe: Enterprise Library and Dataset Update Pin
Stephan Samuel3-Jun-06 4:27
Stephan Samuel3-Jun-06 4:27 
QuestionTrigger Help Needed Pin
AnneThorne1-Jun-06 6:39
AnneThorne1-Jun-06 6:39 
AnswerRe: Trigger Help Needed Pin
Stephan Samuel1-Jun-06 7:34
Stephan Samuel1-Jun-06 7:34 
Anne,

(I'm assuming SQL Server 2000.)

It looks like you're trying to insert and expand in the trigger. The body of the trigger has access to a table called "inserted" that contains the inserted values. Inserting is easy, but running your expansion is more difficult and poor form in a set-based language like SQL.

This does what you want:

<br />
create trigger mytrigger on tableone for insert as<br />
begin -- trigger<br />
<br />
declare<br />
@ptr int, -- your loop variable<br />
@code char -- what to insert each time<br />
<br />
-- probably can combine the next two lines if clever<br />
select @code = code from inserted<br />
select @ptr = numstart from inserted<br />
<br />
while @ptr <= (select numend from inserted)<br />
begin -- while<br />
<br />
-- consider a select .. into instead of next<br />
insert into tabletwo (num, code) values (@ptr, @code)<br />
select @ptr = @ptr + 1<br />
<br />
end -- while<br />
<br />
end -- trigger<br />


The motherhood-and-apple-pie stand on this, all else being equal, is that you should use neither a trigger nor the second table for this. It says that you should always avoid triggers unless there's no other solution (there is here), and you shouldn't store your data twice (table two simply repeats information you can find in table one). Instead, you should just store the start, end, and code, and craft a query that will give you a result set containing (1, A), (2, A), (3, A) that runs every time you want that result. I'm assuming you have a good business case for this though, like a performance enhancement or something.


Stephan
QuestionSQLDMO Application Deployment Issue Pin
ThanosM1-Jun-06 5:27
ThanosM1-Jun-06 5:27 
AnswerRe: SQLDMO Application Deployment Issue Pin
Paul Brower2-Jun-06 9:27
Paul Brower2-Jun-06 9:27 
GeneralRe: SQLDMO Application Deployment Issue Pin
ThanosM2-Jun-06 13:06
ThanosM2-Jun-06 13:06 
QuestionHow to show a progressbar while loading Datatable ? Pin
Tommi0071-Jun-06 0:39
Tommi0071-Jun-06 0:39 
AnswerRe: How to show a progressbar while loading Datatable ? Pin
SeMartens1-Jun-06 5:18
SeMartens1-Jun-06 5:18 
GeneralRe: How to show a progressbar while loading Datatable ? Pin
Tommi0071-Jun-06 20:50
Tommi0071-Jun-06 20:50 
QuestionSQLXML 3.0 Pin
punam3031-May-06 19:50
punam3031-May-06 19:50 
Questionhow can i index the column ?? Pin
Mohammed Amine31-May-06 12:58
Mohammed Amine31-May-06 12:58 
AnswerRe: how can i index the column ?? Pin
Colin Angus Mackay31-May-06 23:38
Colin Angus Mackay31-May-06 23:38 
Questionattach a database with code Pin
Mohammed Amine31-May-06 12:55
Mohammed Amine31-May-06 12:55 
AnswerRe: attach a database with code [modified] Pin
albCode31-May-06 23:32
albCode31-May-06 23:32 
AnswerRe: attach a database with code Pin
Eric Dahlvang1-Jun-06 2:56
Eric Dahlvang1-Jun-06 2:56 
GeneralRe: attach a database with code Pin
Mohammed Amine1-Jun-06 5:39
Mohammed Amine1-Jun-06 5:39 
GeneralRe: attach a database with code Pin
Eric Dahlvang1-Jun-06 7:07
Eric Dahlvang1-Jun-06 7:07 
GeneralRe: attach a database with code Pin
Mohammed Amine1-Jun-06 7:23
Mohammed Amine1-Jun-06 7:23 
AnswerRe: attach a database with code [modified] Pin
Eric Dahlvang1-Jun-06 7:55
Eric Dahlvang1-Jun-06 7:55 
GeneralRe: attach a database with code [modified] Pin
Mohammed Amine1-Jun-06 10:28
Mohammed Amine1-Jun-06 10:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.