SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Do Loop in T-SQL


Do Loop in T-SQL

Author
Message
trudye10
trudye10
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 135
I have files coming from several clients, the trailers have to validated against the detail record count. Each client may send as many files as needed. How can I check each trailers record count? I guess my question is what is the equivalent of a “Do Until” in T-SQL?

Also I am getting:
The multi-part identifier "TEX.Enroll_Trlr.Dtl_Count" could not be bound.


DECLARE @Run_Date as int
DECLARE @DTL_CNT as int

SET @Run_Date = Convert(Varchar(10),Getdate(),112)
SET @DTL_CNT = (SELECT count(*)
FROM TEX.Enroll_Dtl, TEX.Enroll_Trlr
WHERE TEX.Enroll_Dtl.RunDate = @Run_Date And TEX.Enroll_Trlr.Vendor_Name = 'CFS'
And TEX.Enroll_Dtl.Vendor_File_Name = TEX.Enroll_Trlr.Vendor_File_Name)

IF @DTL_CNT <> Enroll_Trlr.Dtl_Count
BEGIN
print 'goto QUIT'
Exec TEX.sp_Enrollment_Error_handler
END

SET @DTL_CNT = (SELECT count(*)
FROM TEX.Enroll_Dtl, TEX.Enroll_Trlr
WHERE TEX.Enroll_Dtl.RunDate = @Run_Date And TEX.Enroll_Trlr.Vendor_Name = 'GNA'
And TEX.Enroll_Dtl.Vendor_File_Name = TEX.Enroll_Trlr.Vendor_File_Name)

IF @DTL_CNT <> TEX.Enroll_Trlr.Dtl_Count
BEGIN
print 'goto QUIT'
Exec TEX.sp_Enrollment_Error_handler
END

SET @DTL_CNT = (SELECT count(*)
FROM TEX.Enroll_Dtl, TEX.Enroll_Trlr
WHERE TEX.Enroll_Dtl.RunDate = @Run_Date And TEX.Enroll_Trlr.Vendor_Name = 'SYN'
And TEX.Enroll_Dtl.Vendor_File_Name = TEX.Enroll_Trlr.Vendor_File_Name)

IF @DTL_CNT <> TEX.Enroll_Dtl.Dtl_Count
BEGIN
print 'goto QUIT'
Exec TEX.sp_Enrollment_Error_handler
END


END
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12855 Visits: 18584
I don't recommend this approach, but you can structure a WHILE loop to act as a DO...UNTIL loop.


WHILE (1 = 1)
BEGIN
--do stuff
.....
-- do more stuff

if (break_condition = true)
BREAK
END



That said - from what you're describing you'd be better off bulk loading to some staging table, and doing your validation there before moving it to the permanent home.....

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6340 Visits: 4551
Another alternative would be to use SSIS to import and validate your files prior to loading them in your database.

John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
trudye10
trudye10
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 135
Thanx so much Guys
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search