Click here to Skip to main content
15,895,084 members
Home / Discussions / Database
   

Database

 
AnswerRe: SQL Performance Question Pin
Colin Angus Mackay6-Oct-05 9:11
Colin Angus Mackay6-Oct-05 9:11 
GeneralRe: SQL Performance Question Pin
jgallen236-Oct-05 9:17
jgallen236-Oct-05 9:17 
QuestionExecuting SQL Script File Pin
KragAapie5-Oct-05 19:32
KragAapie5-Oct-05 19:32 
AnswerRe: Executing SQL Script File Pin
Colin Angus Mackay5-Oct-05 21:59
Colin Angus Mackay5-Oct-05 21:59 
QuestionHow to call stored procedure from update statement Pin
iluha5-Oct-05 15:54
iluha5-Oct-05 15:54 
AnswerRe: How to call stored procedure from update statement (edit) Pin
Nic Rowan6-Oct-05 1:55
Nic Rowan6-Oct-05 1:55 
QuestionImport/Export in MS Access Pin
suzie1005-Oct-05 8:38
suzie1005-Oct-05 8:38 
QuestionHelp with Cursors in Stored Procedures Pin
jszpila5-Oct-05 6:33
jszpila5-Oct-05 6:33 
Hello everyone,
I've just written my first stored procedure and maybe gotten in a little over my head. It's not exactly working out the way I thought; essentially, what I want to do is select a few fields from one table set of rows that meets the SELECT criteria and also where the identifying number is not already in the second table, and then perform a few operations on some of the fields and subsequently insert everything into the second table. I've been told a cursor would assist in this, but I have yet to find a good and clear tutorial/example on T-SQL cursors. Just so you can get a better idea of what I'm talking about, here is my stored procedure thus far:
<br />
CREATE PROCEDURE sp_UpdateLeaders<br />
AS<br />
DECLARE<br />
-- prep all variables<br />
@YspCalc money, <br />
@FeeCalc money, <br />
@Fees money, <br />
@Amount money, <br />
@Ysp money,<br />
@Fee1 money, <br />
@Fee2 money, <br />
@Fee3 money, <br />
@Fee4 money, <br />
@Fee5 money, <br />
@Fee6 money,<br />
@Number varchar(50), <br />
@McName varchar(50), <br />
@Client varchar(50), <br />
@Officer varchar(50),<br />
@FName varchar(25), <br />
@LName varchar(25),<br />
@Date datetime,<br />
@Points integer, <br />
@LoanType integer, <br />
@Mortgage integer<br />
<br />
-- get info from bors<br />
SELECT <br />
TLH_1_68=@FName, TLH_1_69 = @LName, TLH_1_19 = @LoanType, <br />
TLH_1_748 = @Date, TLH_1_2 = @Amount, TLH_2_58 = @McName, <br />
TLH_1_420 = @Mortgage, TLH_1_869 = @Ysp, TLH_1_2 = @Amount,  <br />
GRICA_1_2 = @Fee1, GRICA_1_4 = @Fee2, GRICA_1_6 = @Fee3,  <br />
GRICA_1_10 = @Fee4, GRICA_1_12 = @Fee5, GRICA_1_8 = @Fee6,<br />
TLH_1_317 = @Officer, TLH_1_876 = @Number<br />
FROM MakeDaily53.DBO.bors<br />
WHERE TLH_1_749 Is NULL <br />
AND Filename NOT LIKE '%.2'<br />
-- only for the following branch codes<br />
AND TLH_1_1007 IN ('645','644','642','638','636','599','598','592','589','587','586','582','581','580','579','565','112')<br />
<br />
Print('Before')<br />
<br />
-- check for existing loan in leaderboard<br />
IF NOT EXISTS(SELECT * FROM LeaderBoard WHERE LoanNumber = @Number)<br />
BEGIN<br />
<br />
Print(@Number)<br />
<br />
-- check for nulls<br />
IF @Fee1 = Null<br />
SET @Fee1 = 0<br />
IF @Fee2 = Null<br />
SET @Fee2 = 0<br />
IF @Fee3 = Null<br />
SET @Fee3 = 0<br />
IF @Fee4 = Null<br />
SET @Fee4 = 0<br />
IF @Fee5 = Null<br />
SET @Fee5 = 0<br />
IF @Fee6 = Null<br />
SET @Fee6 = 0<br />
<br />
-- concatenate client name<br />
SET @Client = @FName + ' ' + @LName<br />
-- ysp+rsp caluclation<br />
SET @YspCalc = (((@Ysp - 100)/100)*@Amount)<br />
-- additional fees are calulated differently for 1st and 2nd mortgages<br />
IF (@Mortgage = 1)<br />
SET @FeeCalc = ((@Fee1 + @Fee2 + @Fee3 + @Fee4 + @Fee5 + @Fee6)-1425)<br />
IF (@Mortgage = 2)<br />
SET @FeeCalc = ((@Fee1 + @Fee2 + @Fee3 + @Fee4 + @Fee5 + @Fee6)-275)<br />
<br />
-- insert into leaderboard<br />
INSERT INTO LeaderBoard (ClientName, LoanType, LoanNumber, ClosedDate, LoanAmount, YspSrp, Points, AddFees, McName, MortgageType, LoanOfficer)<br />
VALUES (@Client, @LoanType, @Number, @Date, @Amount, @YspCalc, @Points, @FeeCalc, @McName, @Mortgage, @Officer)<br />
<br />
END<br />
<br />
-- go!<br />
GO


