SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 While insert loop HELP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mike13
Posting Yak Master

Netherlands
214 Posts

Posted - 06/26/2013 :  11:18:45  Show Profile  Click to see mike13's MSN Messenger address  Reply with Quote
Hi All,

I got this SP, but this keep in a loop.
but of course this is a continuous loop
i just want it to loop thru 1 time thru all the customers.

CREATE PROCEDURE [dbo].[SP_BACK_mail_Send_Massmail]

@subject as nvarchar(50),
@body as nvarchar(max),
@shopid as nvarchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Begin
WHILE EXISTS (SELECT dbo.T_Customer.CustomerID
FROM dbo.T_Customer INNER JOIN
dbo.T_SYS_ShopSystem ON dbo.T_Customer.site = dbo.T_SYS_ShopSystem.shopid
WHERE (dbo.T_SYS_ShopSystem.shopid = @shopid))

INSERT INTO T_Customer_Mailbox
(userid, useremail, Subject, ordernr, mailbody, replytoemail,answerdby,[status],lang)
SELECT dbo.T_Customer.CustomerID,dbo.T_SYS_ShopSystem.managername, @subject, 0, @body, 0, 'system',2,
dbo.T_SYS_ShopSystem.shoplang
FROM dbo.T_Customer INNER JOIN
dbo.T_SYS_ShopSystem ON dbo.T_Customer.site = dbo.T_SYS_ShopSystem.shopid
WHERE (dbo.T_SYS_ShopSystem.shopid = @shopid)
end

tnx a lot,

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/26/2013 :  11:23:35  Show Profile  Reply with Quote
why do you need a loop?
the below code

...
INSERT INTO T_Customer_Mailbox
(userid, useremail, Subject, ordernr, mailbody, replytoemail,answerdby,[status],lang)
SELECT dbo.T_Customer.CustomerID,dbo.T_SYS_ShopSystem.managername, @subject, 0, @body, 0, 'system',2, 
dbo.T_SYS_ShopSystem.shoplang
FROM dbo.T_Customer INNER JOIN
dbo.T_SYS_ShopSystem ON dbo.T_Customer.site = dbo.T_SYS_ShopSystem.shopid
WHERE (dbo.T_SYS_ShopSystem.shopid = @shopid)
....


itself will insert all customer records belong to shopid which is passed

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

mike13
Posting Yak Master

Netherlands
214 Posts

Posted - 06/26/2013 :  11:29:28  Show Profile  Click to see mike13's MSN Messenger address  Reply with Quote
Time outs 250.000 records
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/26/2013 :  12:53:01  Show Profile  Reply with Quote
have a look at execution plan and see what are the costly steps

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

mike13
Posting Yak Master

Netherlands
214 Posts

Posted - 06/26/2013 :  12:58:06  Show Profile  Click to see mike13's MSN Messenger address  Reply with Quote
mmm can you just answer my question?
how can i do a loop
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/26/2013 :  13:16:38  Show Profile  Reply with Quote
why do you need a loop here? as i see there's nothing that calls for a loop. You just need a set based insert.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 06/26/2013 :  14:58:32  Show Profile  Reply with Quote
quote:
Originally posted by mike13

mmm can you just answer my question?
how can i do a loop



Your loop is an infinite one .. and that okay, if you're that much interested in a looping method, instead of using a better approach as suggested, then here's an example of the loop

CREATE PROCEDURE [dbo].[SP_BACK_mail_Send_Massmail]

@subject as nvarchar(50),
@body as nvarchar(max),
@shopid as nvarchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Begin
WHILE EXISTS (SELECT dbo.T_Customer.CustomerID
FROM dbo.T_Customer INNER JOIN
dbo.T_SYS_ShopSystem ON dbo.T_Customer.site = dbo.T_SYS_ShopSystem.shopid
WHERE (dbo.T_SYS_ShopSystem.shopid = @shopid))

INSERT INTO T_Customer_Mailbox
(userid, useremail, Subject, ordernr, mailbody, replytoemail,answerdby,[status],lang)
SELECT dbo.T_Customer.CustomerID,dbo.T_SYS_ShopSystem.managername, @subject, 0, @body, 0, 'system',2,
dbo.T_SYS_ShopSystem.shoplang
FROM dbo.T_Customer INNER JOIN
dbo.T_SYS_ShopSystem ON dbo.T_Customer.site = dbo.T_SYS_ShopSystem.shopid
WHERE (dbo.T_SYS_ShopSystem.shopid = @shopid)

Return;

end


Cheers
MIK
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000