Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2010-12-03 : 04:37:08
|
| Good morning everyone. I am trying to create the below into a stored procedure. When i run the script manually the desired result are as follows.0106500NULL10but when i execute the stored proc i get0I dont understand why it is returning nothing when running stored proc.CREATE procedure Nominees_Listas truncate table nominess_list--New Nomineesgoif exists (select * FROM sysobjects where name = 'New_Nominees' ) --select * from monthly_stats DROP table New_Nomineesselect distinct (organisation.organisation_ref) into New_Nomineesfrom organisation WITH (NOLOCK) , member , member member3 where member.join_date between '15-Nov-2010' AND '22-Nov-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 33 and member.member_class = 1286 and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , New_Nominees WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = New_Nominees.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from Nominessgo--Renewal Nonineesif exists (select * FROM sysobjects where name = 'Renewed_Orgs' ) --select * from monthly_stats DROP table Renewed_Orgsselect distinct (organisation.organisation_ref) into Renewed_Orgsfrom organisation WITH (NOLOCK) , member, membership_history membership_histor3 WITH (NOLOCK) , member member3 WITH (NOLOCK) where member.join_date < '23-Mar-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 33 and member.member_class = 1286 and membership_histor3.member_plan_ref IN ( 24, 25, 22, 23 )and membership_histor3.history_status = 249 and membership_histor3.status_date BETWEEN '15-Nov-2010' AND '22-Nov-2010' and (membership_histor3.member_ref = member3.member_ref or membership_histor3.feepaying_member = member3.member_ref) and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , Renewed_Orgs WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = Renewed_Orgs.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from Nominessgo--Resigned Nomineesif exists (select * FROM sysobjects where name = 'New_Nominees' ) --select * from monthly_stats DROP table New_Nomineesselect distinct (organisation.organisation_ref) into New_Nomineesfrom organisation WITH (NOLOCK) , member , member member3 where member.leave_date between '15-Nov-2009' AND '22-Nov-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 1130and member.member_class = 1286 and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , New_Nominees WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = New_Nominees.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from Nominessgo--Deceased Nomineesif exists (select * FROM sysobjects where name = 'New_Nominees' ) --select * from monthly_stats DROP table New_Nomineesselect distinct (organisation.organisation_ref) into New_Nomineesfrom organisation WITH (NOLOCK) , member , member member3 where member.leave_date between '15-Nov-2009' AND '22-Nov-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 1379and member.member_class = 1286 and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , New_Nominees WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = New_Nominees.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from Nominessgo--Lapsed Nomineesif exists (select * FROM sysobjects where name = 'New_Nominees' ) --select * from monthly_stats DROP table New_Nomineesselect distinct (organisation.organisation_ref) into New_Nomineesfrom organisation WITH (NOLOCK) , member , member member3 where member.leave_date between '15-Nov-2009' AND '22-Nov-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 1331and member.member_class = 1286 and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , New_Nominees WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = New_Nominees.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from Nominessgo--New Nominees £'s Total Amountif exists (select * FROM sysobjects where name = 'New_Nominees' ) --select * from monthly_stats DROP table New_Nomineesselect distinct (organisation.organisation_ref) into New_Nomineesfrom organisation WITH (NOLOCK) , member , member member3 where member.join_date between '15-Nov-2010' AND '22-Nov-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 33 and member.member_class = 1286 and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , New_Nominees WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = New_Nominees.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list (Total)select sum(Total_Amount) from individual WITH (NOLOCK) , member , membership_history, Nominess--select * from individual WITH (NOLOCK) , member , membership_historyinner join contact on contact.contact_ref = Nominess.contact_refinner join member mem on contact.individual_ref = mem.individual_refwhere member.join_date BETWEEN '15-Nov-2010' AND '22-Nov-2010' and member.individual_ref = individual.individual_ref and member.grad_date BETWEEN '15-Nov-2010' AND '22-Nov-2010' and member.member_status = 33 and member.member_class = 1286and member.member_ref = membership_history.member_refand membership_history.history_status = 249and membership_history.member_plan_ref IN ( 264, 309, 308, 265, 302, 304, 305, 303, 11, 12, 282, 283, 243, 244, 17, 18, 19, 15, 16, 20, 21, 28, 29, 13, 14, 35, 36 )and membership_history.status_date BETWEEN '15-Nov-2010' AND '22-Nov-2010' go-- Renewal Nominees £'s Total Amountif exists (select * FROM sysobjects where name = 'Renewed_Orgs' ) --select * from monthly_stats DROP table Renewed_Orgsselect distinct (organisation.organisation_ref) into Renewed_Orgsfrom organisation WITH (NOLOCK) , member, membership_history membership_histor3 WITH (NOLOCK) , member member3 WITH (NOLOCK) where member.join_date < '01-Mar-2010' and member.organisation_ref = organisation.organisation_ref and member.member_status = 33 and member.member_class = 1286 and membership_histor3.member_plan_ref IN ( 24, 25, 22, 23 )and membership_histor3.history_status = 249 and membership_histor3.status_date BETWEEN '15-Nov-2010' AND '22-Nov-2010' and (membership_histor3.member_ref = member3.member_ref or membership_histor3.feepaying_member = member3.member_ref) and member3.organisation_ref = organisation.organisation_refif exists(select * FROM sysobjects where name = 'Nominess' ) DROP table Nominessselect DISTINCT contact.contact_ref into Nominess from contact WITH (NOLOCK) , organisation WITH (NOLOCK) , Renewed_Orgs WITH (NOLOCK) where 1=1 AND contact.organisation_ref = organisation.organisation_ref AND organisation.organisation_ref = Renewed_Orgs.organisation_refDelete from Nominess where contact_ref in(Select N.contact_ref from Nominess N inner join contact C on C.contact_ref = N.contact_refwhere C.Main_Contact = 'Y')insert into nominess_list ( Total)select count(contact_ref) from NominessMCTS / MCITP certified |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-12-03 : 04:42:19
|
This:CREATE procedure Nominees_Listas truncate table nominess_listis your whole SP because after that you are using GO. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-12-03 : 04:43:32
|
edit: slipslop deleted |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-03 : 04:44:09
|
| A go will termoinate the batch - i.e. be the end of the stored procedureYour stored procedure isCREATE procedure Nominees_Listastruncate table nominess_list--New NomineesgoThe other statements will be executed when you try to cretae the sp but won't be execited when you run it.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2010-12-03 : 04:56:57
|
| I am human webfred. not a robot. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-12-03 : 05:48:58
|
quote: Originally posted by masterdineen I am human webfred. not a robot.
I am really sorry.Please bear with me.I shouldn't have posted that.Again sorry, I was in a bad temper.OK?  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2010-12-03 : 06:45:51
|
| not a problem.It must of seem like a really crapy post. I seem to get the most easiest things wrong, and manage to get the harder things correct.Strange. sorry about that |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-12-03 : 06:48:03
|
| If that's the worst you do when you lose your temper you need to try harder :).==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-12-03 : 06:56:33
|
quote: Originally posted by webfred
quote: Originally posted by masterdineen I am human webfred. not a robot.
I am really sorry.Please bear with me.I shouldn't have posted that.Again sorry, I was in a bad temper.OK?  No, you're never too old to Yak'n'Roll if you're too young to die.
So what did I miss ? PBUH |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-12-03 : 07:10:55
|
| "WITH (NOLOCK)"Only use if you know how dangerous the side effects are, and are handling them in your application and/or the data is not being used for line-of-business decisions, or users are aware of the ramificationsWhat most people who use NOLOCK actually want is READ_COMMITTED_SNAPSHOT [introduced in SQL2005] (its a database setting so you don't need anything in your code per se, after setting it, but you will need to remove all NOLOCK from your code unless you are comfortable with the side effects). |
 |
|
|
|
|
|
|
|