Any help is greatly appreciated. Thanks in advance!

-------------------
abort, retry, fail?
AnswerRe: Help with Cursors in Stored Procedures [long reply] Pin
Colin Angus Mackay5-Oct-05 11:12
Colin Angus Mackay5-Oct-05 11:12 
AnswerRe: Help with Cursors in Stored Procedures [long reply] Pin
jszpila5-Oct-05 11:54
jszpila5-Oct-05 11:54 
GeneralRe: Help with Cursors in Stored Procedures [long reply] Pin
Colin Angus Mackay5-Oct-05 12:16
Colin Angus Mackay5-Oct-05 12:16 
QuestionLooking for the right forum Pin
Michael Hulthin4-Oct-05 21:42
Michael Hulthin4-Oct-05 21:42 
QuestionHow to launch access database and view reports Pin
dw19284-Oct-05 20:36
dw19284-Oct-05 20:36 
Questionadd stored procedures .sql to database Pin
theStorminMormon4-Oct-05 10:32
theStorminMormon4-Oct-05 10:32 
AnswerRe: add stored procedures .sql to database Pin
Colin Angus Mackay4-Oct-05 12:44
Colin Angus Mackay4-Oct-05 12:44 
GeneralRe: add stored procedures .sql to database Pin
theStorminMormon5-Oct-05 2:57
theStorminMormon5-Oct-05 2:57 
GeneralRe: add stored procedures .sql to database Pin
Scott Serl6-Oct-05 11:14
Scott Serl6-Oct-05 11:14 
AnswerRe: add stored procedures .sql to database Pin
Luis Alonso Ramos6-Oct-05 18:45
Luis Alonso Ramos6-Oct-05 18:45 
QuestionUsing SqlConnection without Time out Pin
thainam3-Oct-05 23:13
thainam3-Oct-05 23:13 
AnswerRe: Using SqlConnection without Time out Pin
Colin Angus Mackay4-Oct-05 1:19
Colin Angus Mackay4-Oct-05 1:19 
QuestionSQL SERVER Pin
Tran Ngoc Minh3-Oct-05 18:15
Tran Ngoc Minh3-Oct-05 18:15 
AnswerRe: SQL SERVER Pin
Colin Angus Mackay4-Oct-05 1:25
Colin Angus Mackay4-Oct-05 1:25 
QuestionStore procedure access in c++ Pin
Anonymous3-Oct-05 17:14
Anonymous3-Oct-05 17:14 
QuestionMultiple Selects in SP Pin
Russell Jones3-Oct-05 6:05
Russell Jones3-Oct-05 6:05 
AnswerRe: Multiple Selects in SP Pin
afronaut3-Oct-05 6:45
afronaut3-Oct-05 6:45 

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.